diff --git a/main.go b/main.go index 7bf8746..000a56c 100644 --- a/main.go +++ b/main.go @@ -9,6 +9,7 @@ import ( "path/filepath" "strconv" "strings" + "sync" "time" "github.com/mmcdole/gofeed" @@ -24,6 +25,8 @@ var lat, lon float64 var instapaper bool var myMap []RSS var db *sql.DB +var feeds chan *gofeed.Feed +var wg sync.WaitGroup type RSS struct { url string @@ -33,7 +36,7 @@ type RSS struct { func check(e error) { if e != nil { - panic(e) + log.Fatal(e) } } @@ -96,37 +99,46 @@ func writeToMarkdown(body string) { } } -func parseFeedURL(fp *gofeed.Parser, url string) *gofeed.Feed { +func parseFeedURL(fp *gofeed.Parser, url string) { feed, err := fp.ParseURL(url) - if err != nil { - return nil + if err == nil { + feeds <- feed + } else { + log.Println("Cound not parse: " + url) + log.Println(err) } - return feed + wg.Done() } func main() { bootstrapConfig() - // Start writing to markdown // Display weather if lat and lon are set if lat != 0 && lon != 0 { writeToMarkdown(getWeather(lat, lon)) } + feeds = make(chan *gofeed.Feed, len(myMap)) + wg.Add(len(myMap)) + // Set up a slice to store the parsed feeds + var parsedFeeds []*gofeed.Feed + fp := gofeed.NewParser() - for _, rss := range myMap { - if rss.disabled { - continue + for _, feed := range myMap { + go parseFeedURL(fp, feed.url) + } + go func() { + for feed := range feeds { + parsedFeeds = append(parsedFeeds, feed) } - feed := parseFeedURL(fp, rss.url) + }() + wg.Wait() - if feed == nil { - continue - } + for _, feed := range parsedFeeds { items := "" for index, item := range feed.Items { - if index == rss.limit { + if index == 10 { break } var url string