GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.0 KiB

  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "time"
  8. "git.aiterp.net/rpdata/api/internal/config"
  9. "git.aiterp.net/rpdata/api/internal/store"
  10. "git.aiterp.net/rpdata/api/models/files"
  11. )
  12. func main() {
  13. client := http.Client{Timeout: time.Second * 30}
  14. res, err := client.Get(config.Global().Wiki.URL + "?action=query&list=allimages&ailimit=5&aiprop=dimensions|mime|url|user|timestamp&format=json&ailimit=500")
  15. if err != nil {
  16. log.Fatalln(err)
  17. }
  18. if res.StatusCode != 200 {
  19. log.Fatalln(res.Status)
  20. }
  21. data := ResponseData{}
  22. err = json.NewDecoder(res.Body).Decode(&data)
  23. if err != nil {
  24. log.Fatalln(err)
  25. }
  26. store.Init()
  27. for _, info := range data.Query.Allimages {
  28. existing, err := files.FindName("wiki", info.Name, info.User)
  29. if err == nil && existing.ID != "" {
  30. log.Println("Skipping", info.Name, "because it already exists")
  31. continue
  32. }
  33. ts, err := time.Parse(time.RFC3339, info.Timestamp)
  34. if err != nil {
  35. log.Println("Skipping", info.Name, "error:", err)
  36. continue
  37. }
  38. file, err := files.Insert(info.Name, "wiki", info.Mime, info.User, ts, info.Size, info.URL)
  39. if err != nil {
  40. log.Println("Skipping", info.Name, "error:", err)
  41. continue
  42. }
  43. fmt.Println(file.Name, "inserted ( id:", file.ID, ")")
  44. }
  45. }
  46. // The ResponseData from the wiki looks like this
  47. type ResponseData struct {
  48. Batchcomplete string `json:"batchcomplete"`
  49. Query struct {
  50. Allimages []struct {
  51. Descriptionshorturl string `json:"descriptionshorturl"`
  52. Descriptionurl string `json:"descriptionurl"`
  53. Height int `json:"height"`
  54. Mime string `json:"mime"`
  55. Name string `json:"name"`
  56. Ns int `json:"ns"`
  57. Size int64 `json:"size"`
  58. Timestamp string `json:"timestamp"`
  59. Title string `json:"title"`
  60. URL string `json:"url"`
  61. User string `json:"user"`
  62. Width int `json:"width"`
  63. } `json:"allimages"`
  64. } `json:"query"`
  65. }