-
Notifications
You must be signed in to change notification settings - Fork 0
/
tributary.go
196 lines (160 loc) · 4.28 KB
/
tributary.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Package tributary fetches a single feed.
package tributary
import (
"log"
"net/http"
"net/url"
"time"
"golang.org/x/net/html/charset"
"hawx.me/code/riviera/feed"
"hawx.me/code/riviera/feed/common"
"hawx.me/code/riviera/river/events"
"hawx.me/code/riviera/river/mapping"
"hawx.me/code/riviera/river/riverjs"
)
// A Tributary reports changes in a feed to the channels it is given.
type Tributary interface {
// Name returns a unique name to refer to the tributary.
Name() string
// Feeds sets a channel that is used to send out the latest updates to the
// tributary.
Feeds(chan<- riverjs.Feed)
// Events sets a channel that is used to send out events for the tributary.
Events(chan<- events.Event)
// Start polling for updates.
Start()
// Stop polling.
Stop()
}
type tributary struct {
uri *url.URL
feed *feed.Feed
client *http.Client
mapping mapping.Mapping
feeds chan<- riverjs.Feed
events chan<- events.Event
quit chan struct{}
}
// New returns a tributary watching the feed at the URI given.
func New(store feed.Database, uri string, cacheTimeout time.Duration, mapping mapping.Mapping) Tributary {
parsedURI, _ := url.Parse(uri)
p := &tributary{
uri: parsedURI,
mapping: mapping,
quit: make(chan struct{}),
}
p.feed = feed.New(cacheTimeout, p.itemHandler, store)
p.client = &http.Client{Timeout: time.Minute, Transport: &statusTransport{http.DefaultTransport.(*http.Transport), p}}
return p
}
func (t *tributary) Name() string {
return t.uri.String()
}
func (t *tributary) Feeds(feeds chan<- riverjs.Feed) {
t.feeds = feeds
}
func (t *tributary) Events(events chan<- events.Event) {
t.events = events
}
func (t *tributary) Start() {
go func() {
log.Printf("started fetching %s\n", t.uri)
t.fetch()
loop:
for {
select {
case <-time.After(t.feed.DurationTillUpdate()):
log.Printf("fetching %s\n", t.uri)
t.fetch()
case <-t.quit:
break loop
}
}
log.Printf("stopped fetching %s\n", t.uri)
close(t.quit)
}()
}
func (t *tributary) Stop() {
t.quit <- struct{}{}
<-t.quit
}
type statusTransport struct {
*http.Transport
trib *tributary
}
// RoundTrip performs a RoundTrip using the underlying Transport, but then
// checks if the status returned was a 301 MovedPermanently. If so it modifies
// the underlying uri which will then be saved to the subscriptions next time it
// is fetched.
func (t *statusTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
resp, err = t.Transport.RoundTrip(req)
if err != nil {
return
}
if resp.StatusCode == http.StatusMovedPermanently {
newLoc := resp.Header.Get("Location")
newURL, errp := url.Parse(newLoc)
if errp != nil {
log.Printf("%s moved to %s, error: %s\n", t.trib.uri, newLoc, errp)
return
}
log.Printf("%s moved to %s\n", t.trib.uri, newLoc)
t.trib.uri = newURL
}
return
}
// fetch retrieves the feed for the tributary.
func (t *tributary) fetch() {
code, err := t.feed.Fetch(t.uri.String(), t.client, charset.NewReaderLabel)
t.events <- events.Event{
At: time.Now().UTC(),
URI: t.Name(),
Code: code,
}
if err != nil {
log.Printf("error fetching %s: %d %s\n", t.uri, code, err)
return
}
}
func maybeResolvedLink(root *url.URL, other string) string {
parsed, err := root.Parse(other)
if err == nil {
return parsed.String()
}
return other
}
func (t *tributary) itemHandler(feed *feed.Feed, ch *common.Channel, newitems []*common.Item) {
items := []riverjs.Item{}
for _, item := range newitems {
converted := t.mapping(item)
if converted != nil {
converted.Link = maybeResolvedLink(t.uri, converted.Link)
converted.PermaLink = maybeResolvedLink(t.uri, converted.PermaLink)
items = append(items, *converted)
}
}
log.Printf("%d new item(s) in %s\n", len(items), t.uri)
if len(items) == 0 {
return
}
feedURL := t.uri.String()
websiteURL := ""
for _, link := range ch.Links {
if feedURL != "" && websiteURL != "" {
break
}
if link.Rel == "self" {
feedURL = maybeResolvedLink(t.uri, link.Href)
} else {
websiteURL = maybeResolvedLink(t.uri, link.Href)
}
}
t.feeds <- riverjs.Feed{
FeedURL: feedURL,
WebsiteURL: websiteURL,
FeedTitle: ch.Title,
FeedDescription: ch.Description,
WhenLastUpdate: riverjs.Time(time.Now()),
Items: items,
}
}