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.

83 lines
1.7 KiB

  1. package main
  2. import (
  3. "flag"
  4. "log"
  5. "os"
  6. "time"
  7. "git.aiterp.net/rpdata/api/internal/store"
  8. "git.aiterp.net/rpdata/api/models/posts"
  9. "git.aiterp.net/rpdata/api/models/logs"
  10. )
  11. var dateStr string
  12. var tzStr string
  13. var channel string
  14. var fileName string
  15. func main() {
  16. var inputPosts []Post
  17. flag.StringVar(&dateStr, "date", "2013-04-04", "Date in YYYY-MM-DD form.")
  18. flag.StringVar(&tzStr, "tz", "Europe/Oslo", "Time zone for the date.")
  19. flag.StringVar(&channel, "channel", "#RedrockAgency", "Channel it's set in.")
  20. flag.StringVar(&fileName, "file", "-", "File to read from (- = stdin).")
  21. flag.Parse()
  22. tz, err := time.LoadLocation(tzStr)
  23. if err != nil {
  24. log.Fatalln("Parse timezone:", err)
  25. }
  26. date, err := time.ParseInLocation("2006-01-02", dateStr, tz)
  27. if err != nil {
  28. log.Fatalln("Parse date:", err)
  29. }
  30. err = store.Init()
  31. if err != nil {
  32. log.Fatalln("Init store:", err)
  33. }
  34. if fileName != "-" {
  35. file, fileErr := os.Open(fileName)
  36. if fileErr != nil {
  37. log.Fatalln("Open file:", fileErr)
  38. }
  39. inputPosts, err = parsePosts(file, date)
  40. file.Close()
  41. } else {
  42. inputPosts, err = parsePosts(os.Stdin, date)
  43. }
  44. if err != nil {
  45. log.Fatalln("Read inputPosts:", err)
  46. }
  47. if len(inputPosts) == 0 {
  48. log.Fatalln("Input file contained no posts.")
  49. }
  50. l, err := logs.Add(inputPosts[0].Time, channel, "", "", "", false)
  51. if err != nil {
  52. log.Fatalln("Add log:", err)
  53. }
  54. log.Println("Added log", l.ID)
  55. for _, inputPost := range inputPosts {
  56. p, err := posts.Add(l, inputPost.Time, inputPost.Kind, inputPost.Nick, inputPost.Text)
  57. if err != nil {
  58. log.Fatalln("Add post:", err)
  59. }
  60. log.Println("Added post", p.ID)
  61. }
  62. _, err = logs.UpdateCharacters(l)
  63. if err != nil {
  64. log.Println("Warning, failed to update characters:", err)
  65. }
  66. }