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
49
50
51
52
53
54
55
56
57
58
59
|
package feed
import (
ctxt "context"
"fmt"
"sync"
"time"
"github.com/mmcdole/gofeed"
"github.com/Necoro/feed2imap-go/internal/log"
)
func context() (ctxt.Context, ctxt.CancelFunc) {
return ctxt.WithTimeout(ctxt.Background(), 60*time.Second)
}
func parseFeed(feed *Feed) error {
ctx, cancel := context()
defer cancel()
fp := gofeed.NewParser()
parsedFeed, err := fp.ParseURLWithContext(feed.Url, ctx)
if err != nil {
return fmt.Errorf("while fetching %s from %s: %w", feed.Name, feed.Url, err)
}
feed.feed = parsedFeed
feed.items = make([]feeditem, len(parsedFeed.Items))
for idx, item := range parsedFeed.Items {
feed.items[idx] = feeditem{parsedFeed, item}
}
return nil
}
func handleFeed(feed *Feed, group *sync.WaitGroup) {
defer group.Done()
log.Printf("Fetching %s from %s", feed.Name, feed.Url)
err := parseFeed(feed)
if err != nil {
log.Error(err)
}
}
func (feeds Feeds) Parse() int {
feeds.ForeachGo(handleFeed)
ctr := 0
for _, feed := range feeds.feeds {
success := feed.Success()
feed.cached.Checked(!success)
if success {
ctr++
}
}
return ctr
}
|