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.

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