aboutsummaryrefslogtreecommitdiff
path: root/internal/feed/parse.go
blob: 53ceb00e25eb594dd95d151543ebd1d914ae63a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package feed

import (
	ctxt "context"
	"fmt"
	"sync"
	"time"

	"github.com/mmcdole/gofeed"

	"github.com/Necoro/feed2imap-go/internal/config"
	"github.com/Necoro/feed2imap-go/internal/log"
)

func context() (ctxt.Context, ctxt.CancelFunc) {
	return ctxt.WithTimeout(ctxt.Background(), 60*time.Second)
}

func parseFeed(feed *config.Feed) error {
	ctx, cancel := context()
	defer cancel()
	fp := gofeed.NewParser()
	if _, err := fp.ParseURLWithContext(feed.Url, ctx); err != nil {
		return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err)
	}

	return nil
}

func handleFeed(feed *config.Feed, wg *sync.WaitGroup) {
	defer wg.Done()
	log.Printf("Fetching %s from %s", feed.Name, feed.Url)

	if err := parseFeed(feed); err != nil {
		log.Error(err)
	}
}

func Parse(feeds config.Feeds) {
	var wg sync.WaitGroup
	wg.Add(len(feeds))

	for _, feed := range feeds {
		go handleFeed(feed, &wg)
	}

	wg.Wait()
}
n> 2012-08-31Add remove synonyms.Jason A. Donenfeld1-2/+2 2012-08-31Use basename in usage.Jason A. Donenfeld1-2/+1 2012-08-19now using gpg_id as a varMatthew Ramirez1-2/+2 2012-08-07Forty five seconds.Jason A. Donenfeld1-1/+1 2012-08-06Deal with klipper and new lines.Jason A. Donenfeld1-3/+19 2012-08-06Update examples.Jason A. Donenfeld1-7/+7 2012-08-06Update readme.Jason A. Donenfeld1-11/+13 2012-08-06Be slicker and more like git.Jason A. Donenfeld1-114/+173