-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge.go
80 lines (64 loc) · 1.63 KB
/
merge.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"fmt"
"sort"
"strings"
"time"
"github.com/gorilla/feeds"
"github.com/mmcdole/gofeed"
)
// Merge takes a list of raw feed strings, parses and then merges them
func Merge(rawFeeds []string) feeds.Feed {
fp := gofeed.NewParser()
var items itemList
var feedList []string
for _, feed := range rawFeeds {
feed, err := fp.ParseString(feed)
if err == nil {
feedList = append(feedList, feed.Title)
for _, item := range feed.Items {
published := item.PublishedParsed
updated := item.UpdatedParsed
if published == nil && updated == nil {
continue
}
time := published
if time == nil {
time = updated
}
items = append(items, convertItem(item, *time, feed))
}
}
}
sort.Sort(sort.Reverse(items))
feed := feeds.Feed{
Title: fmt.Sprintf("RSSMerge Feed (%v feeds)", len(feedList)),
Link: &feeds.Link{Href: "PLACEHOLDER"},
Description: strings.Join(feedList, ", "),
Author: &feeds.Author{Name: "RSSMerge", Email: "[email protected]"},
Created: time.Now(),
Items: items,
}
return feed
}
func convertItem(item *gofeed.Item, created time.Time, feed *gofeed.Feed) *feeds.Item {
if item.Link == "" {
item.Link = feed.Link
}
return &feeds.Item{
Title: item.Title,
Link: &feeds.Link{Href: item.Link},
Created: created,
Description: feed.Title,
}
}
type itemList []*feeds.Item
func (slice itemList) Len() int {
return len(slice)
}
func (slice itemList) Less(i, j int) bool {
return slice[i].Created.Before(slice[j].Created)
}
func (slice itemList) Swap(i, j int) {
slice[i], slice[j] = slice[j], slice[i]
}