52 lines
817 B
Go
52 lines
817 B
Go
|
package nomad
|
||
|
|
||
|
import (
|
||
|
"sort"
|
||
|
|
||
|
"github.com/mmcdole/gofeed"
|
||
|
)
|
||
|
|
||
|
type Item struct {
|
||
|
URL Source
|
||
|
Title string
|
||
|
PubDate int64
|
||
|
}
|
||
|
|
||
|
type itemList []Item
|
||
|
|
||
|
func (l itemList) Len() int {
|
||
|
return len(l)
|
||
|
}
|
||
|
|
||
|
func (l itemList) Less(i, j int) bool {
|
||
|
return l[i].PubDate < l[j].PubDate
|
||
|
}
|
||
|
|
||
|
func (l itemList) Swap(i, j int) {
|
||
|
l[i], l[j] = l[j], l[i]
|
||
|
}
|
||
|
|
||
|
func FetchRSS(feedSource Source) (items []Item, err error) {
|
||
|
rd, err := feedSource.Fetch()
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
feedParser := gofeed.NewParser()
|
||
|
feed, err := feedParser.ParseString(rd)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
for _, item := range feed.Items {
|
||
|
items = append(items, Item{
|
||
|
URL: NewSoupSource(item.Link),
|
||
|
Title: item.Title,
|
||
|
PubDate: item.PublishedParsed.Unix(),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
sort.Sort(itemList(items))
|
||
|
return items, nil
|
||
|
}
|