package main import ( "flag" "fmt" "log" "strings" "time" "git.aiterp.net/rpdata/api/internal/store" "git.aiterp.net/rpdata/api/model/story" _ "github.com/go-sql-driver/mysql" "github.com/jmoiron/sqlx" ) var flagHost = flag.String("host", "127.0.0.1:3306", "SQL host") var flagDB = flag.String("database", "aitestory", "SQL database") var flagUser = flag.String("user", "aitestory", "SQL user") var flagPassword = flag.String("password", "", "SQL password") func main() { flag.Parse() db, err := sqlx.Connect("mysql", fmt.Sprintf("%s:%s@(%s)/%s", *flagUser, *flagPassword, *flagHost, *flagDB)) if err != nil { log.Fatalln(err) } err = db.Ping() if err != nil { log.Fatalln(err) } store.Init() results := make([]storyResult, 0, 64) rows, err := db.Queryx("SELECT * FROM page WHERE unlisted=0;") for rows.Next() { result := storyResult{} err := rows.StructScan(&result) if err != nil { log.Fatalln(err) } results = append(results, result) } tagResults := make([]tagResult, 0, 256) rows, err = db.Queryx("SELECT page_id,type,name FROM page_tag LEFT JOIN tag ON tag_id=tag.id;") for rows.Next() { result := tagResult{} err := rows.StructScan(&result) if err != nil { log.Fatalln(err) } tagResults = append(tagResults, result) } for _, result := range results { fictionalDate, err := time.Parse("2006-01-02 15:04:05", result.FictionalDate) if err != nil { if result.FictionalDate != "0000-00-00 00:00:00" { log.Fatalln(err) } } if fictionalDate.Year() < 1800 { fictionalDate = time.Time{} } publishDate, err := time.Parse("2006-01-02 15:04:05", result.PublishDate) if err != nil { log.Fatalln(err) } tags := make([]story.Tag, 0, 8) for _, tagResult := range tagResults { if tagResult.PageID == result.ID { tags = append(tags, story.Tag{Kind: tagResult.Type, Name: tagResult.Name}) } } story, err := story.New(result.Name, result.Author, result.Category, false, false, tags, publishDate, fictionalDate) if err != nil { log.Fatalln(err) } // Change the story title title := result.Name if strings.HasPrefix(result.Source, "#") { firstNewline := strings.Index(result.Source, "\n") result.Name = strings.Replace(result.Source[1:firstNewline], "\r", "", -1) result.Source = result.Source[firstNewline+1:] result.Source = strings.Replace(result.Source, "\r\n", "\n", -1) if strings.HasPrefix(result.Source, "\r") || strings.HasPrefix(result.Source, "\n") { result.Source = result.Source[1:] } } chapter, err := story.AddChapter(title, result.Author, result.Source, publishDate, fictionalDate) if err != nil { log.Fatalln(err) } if !result.Unlisted { listed := true err := story.Edit(nil, nil, &listed, nil, nil) if err != nil { log.Fatalln(err) } } fmt.Println(result.ID, "->", story.ID, chapter.ID) } } type tagResult struct { PageID string `db:"page_id"` Type string `db:"type"` Name string `db:"name"` } type storyResult struct { ID string `db:"id"` Name string `db:"name"` Author string `db:"author"` Category string `db:"category"` FictionalDate string `db:"fictional_date"` PublishDate string `db:"publish_date"` EditDate string `db:"edit_date"` Unlisted bool `db:"unlisted"` Dated bool `db:"dated"` Spesific bool `db:"specific"` Indexed bool `db:"indexed"` Published bool `db:"published"` Type string `db:"type"` Source string `db:"source"` Cache string `db:"cache"` BackgroundURL *string `db:"background_url"` }