api_w_random.go 739 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. package route
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "log"
  7. "opennamu/route/tool"
  8. )
  9. func Api_w_random(call_arg []string) {
  10. db_set := map[string]string{}
  11. json.Unmarshal([]byte(call_arg[0]), &db_set)
  12. db := tool.DB_connect(db_set)
  13. if db == nil {
  14. return
  15. }
  16. defer db.Close()
  17. var title string
  18. err := db.QueryRow(tool.DB_change(db_set, "select title from data where title not like 'user:%' and title not like 'category:%' and title not like 'file:%' order by random() limit 1")).Scan(&title)
  19. if err != nil {
  20. if err == sql.ErrNoRows {
  21. title = ""
  22. } else {
  23. log.Fatal(err)
  24. }
  25. }
  26. new_data := map[string]string{}
  27. new_data["data"] = title
  28. json_data, _ := json.Marshal(new_data)
  29. fmt.Print(string(json_data))
  30. }