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

package main
import (
"io/ioutil"
"log"
"os"
"strings"
"time"
)
// LogFile contains the meta-data and entries
// of a lobgot3 file.
type LogFile struct {
Path string
Channel string
Time time.Time
Title string
Tag string
Entries []LogEntry
}
// LogEntry represents one line in a log-file
type LogEntry struct {
Type string
Time time.Time
Nick string
Text string
}
// NewLogFile creates a new logfile, but it doesn't
// load it.
func NewLogFile(path string) *LogFile {
split := strings.Split(path, "/")
name := strings.Replace(split[len(split)-1], ".txt", "", 1)
tokens := strings.Split(name, "_")
channel := tokens[len(tokens)-1]
parsedTime, err := time.Parse("2006-01-02 150405", strings.Join(tokens[0:2], " ")[:17])
if err != nil {
log.Fatalf("Failed to parse filename %s:\n\t%s", path, err)
}
logFile := new(LogFile)
logFile.Path = path
logFile.Channel = channel
logFile.Time = parsedTime
return logFile
}
// Load loads the file and populates the remaining data
func (logFile *LogFile) Load() {
file, err := os.Open(logFile.Path)
if err != nil {
log.Fatalf("Failed to open file %s:\n\t%s", logFile.Path, err)
}
data, err := ioutil.ReadAll(file)
if err != nil || len(data) == 0 {
log.Fatalf("Failed to read file %s:\n\t%s", logFile.Path, err)
}
rootDate := logFile.Time.Format("2006-01-02")
lines := strings.Split(string(data), "\n")
for _, line := range lines {
split := strings.Split(line, " :")
tokens := strings.Split(split[0], " ")
text := ""
if len(split) > 1 {
text = split[1]
}
switch tokens[0] {
case "ACTION", "TEXT":
{
parsedTime, _ := time.Parse("2006-01-02 15:04:05", rootDate+" "+tokens[2])
entry := LogEntry{Nick: tokens[1], Text: text, Type: tokens[0], Time: parsedTime}
// Handle midnights
if entry.Time.Before(logFile.Time) {
entry.Time = entry.Time.Add(time.Hour * 24)
}
logFile.Entries = append(logFile.Entries, entry)
}
case "TITLE":
{
logFile.Title = text
}
case "TAG":
{
logFile.Tag = text
}
}
}
}