forked from sbouchet/rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
98 lines (81 loc) · 2.32 KB
/
main.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"html"
"log"
"os"
"sort"
"strings"
"time"
"github.com/gorilla/feeds"
"github.com/mmcdole/gofeed"
)
const (
TITLE = "JBoss Tools Aggregated Feed"
LINK = "https://tools.jboss.org"
)
var (
urls = map[string]string{
"JBoss Tools": "https://tools.jboss.org/blog/news.atom",
"JBoss Blogs": "https://www.jboss.org/atom.xml",
"Red Hat Developer": "https://developers.redhat.com/blog/feed/",
"Quarkus": "https://quarkus.io/feed",
}
)
type Items []*gofeed.Item
func (a Items) Len() int { return len(a) }
func (a Items) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a Items) Less(i, j int) bool { return !(*a[i].PublishedParsed).Before(*a[j].PublishedParsed) }
func main() {
parentCtx := context.Background()
// fetch lates info from feeds
items := []*gofeed.Item{}
for name, url := range urls {
log.Printf("checking: %s @ %s\n", name, url)
ctx, cancel := context.WithTimeout(parentCtx, 15*time.Second)
fp := gofeed.NewParser()
feed, err := fp.ParseURLWithContext(url, ctx)
if err != nil {
log.Fatalf("failed to parse feed (%s): %s", url, err)
}
cancel()
// save items
for i := range feed.Items {
feed.Items[i].Title = name + ": " + feed.Items[i].Title
items = append(items, feed.Items[i])
}
}
// sort items
sort.Sort(Items(items)) // newest first
// create new feed
feed := &feeds.Feed{
Title: TITLE,
Link: &feeds.Link{Href: LINK},
Updated: time.Now(),
}
for i := range items {
feed.Items = append(feed.Items, &feeds.Item{
Title: items[i].Title,
Link: &feeds.Link{Href: items[i].Link},
Description: "", // skip description as it usually contains code that fails to parse correclty and not used in central
Created: *items[i].PublishedParsed,
Id: items[i].GUID,
})
}
data, err := feed.ToAtom()
if err != nil {
log.Fatalf("failed to generate atom feed file: %s", err)
}
// remove <?xml version="1.0" encoding="UTF-8"?>
data = strings.Replace(data, `<?xml version="1.0" encoding="UTF-8"?>`, "", 1)
f, err := os.OpenFile("rss.xml", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte(html.UnescapeString(data))); err != nil {
log.Fatalf("failed to write rss file: %s", err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}