api_w_random.go 724 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package route
  2. import (
  3. "database/sql"
  4. "encoding/json"
  5. "fmt"
  6. "opennamu/route/tool"
  7. )
  8. func Api_w_random(call_arg []string) {
  9. db_set := map[string]string{}
  10. json.Unmarshal([]byte(call_arg[0]), &db_set)
  11. db := tool.DB_connect(db_set)
  12. if db == nil {
  13. return
  14. }
  15. defer db.Close()
  16. var title string
  17. 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)
  18. if err != nil {
  19. if err == sql.ErrNoRows {
  20. title = ""
  21. } else {
  22. return
  23. }
  24. }
  25. new_data := map[string]string{}
  26. new_data["data"] = title
  27. json_data, _ := json.Marshal(new_data)
  28. fmt.Print(string(json_data))
  29. }