package main import ( "fmt" "log" "os" "path" "strings" "time" "git.aiterp.net/rpdata/api/models/posts" "git.aiterp.net/rpdata/api/internal/store" "git.aiterp.net/rpdata/api/models/logs" ) var prefixReplacer = strings.NewReplacer("+", "", "@", "", "!", "", "%", "") func main() { err := store.Init() if err != nil { log.Fatalln(err) } for _, filepath := range os.Args[1:] { name := strings.Replace(path.Base(filepath), ".txt", "", 1) file, err := os.Open(filepath) if err != nil { log.Println(filepath, err) continue } logLines := parseFile(file) file.Close() // Get title and event title := "" event := "" for _, line := range logLines { if line.Verb == "TITLE" { title = line.Text } if line.Verb == "TAG" { event = line.Text } } date, channel, err := parseFilename(name) if err != nil { log.Fatalln(err) } l, err := logs.Add(date, channel, title, event, "", false) if err != nil { log.Println(err) continue } _, err = posts.Add(l, time.Now(), "annotation.info", "rpdata-lb2logimport", "This logfile is imported from aitelogs2 and may contain errors or wrong timestamps.") if err != nil { log.Println(err) } for _, line := range logLines { if line.Verb != "CHARS" { continue } _, err = posts.Add(l, l.Date, "chars", prefixReplacer.Replace(line.Args[0]), line.Text) if err != nil { log.Println(err) } } for _, line := range logLines { if line.Verb != "SCENE" && line.Verb != "ACTION" && line.Verb != "TEXT" { continue } postTime, err := time.ParseInLocation("2006-01-02 15:04:05", date.Format("2006-01-02")+" "+line.Args[1], time.Local) diff := postTime.Sub(date) if err != nil { log.Println(err) continue } if diff < 0 { if diff > -time.Second { postTime = postTime.Add(diff) } else { postTime = postTime.Add(time.Hour * 24) } } if line.Args[0][0] == '=' { line.Verb = "SCENE" } _, err = posts.Add(l, postTime, strings.ToLower(line.Verb), prefixReplacer.Replace(line.Args[0]), line.Text) if err != nil { log.Println(err) } } l, err = logs.UpdateCharacters(l) if err != nil { log.Println(err) } fmt.Println(l.ID, "completed") } }