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.

108 lines
2.2 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "path"
  7. "strings"
  8. "time"
  9. "git.aiterp.net/rpdata/api/internal/store"
  10. logModel "git.aiterp.net/rpdata/api/model/log"
  11. )
  12. var prefixReplacer = strings.NewReplacer("+", "", "@", "", "!", "", "%", "")
  13. func main() {
  14. err := store.Init()
  15. if err != nil {
  16. log.Fatalln(err)
  17. }
  18. for _, filepath := range os.Args[1:] {
  19. name := strings.Replace(path.Base(filepath), ".txt", "", 1)
  20. file, err := os.Open(filepath)
  21. if err != nil {
  22. log.Println(filepath, err)
  23. continue
  24. }
  25. logLines := parseFile(file)
  26. file.Close()
  27. // Get title and event
  28. title := ""
  29. event := ""
  30. for _, line := range logLines {
  31. if line.Verb == "TITLE" {
  32. title = line.Text
  33. }
  34. if line.Verb == "TAG" {
  35. event = line.Text
  36. }
  37. }
  38. date, channel, err := parseFilename(name)
  39. if err != nil {
  40. log.Fatalln(err)
  41. }
  42. l, err := logModel.New(date, channel, title, event, "", false)
  43. if err != nil {
  44. log.Println(err)
  45. continue
  46. }
  47. _, err = l.NewPost(time.Now(), "annotation.info", "rpdata-lb2logimport", "This logfile is imported from aitelogs2 and may contain errors or wrong timestamps.")
  48. if err != nil {
  49. log.Println(err)
  50. }
  51. for _, line := range logLines {
  52. if line.Verb != "CHARS" {
  53. continue
  54. }
  55. _, err = l.NewPost(l.Date, "chars", prefixReplacer.Replace(line.Args[0]), line.Text)
  56. if err != nil {
  57. log.Println(err)
  58. }
  59. }
  60. for _, line := range logLines {
  61. if line.Verb != "SCENE" && line.Verb != "ACTION" && line.Verb != "TEXT" {
  62. continue
  63. }
  64. postTime, err := time.ParseInLocation("2006-01-02 15:04:05", date.Format("2006-01-02")+" "+line.Args[1], time.Local)
  65. diff := postTime.Sub(date)
  66. if err != nil {
  67. log.Println(err)
  68. continue
  69. }
  70. if diff < 0 {
  71. if diff > -time.Second {
  72. postTime = postTime.Add(diff)
  73. } else {
  74. postTime = postTime.Add(time.Hour * 24)
  75. }
  76. }
  77. if line.Args[0][0] == '=' {
  78. line.Verb = "SCENE"
  79. }
  80. _, err = l.NewPost(postTime, strings.ToLower(line.Verb), prefixReplacer.Replace(line.Args[0]), line.Text)
  81. if err != nil {
  82. log.Println(err)
  83. }
  84. }
  85. err = l.UpdateCharacters()
  86. if err != nil {
  87. log.Println(err)
  88. }
  89. fmt.Println(l.ID, "completed")
  90. }
  91. }