2023-05-06 07:14:37 +00:00
|
|
|
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 {
|
2023-05-06 07:32:00 +00:00
|
|
|
return filepath.Join("/etc", "nlink", file)
|
2023-05-06 07:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
2023-05-06 07:32:00 +00:00
|
|
|
|
|
|
|
if err := restoreDatabase(config.Get("local_database")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-05-06 07:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-05-06 07:32:00 +00:00
|
|
|
if err := restoreDatabase(config.Get("local_database")); err != nil {
|
|
|
|
log.Err(err)
|
|
|
|
}
|
|
|
|
|
2023-05-06 07:14:37 +00:00
|
|
|
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",
|
2023-05-06 07:50:51 +00:00
|
|
|
WriteSyslog: false,
|
2023-05-06 07:14:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := log.Setup(logOpts); err != nil {
|
|
|
|
die.If(err)
|
|
|
|
}
|
|
|
|
|
2023-05-06 07:32:00 +00:00
|
|
|
if err := restoreDatabase(config.Get("local_database")); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2023-05-06 07:14:37 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|