128 lines
2.3 KiB
Go
128 lines
2.3 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("/perm", "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()
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
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)
|
||
|
}
|
||
|
|
||
|
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: true,
|
||
|
}
|
||
|
|
||
|
if err := log.Setup(logOpts); err != nil {
|
||
|
die.If(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)
|
||
|
}
|
||
|
}
|