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.

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