This repository has been archived by the owner on Mar 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
144 lines (119 loc) · 3.66 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"flag"
"fmt"
"log"
"net/http"
"regexp"
"sort"
"time"
"github.com/gorilla/feeds"
rss "github.com/mattn/go-pkg-rss"
)
var podcastEpisodePrefix = regexp.MustCompile(`^\d : `)
var sourceFeeds = []sourceFeed{
{uri: "https://robots.thoughtbot.com/summaries.xml", name: "Giant Robots blog"},
{uri: "https://feeds.simplecast.com/KARThxOK", name: "Giant Robots podcast"},
{uri: "https://feeds.simplecast.com/ky3kewHN", name: "The Bike Shed podcast"},
{uri: "https://feeds.simplecast.com/ZBfsoMJW", name: "Tentative podcast"},
{uri: "https://thoughtbot.com/upcase/the-weekly-iteration.rss", name: "The Weekly Iteration videos"},
{uri: "https://hub.thoughtbot.com/releases.atom", name: "Open source software releases"},
}
func main() {
port := flag.String("port", "8080", "HTTP Port to listen on")
flag.Parse()
http.Handle("/", rssHandler(sourceFeeds))
log.Fatal(http.ListenAndServe(":" *port, nil))
}
func rssHandler(sourceFeeds []sourceFeed) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
if req.Header.Get("X-Forwarded-Proto") == "http" {
destination := *req.URL
destination.Host = req.Host
destination.Scheme = "https"
http.Redirect(w, req, destination.String(), http.StatusFound)
return
}
master := &feeds.Feed{
Title: "thoughtbot",
Link: &feeds.Link{Href: "https://rss.thoughtbot.com"},
Description: "All the thoughts fit to bot.",
Author: &feeds.Author{Name: "thoughtbot", Email: "[email protected]"},
Created: time.Now(),
}
for _, feed := range sourceFeeds {
fetch(feed, master)
}
sort.Sort(byCreated(master.Items))
result, err := master.ToAtom()
if err != nil {
log.Printf("error generating feed: %v", err)
http.Error(w, "error generating feed", http.StatusInternalServerError)
return
}
_, err = fmt.Fprintln(w, result)
if err != nil {
log.Printf("error printing feed: %v", err)
http.Error(w, "error printing feed", http.StatusInternalServerError)
return
}
})
}
func fetch(feed sourceFeed, master *feeds.Feed) {
fetcher := rss.New(5, true, chanHandler, makeHandler(master, feed.name))
client := &http.Client{
Timeout: time.Second,
}
err := fetcher.FetchClient(feed.uri, client, nil)
if err != nil {
log.Printf("error fetching feed: %v", err)
}
}
func chanHandler(feed *rss.Feed, newchannels []*rss.Channel) {
// no need to do anything...
}
func makeHandler(master *feeds.Feed, sourceName string) rss.ItemHandlerFunc {
return func(feed *rss.Feed, ch *rss.Channel, items []*rss.Item) {
for i := 0; i < len(items); i {
published, err := items[i].ParsedPubDate()
if err != nil {
log.Printf("error parsing publication date: %v", err)
continue
}
weekAgo := time.Now().AddDate(0, 0, -7)
if published.After(weekAgo) {
item := &feeds.Item{
Title: stripPodcastEpisodePrefix(items[i].Title),
Link: &feeds.Link{Href: items[i].Links[0].Href},
Description: getDescription(items[i]),
Author: &feeds.Author{Name: sourceName},
Created: published,
}
master.Add(item)
}
}
}
}
type byCreated []*feeds.Item
func (s byCreated) Len() int {
return len(s)
}
func (s byCreated) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s byCreated) Less(i, j int) bool {
return s[j].Created.Before(s[i].Created)
}
func stripPodcastEpisodePrefix(s string) string {
return podcastEpisodePrefix.ReplaceAllString(s, "")
}
func getDescription(item *rss.Item) string {
if ext, ok := item.Extensions["http://www.itunes.com/dtds/podcast-1.0.dtd"]; ok {
return ext["subtitle"][0].Value
}
return item.Description
}
type sourceFeed struct {
uri string
name string
}