Compare commits

...

4 Commits

Author SHA1 Message Date
Kyle Isom 6f29ac4b03 Fixing links for blog entries. 2023-08-29 14:49:48 -07:00
Kyle Isom 5e0a725deb Update link posts to support !nomad entries. 2023-08-29 14:14:40 -07:00
Kyle Isom baf36dbaf8 even yet more logs 2023-08-29 13:23:38 -07:00
Kyle Isom 64190a500d yet more logs 2023-08-29 13:15:50 -07:00
3 changed files with 74 additions and 15 deletions

27
main.go
View File

@ -98,6 +98,15 @@ func splitFeeds() []string {
return feeds return feeds
} }
func fetchLoop(feeds []string, db *nomad.DB) {
for _, feed := range feeds {
log.Infof("fetching items for feed %s", feed)
if err := fetchItems(db, feed, false); err != nil {
log.Errln(err)
}
}
}
func main() { func main() {
var ( var (
configFile string configFile string
@ -105,12 +114,14 @@ func main() {
initDB bool initDB bool
level string level string
markOnly bool markOnly bool
singleRun bool
) )
flag.StringVar(&configFile, "f", defaultPath("nlink.conf"), "`path` to config file") flag.StringVar(&configFile, "f", defaultPath("nlink.conf"), "`path` to config file")
flag.BoolVar(&initDB, "i", false, "initialize a new DB") flag.BoolVar(&initDB, "i", false, "initialize a new DB")
flag.StringVar(&level, "l", "DEBUG", "log level") flag.StringVar(&level, "l", "DEBUG", "log level")
flag.BoolVar(&markOnly, "m", false, "only mark posts as having been posted") flag.BoolVar(&markOnly, "m", false, "only mark posts as having been posted")
flag.BoolVar(&singleRun, "s", false, "only go through a single run")
flag.DurationVar(&interval, "t", defaultInterval, "interval between fetching posts") flag.DurationVar(&interval, "t", defaultInterval, "interval between fetching posts")
flag.Parse() flag.Parse()
@ -121,6 +132,8 @@ func main() {
die.If(err) die.If(err)
} }
log.Infoln("nlink is starting")
log.Debugf("loading config file %s\n", configFile) log.Debugf("loading config file %s\n", configFile)
if err := config.LoadFile(configFile); err != nil { if err := config.LoadFile(configFile); err != nil {
log.Fatal(err) log.Fatal(err)
@ -153,6 +166,7 @@ func main() {
defer db.Close() defer db.Close()
feeds := splitFeeds() feeds := splitFeeds()
if markOnly { if markOnly {
log.Info("marking database") log.Info("marking database")
for _, feed := range feeds { for _, feed := range feeds {
@ -164,15 +178,16 @@ func main() {
return return
} }
if singleRun {
log.Debugf("running a single loop")
fetchLoop(feeds, db)
return
}
log.Debugf("will look for new items every %s\n", interval) log.Debugf("will look for new items every %s\n", interval)
for { for {
log.Infoln("running fetch loop") log.Infoln("running fetch loop")
for _, feed := range feeds { fetchLoop(feeds, db)
log.Infof("fetching items for feed %s", feed)
if err := fetchItems(db, feed, markOnly); err != nil {
log.Errln(err)
}
}
time.Sleep(interval) time.Sleep(interval)
} }

View File

@ -4,14 +4,26 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"net/url"
"path/filepath" "path/filepath"
"git.sr.ht/~thrrgilag/woodstock" "git.sr.ht/~thrrgilag/woodstock"
"git.wntrmute.dev/kyle/goutils/log"
"github.com/anaskhan96/soup" "github.com/anaskhan96/soup"
) )
func nomadLink(url Source) string { func nomadLink(item Item) string {
return fmt.Sprintf("[nomad.wntrmute.net](%s)", url.ID()) if item.Title != "" {
return fmt.Sprintf("[%s](%s)", item.Title, item.URL.ID())
}
u, err := url.Parse(item.URL.ID())
if err != nil {
log.Errf("nomadLink: %s", err)
return fmt.Sprintf("[%s](%s)", item.URL.ID(), item.URL.ID())
}
return fmt.Sprintf("[%s](%s)", u.Host, item.URL.ID())
} }
type sel struct { type sel struct {
@ -22,6 +34,23 @@ func selector(selectors ...string) sel {
return sel{selectors: selectors} return sel{selectors: selectors}
} }
type imageFinder func(soup.Root) (string, bool)
var imageFinders = []imageFinder{
func(root soup.Root) (string, bool) {
return find(
root, "src",
selector("div", "class", "entry-image"),
selector("img"))
},
func(root soup.Root) (string, bool) {
return find(
root, "src",
selector("div", "class", "gh-inner"),
selector("img"))
},
}
func find(root soup.Root, attr string, selectors ...sel) (string, bool) { func find(root soup.Root, attr string, selectors ...sel) (string, bool) {
result := root result := root
for _, selector := range selectors { for _, selector := range selectors {
@ -41,16 +70,18 @@ func find(root soup.Root, attr string, selectors ...sel) (string, bool) {
} }
type Post struct { type Post struct {
Title string
Image Source Image Source
Body string Body string
URL Source URL Source
} }
func NewPost(item Item) Post { func NewPost(item Item) Post {
return Post{ post := Post{
Body: nomadLink(item.URL), Body: nomadLink(item),
URL: item.URL, URL: item.URL,
} }
return post
} }
func (p *Post) Fetch() error { func (p *Post) Fetch() error {
@ -65,10 +96,15 @@ func (p *Post) Fetch() error {
p.Body = body + " " + p.Body p.Body = body + " " + p.Body
} }
imageURL, hasImageURL := find( var imageURL string
root, "src", var hasImageURL bool
selector("div", "class", "entry-image"),
selector("img")) for _, finder := range imageFinders {
imageURL, hasImageURL = finder(root)
if hasImageURL {
break
}
}
if hasImageURL { if hasImageURL {
p.Image = NewURLSource(imageURL) p.Image = NewURLSource(imageURL)

View File

@ -30,6 +30,7 @@ func connectMinio() (err error) {
} }
func restoreDatabase(path string) error { func restoreDatabase(path string) error {
log.Debugln("restoring database from minio")
if minioClient == nil { if minioClient == nil {
if err := connectMinio(); err != nil { if err := connectMinio(); err != nil {
return err return err
@ -58,7 +59,12 @@ func restoreDatabase(path string) error {
defer dbFile.Close() defer dbFile.Close()
_, err = io.Copy(dbFile, obj) _, err = io.Copy(dbFile, obj)
return err if err != nil {
return err
}
log.Debugln("database restored from minio")
return nil
} }
func saveDatabase(path string) error { func saveDatabase(path string) error {
@ -91,5 +97,7 @@ func saveDatabase(path string) error {
if err != nil { if err != nil {
return err return err
} }
log.Debugln("database saved to minio")
return nil return nil
} }