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.
78 lines
1.6 KiB
78 lines
1.6 KiB
package main
|
|
|
|
import (
|
|
"flag"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"git.aiterp.net/rpdata/api/internal/store"
|
|
"git.aiterp.net/rpdata/api/models/posts"
|
|
|
|
"git.aiterp.net/rpdata/api/models/logs"
|
|
)
|
|
|
|
var dateStr string
|
|
var tzStr string
|
|
var channel string
|
|
var fileName string
|
|
|
|
func main() {
|
|
var inputPosts []Post
|
|
|
|
flag.StringVar(&dateStr, "date", "2013-04-04", "Date in YYYY-MM-DD form.")
|
|
flag.StringVar(&tzStr, "tz", "Europe/Oslo", "Time zone for the date.")
|
|
flag.StringVar(&channel, "channel", "#RedrockAgency", "Channel it's set in.")
|
|
flag.StringVar(&fileName, "file", "-", "File to read from (- = stdin).")
|
|
flag.Parse()
|
|
|
|
tz, err := time.LoadLocation(tzStr)
|
|
if err != nil {
|
|
log.Fatalln("Parse timezone:", err)
|
|
}
|
|
|
|
date, err := time.ParseInLocation("2006-01-02", dateStr, tz)
|
|
if err != nil {
|
|
log.Fatalln("Parse date:", err)
|
|
}
|
|
|
|
err = store.Init()
|
|
if err != nil {
|
|
log.Fatalln("Init store:", err)
|
|
}
|
|
|
|
if fileName != "-" {
|
|
file, fileErr := os.Open(fileName)
|
|
if fileErr != nil {
|
|
log.Fatalln("Open file:", fileErr)
|
|
}
|
|
|
|
inputPosts, err = parsePosts(file, date)
|
|
file.Close()
|
|
} else {
|
|
inputPosts, err = parsePosts(os.Stdin, date)
|
|
}
|
|
if err != nil {
|
|
log.Fatalln("Read inputPosts:", err)
|
|
}
|
|
|
|
if len(inputPosts) == 0 {
|
|
log.Fatalln("Input file contained no posts.")
|
|
}
|
|
|
|
l, err := logs.Add(inputPosts[0].Time, channel, "", "", "", false)
|
|
if err != nil {
|
|
log.Fatalln("Add log:", err)
|
|
}
|
|
|
|
log.Println("Added log", l.ID)
|
|
|
|
for _, inputPost := range inputPosts {
|
|
p, err := posts.Add(l, inputPost.Time, inputPost.Kind, inputPost.Nick, inputPost.Text)
|
|
if err != nil {
|
|
log.Fatalln("Add post:", err)
|
|
}
|
|
|
|
log.Println("Added post", p.ID)
|
|
}
|
|
}
|