~skuzzymiglet/feef

ref: bf4c2b611efd1e36f466fea746d851dc30e3a7a0 feef/notify.go -rw-r--r-- 1.4 KiB
bf4c2b61skuzzymiglet remove check for greater number of items - feeds that return a constant number of items will now not fall through 1 year, 4 months ago
                                                                                
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
60
61
62
63
64
65
66
67
package main

import (
	"context"
	"sync"
	"time"

	log "github.com/sirupsen/logrus"
)

func findNewItems(oldFeed, newFeed LinkedFeed) []LinkedFeedItem {
	var buf []LinkedFeedItem
	tmp := make(map[string]struct{}, len(newFeed.Items))

	for _, i := range oldFeed.Items {
		tmp[i.GUID] = struct{}{}
	}
	for _, i := range newFeed.Items { // For each new...
		if _, found := tmp[i.GUID]; !found {
			buf = append(buf, i)
		}
	}
	return buf
}

func Notify(ctx context.Context, n NotifyParam, out chan<- LinkedFeedItem, errChan chan error) {
	var wg sync.WaitGroup
	for _, u := range n.urls {
		// When only showing new items, fetch the initial feed
		// Othwerwise start with nothing
		initial := true
		var last LinkedFeed
		wg.Add(1)
		go func(u string) {
			defer wg.Done()
			// gofeed.Parser is not thread-safe
			for {
				select {
				case <-ctx.Done():
					return
				default:
					lf, err := n.Fetcher.Fetch(ctx, u)
					if err != nil {
						errChan <- err
						return
					}
					if lf.Feed.FeedLink != u {
						log.Debugf("feed request url and self-reference url mismatch: requested %s, got %s", u, lf.Feed.FeedLink)
					}
					if initial && n.mode == newItems {
						// immediately move on in "newItems" mode
						initial = false
					} else {
						newItems := findNewItems(last, lf)
						for _, item := range newItems {
							out <- item
						}
						time.Sleep(n.poll)
					}
					last = lf

				}
			}
		}(u)
	}
	wg.Wait()
}