nlink/main.go

141 lines
2.6 KiB
Go

package main
import (
"flag"
"path/filepath"
"time"
"git.sr.ht/~thrrgilag/woodstock"
"git.wntrmute.dev/kyle/nlink/nomad"
"git.wntrmute.dev/kyle/goutils/config"
"git.wntrmute.dev/kyle/goutils/die"
log "git.wntrmute.dev/kyle/goutils/syslog"
)
var defaultRSS = "https://nomad.wntrmute.net/u/kyle.rss"
func defaultPath(file string) string {
return filepath.Join("/etc", "nlink", file)
}
func fetchItems(db *nomad.DB, markOnly bool) error {
rssFeed := nomad.NewURLSource(config.Get("local_rss_feed_url"))
items, err := nomad.FetchRSS(rssFeed)
if err != nil {
return nil
}
items, err = db.Filter(nil, items)
if err != nil {
return err
}
if items == nil {
return nil
}
if markOnly {
tx, err := db.Begin()
if err != nil {
return err
}
for _, item := range items {
if err = db.Mark(tx, item); err != nil {
return err
}
}
tx.Commit()
if err := restoreDatabase(config.Get("local_database")); err != nil {
return err
}
return nil
}
client := woodstock.NewClient(config.Get("pnut_id"), config.Get("pnut_secret"))
client.SetAccessToken(config.Get(""))
for _, item := range items {
p := nomad.NewPost(item)
err = p.Fetch()
die.If(err)
err = p.Post(client)
die.If(err)
err = db.Mark(nil, item)
}
if err := restoreDatabase(config.Get("local_database")); err != nil {
log.Err(err)
}
return nil
}
const interval = 5 * time.Minute
func main() {
var (
configFile string
interval time.Duration
initDB bool
level string
markOnly bool
)
flag.StringVar(&configFile, "f", defaultPath("nlink.conf"), "`path` to config file")
flag.BoolVar(&initDB, "i", false, "initialize a new DB")
flag.StringVar(&level, "l", "INFO", "log level")
flag.BoolVar(&markOnly, "m", false, "only mark posts as having been posted")
flag.Parse()
logOpts := &log.Options{
Level: level,
Tag: "nlink",
Facility: "daemon",
WriteSyslog: false,
}
if err := log.Setup(logOpts); err != nil {
die.If(err)
}
if err := restoreDatabase(config.Get("local_database")); err != nil {
log.Fatal(err)
}
log.Debugf("loading config file %s", configFile)
if err := config.LoadFile(configFile); err != nil {
log.Fatal(err)
}
if initDB {
db := nomad.NewDB(config.Get("local_database"))
err := db.Create()
die.If(err)
db.Close()
}
db := nomad.NewDB(config.Get("local_database"))
err := db.Connect()
die.If(err)
defer db.Close()
if markOnly {
if err = fetchItems(db, markOnly); err != nil {
log.Fatal(err)
}
return
}
for {
err = fetchItems(db, markOnly)
log.Info(err)
time.Sleep(interval)
}
}