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.

131 lines
3.4 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. // 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, result.Source, publishDate, fictionalDate)
  84. if err != nil {
  85. log.Fatalln(err)
  86. }
  87. fmt.Println(result.ID, "->", story.ID, chapter.ID)
  88. }
  89. }
  90. type tagResult struct {
  91. PageID string `db:"page_id"`
  92. Type string `db:"type"`
  93. Name string `db:"name"`
  94. }
  95. type storyResult struct {
  96. ID string `db:"id"`
  97. Name string `db:"name"`
  98. Author string `db:"author"`
  99. Category string `db:"category"`
  100. FictionalDate string `db:"fictional_date"`
  101. PublishDate string `db:"publish_date"`
  102. EditDate string `db:"edit_date"`
  103. Unlisted bool `db:"unlisted"`
  104. Dated bool `db:"dated"`
  105. Spesific bool `db:"specific"`
  106. Indexed bool `db:"indexed"`
  107. Published bool `db:"published"`
  108. Type string `db:"type"`
  109. Source string `db:"source"`
  110. Cache string `db:"cache"`
  111. BackgroundURL *string `db:"background_url"`
  112. }