The storage system and conversion tools for the new Logs website.
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.

98 lines
2.0 KiB

  1. package main
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "strings"
  7. "time"
  8. )
  9. // LogFile contains the meta-data and entries
  10. // of a lobgot3 file.
  11. type LogFile struct {
  12. Path string
  13. Channel string
  14. Time time.Time
  15. Title string
  16. Tag string
  17. Entries []LogEntry
  18. }
  19. // LogEntry represents one line in a log-file
  20. type LogEntry struct {
  21. Type string
  22. Time time.Time
  23. Nick string
  24. Text string
  25. }
  26. // NewLogFile creates a new logfile, but it doesn't
  27. // load it.
  28. func NewLogFile(path string) *LogFile {
  29. split := strings.Split(path, "/")
  30. name := strings.Replace(split[len(split)-1], ".txt", "", 1)
  31. tokens := strings.Split(name, "_")
  32. channel := tokens[len(tokens)-1]
  33. parsedTime, err := time.Parse("2006-01-02 150405", strings.Join(tokens[0:2], " ")[:17])
  34. if err != nil {
  35. log.Fatalf("Failed to parse filename %s:\n\t%s", path, err)
  36. }
  37. logFile := new(LogFile)
  38. logFile.Path = path
  39. logFile.Channel = channel
  40. logFile.Time = parsedTime
  41. return logFile
  42. }
  43. // Load loads the file and populates the remaining data
  44. func (logFile *LogFile) Load() {
  45. file, err := os.Open(logFile.Path)
  46. if err != nil {
  47. log.Fatalf("Failed to open file %s:\n\t%s", logFile.Path, err)
  48. }
  49. data, err := ioutil.ReadAll(file)
  50. if err != nil || len(data) == 0 {
  51. log.Fatalf("Failed to read file %s:\n\t%s", logFile.Path, err)
  52. }
  53. rootDate := logFile.Time.Format("2006-01-02")
  54. lines := strings.Split(string(data), "\n")
  55. for _, line := range lines {
  56. split := strings.Split(line, " :")
  57. tokens := strings.Split(split[0], " ")
  58. text := ""
  59. if len(split) > 1 {
  60. text = split[1]
  61. }
  62. switch tokens[0] {
  63. case "ACTION", "TEXT":
  64. {
  65. parsedTime, _ := time.Parse("2006-01-02 15:04:05", rootDate+" "+tokens[2])
  66. entry := LogEntry{Nick: tokens[1], Text: text, Type: tokens[0], Time: parsedTime}
  67. // Handle midnights
  68. if entry.Time.Before(logFile.Time) {
  69. entry.Time = entry.Time.Add(time.Hour * 24)
  70. }
  71. logFile.Entries = append(logFile.Entries, entry)
  72. }
  73. case "TITLE":
  74. {
  75. logFile.Title = text
  76. }
  77. case "TAG":
  78. {
  79. logFile.Tag = text
  80. }
  81. }
  82. }
  83. }