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.

125 lines
3.2 KiB

  1. package main
  2. import (
  3. "flag"
  4. "fmt"
  5. "log"
  6. "strings"
  7. "time"
  8. "git.aiterp.net/rpdata/api/internal/store"
  9. "git.aiterp.net/rpdata/api/model/story"
  10. _ "github.com/go-sql-driver/mysql"
  11. "github.com/jmoiron/sqlx"
  12. )
  13. var flagHost = flag.String("host", "127.0.0.1:3306", "SQL host")
  14. var flagDB = flag.String("database", "aitestory", "SQL database")
  15. var flagUser = flag.String("user", "aitestory", "SQL user")
  16. var flagPassword = flag.String("password", "", "SQL password")
  17. func main() {
  18. flag.Parse()
  19. db, err := sqlx.Connect("mysql", fmt.Sprintf("%s:%s@(%s)/%s", *flagUser, *flagPassword, *flagHost, *flagDB))
  20. if err != nil {
  21. log.Fatalln(err)
  22. }
  23. err = db.Ping()
  24. if err != nil {
  25. log.Fatalln(err)
  26. }
  27. store.Init()
  28. results := make([]storyResult, 0, 64)
  29. rows, err := db.Queryx("SELECT * FROM page WHERE unlisted=0;")
  30. for rows.Next() {
  31. result := storyResult{}
  32. err := rows.StructScan(&result)
  33. if err != nil {
  34. log.Fatalln(err)
  35. }
  36. results = append(results, result)
  37. }
  38. tagResults := make([]tagResult, 0, 256)
  39. rows, err = db.Queryx("SELECT page_id,type,name FROM page_tag LEFT JOIN tag ON tag_id=tag.id;")
  40. for rows.Next() {
  41. result := tagResult{}
  42. err := rows.StructScan(&result)
  43. if err != nil {
  44. log.Fatalln(err)
  45. }
  46. tagResults = append(tagResults, result)
  47. }
  48. for _, result := range results {
  49. fictionalDate, err := time.Parse("2006-01-02 15:04:05", result.FictionalDate)
  50. if err != nil {
  51. if result.FictionalDate != "0000-00-00 00:00:00" {
  52. log.Fatalln(err)
  53. }
  54. }
  55. if fictionalDate.Year() < 1800 {
  56. fictionalDate = time.Time{}
  57. }
  58. publishDate, err := time.Parse("2006-01-02 15:04:05", result.PublishDate)
  59. if err != nil {
  60. log.Fatalln(err)
  61. }
  62. tags := make([]story.Tag, 0, 8)
  63. for _, tagResult := range tagResults {
  64. if tagResult.PageID == result.ID {
  65. tags = append(tags, story.Tag{Kind: tagResult.Type, Name: tagResult.Name})
  66. }
  67. }
  68. story, err := story.New(result.Name, result.Author, result.Category, false, false, tags, publishDate, fictionalDate)
  69. if err != nil {
  70. log.Fatalln(err)
  71. }
  72. title := result.Name
  73. if strings.HasPrefix(result.Source, "#") {
  74. firstNewline := strings.Index(result.Source, "\n")
  75. title = result.Source[1:firstNewline]
  76. result.Source = result.Source[firstNewline+1:]
  77. }
  78. chapter, err := story.AddChapter(title, result.Author, result.Source, publishDate, fictionalDate)
  79. if err != nil {
  80. log.Fatalln(err)
  81. }
  82. fmt.Println(result.ID, "->", story.ID, chapter.ID)
  83. }
  84. }
  85. type tagResult struct {
  86. PageID string `db:"page_id"`
  87. Type string `db:"type"`
  88. Name string `db:"name"`
  89. }
  90. type storyResult struct {
  91. ID string `db:"id"`
  92. Name string `db:"name"`
  93. Author string `db:"author"`
  94. Category string `db:"category"`
  95. FictionalDate string `db:"fictional_date"`
  96. PublishDate string `db:"publish_date"`
  97. EditDate string `db:"edit_date"`
  98. Unlisted bool `db:"unlisted"`
  99. Dated bool `db:"dated"`
  100. Spesific bool `db:"specific"`
  101. Indexed bool `db:"indexed"`
  102. Published bool `db:"published"`
  103. Type string `db:"type"`
  104. Source string `db:"source"`
  105. Cache string `db:"cache"`
  106. BackgroundURL *string `db:"background_url"`
  107. }