Skip to content

Commit

Permalink
Merge pull request #2 from piqoni/concurrent
Browse files Browse the repository at this point in the history
[WIP] Parsing Feeds Concurrently
  • Loading branch information
piqoni authored Jan 7, 2023
2 parents fd97389 + 23d7583 commit 51a76f7
Showing 1 changed file with 26 additions and 14 deletions.
40 changes: 26 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"time"

"github.com/mmcdole/gofeed"
Expand All @@ -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
Expand All @@ -33,7 +36,7 @@ type RSS struct {

func check(e error) {
if e != nil {
panic(e)
log.Fatal(e)
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 51a76f7

Please sign in to comment.