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.6 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[5:], result.Category, false, false, tags, publishDate, fictionalDate)
  69. if err != nil {
  70. log.Fatalln(err)
  71. }
  72. // Change the story title
  73. title := result.Name
  74. if strings.HasPrefix(result.Source, "#") {
  75. firstNewline := strings.Index(result.Source, "\n")
  76. result.Name = strings.Replace(result.Source[1:firstNewline], "\r", "", -1)
  77. result.Source = result.Source[firstNewline+1:]
  78. result.Source = strings.Replace(result.Source, "\r\n", "\n", -1)
  79. if strings.HasPrefix(result.Source, "\r") || strings.HasPrefix(result.Source, "\n") {
  80. result.Source = result.Source[1:]
  81. }
  82. }
  83. chapter, err := story.AddChapter(title, result.Author[5:], result.Source, publishDate, fictionalDate)
  84. if err != nil {
  85. log.Fatalln(err)
  86. }
  87. if !result.Unlisted {
  88. listed := true
  89. err := story.Edit(nil, nil, &listed, nil, nil)
  90. if err != nil {
  91. log.Fatalln(err)
  92. }
  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. }