api_w_random.go 707 B

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