diff --git a/main.go b/main.go index 26dd84b..4b22617 100644 --- a/main.go +++ b/main.go @@ -98,6 +98,15 @@ func splitFeeds() []string { 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() { var ( configFile string @@ -105,12 +114,14 @@ func main() { initDB bool level string markOnly bool + singleRun 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", "DEBUG", "log level") 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.Parse() @@ -167,15 +178,16 @@ func main() { return } + if singleRun { + log.Debugf("running a single loop") + fetchLoop(feeds, db) + return + } + log.Debugf("will look for new items every %s\n", interval) for { log.Infoln("running fetch loop") - for _, feed := range feeds { - log.Infof("fetching items for feed %s", feed) - if err := fetchItems(db, feed, markOnly); err != nil { - log.Errln(err) - } - } + fetchLoop(feeds, db) time.Sleep(interval) } diff --git a/nomad/post.go b/nomad/post.go index 26267ce..2e826c9 100644 --- a/nomad/post.go +++ b/nomad/post.go @@ -12,14 +12,18 @@ import ( "github.com/anaskhan96/soup" ) -func nomadLink(src Source) string { - u, err := url.Parse(src.ID()) - if err != nil { - log.Errf("nomadLink: %s", err) - return fmt.Sprintf("[%s](%s)", src.ID(), src.ID()) +func nomadLink(item Item) string { + if item.Title != "" { + return fmt.Sprintf("[%s](%s)", item.Title, item.URL.ID()) } - return fmt.Sprintf("[%s](%s)", u.Host, src.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 { @@ -66,16 +70,18 @@ func find(root soup.Root, attr string, selectors ...sel) (string, bool) { } type Post struct { + Title string Image Source Body string URL Source } func NewPost(item Item) Post { - return Post{ - Body: nomadLink(item.URL), + post := Post{ + Body: nomadLink(item), URL: item.URL, } + return post } func (p *Post) Fetch() error {