-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaggregator.go
61 lines (52 loc) · 1.2 KB
/
aggregator.go
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
/*
* Copyright (C) 2018 Aurélien Chabot <[email protected]>
*
* SPDX-License-Identifier: MIT
*/
package main
import (
"log"
)
import "github.com/mmcdole/gofeed"
// Aggregator is a RSS aggregator object
type Aggregator struct {
url string
feed *gofeed.Feed
cache *Cache
}
// NewAggregator create a new Aggregator object
func NewAggregator(url string, cache *Cache) *Aggregator {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(url)
if err != nil {
log.Fatal(err)
}
return &Aggregator{url, feed, cache}
}
// GetNewItems return all the new items in the RSS feed
func (a *Aggregator) GetNewItems() []*gofeed.Item {
guid, err := a.cache.Get(a.url)
if err != nil {
return a.feed.Items[:]
}
for i, item := range a.feed.Items {
if item.GUID == guid {
return a.feed.Items[:i]
}
}
return a.feed.Items[:]
}
// GetNewTorrentURL return the url of all the new items in the RSS feed
func (a *Aggregator) GetNewTorrentURL() []string {
urls := make([]string, 0)
items := a.GetNewItems()
log.Printf("%d new items\n", len(items))
for _, item := range items {
log.Println(item.Title)
urls = append(urls, item.Link)
}
if len(items) > 0 {
a.cache.Set(a.url, items[0].GUID)
}
return urls
}