/
main.go
397 lines (351 loc) · 8.9 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
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
package main
import (
"encoding/json"
"encoding/xml"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"path"
"strings"
"sync"
"time"
"code.google.com/p/go-charset/charset"
_ "code.google.com/p/go-charset/data"
"code.google.com/p/go-uuid/uuid"
"github.com/kennygrant/sanitize"
"gopkg.in/yaml.v2"
)
const (
utcTimestampFmt = "Mon, 02 Jan 2006 15:04:05" + " GMT"
localTimestampFmt = "Mon, 02 Jan 2006 15:04:05 MST"
maxFeedItems = 5
characterCount = 280
updatedFeedCount = 300
)
var (
logger = log.New(os.Stdout, "", log.LstdFlags|log.Lmicroseconds)
history = make(map[string]bool)
activeFeeds = make(map[string]*FeedFetcher)
)
type River struct {
UpdatedFeeds struct {
UpdatedFeed []Feed `json:"updatedFeed"`
} `json:"updatedFeeds"`
Metadata map[string]string `json:"metadata"`
}
type FetchResult struct {
Content *xml.Decoder
URL string
}
type FeedFetcher struct {
Poll time.Duration
Ticker *time.Ticker
Delay <-chan time.Time
URL string
}
func (self *FeedFetcher) Run(results chan FetchResult) {
for {
select {
case <-self.Delay:
self.Ticker = time.NewTicker(self.Poll)
fetchFeed(self.URL, results)
case <-self.Ticker.C:
fetchFeed(self.URL, results)
}
}
}
func fetchFeed(url string, results chan FetchResult) {
resp, err := http.Get(url)
if err != nil {
logger.Printf("http.Get error: %v", err)
return
}
if resp.StatusCode == 404 {
logger.Printf("%q returned 404", url)
return
}
decoder := xml.NewDecoder(resp.Body)
decoder.CharsetReader = charset.NewReader // Needed for non-UTF-8 encoded feeds.
results <- FetchResult{decoder, url}
}
func clean(s string) string {
s = sanitize.HTML(s)
s = strings.Trim(s, " ")
const ellipsis = "\u2026"
if len(s) > characterCount {
i := characterCount - 1
c := string(s[i])
for c != " " {
i--
c = string(s[i])
}
switch string(s[i-1]) {
case ".", ",":
return s[:i-1] + ellipsis
default:
return s[:i] + ellipsis
}
}
return s
}
func parseFeed(obj FetchResult) Feed {
var feed = Feed{
Title: "Untitled",
Website: "http://example.com/",
URL: obj.URL,
LastUpdate: time.Now().UTC().Format(utcTimestampFmt),
Items: []FeedItem{},
}
for {
t, _ := obj.Content.Token()
if t == nil {
break
}
if e, ok := t.(xml.StartElement); ok {
switch e.Name.Local {
case "rss":
var rss = RSS{}
obj.Content.DecodeElement(&rss, &e)
feed.Title = rss.Title
feed.Website = rss.Website()
feed.Description = rss.Description
for idx, item := range rss.Items {
if idx > maxFeedItems-1 {
break
}
// Check if we've seen this item before
if found := history[item.Guid()]; found {
break
}
history[item.Guid()] = true
title, body := item.River()
feed.Items = append(feed.Items, FeedItem{
Title: clean(title),
Body: clean(body),
Link: item.Link,
Permalink: item.Permalink,
PubDate: item.Timestamp(),
Id: uuid.New(),
})
}
case "feed":
var atom = Atom{}
obj.Content.DecodeElement(&atom, &e)
feed.Title = atom.Title
feed.Website = atom.Website()
feed.Description = atom.Description
for idx, item := range atom.Items {
if idx > maxFeedItems-1 {
break
}
if found := history[item.Guid()]; found {
break
}
history[item.Guid()] = true
title, body := item.River()
feed.Items = append(feed.Items, FeedItem{
Title: clean(title),
Body: clean(body),
Link: item.WebLink(),
Permalink: item.Id,
PubDate: item.Timestamp(),
Id: uuid.New(),
})
}
}
}
}
if feed.Title == "" {
feed.Title = "Untitled"
}
return feed
}
func buildRiver(c chan FetchResult, output string) {
river := River{
Metadata: map[string]string{
"docs": "http://riverjs.org",
"version": "3",
"secs": "",
},
}
for obj := range c {
feed := parseFeed(obj)
if len(feed.Items) == 0 {
continue
}
logger.Printf("Updating: %v with %v new items", feed.Title, len(feed.Items))
writer, _ := os.Create(output)
encoder := json.NewEncoder(writer)
// Update the timestamps
river.Metadata["whenGMT"] = time.Now().UTC().Format(utcTimestampFmt)
river.Metadata["whenLocal"] = time.Now().Format(localTimestampFmt)
river.UpdatedFeeds.UpdatedFeed = append([]Feed{feed}, river.UpdatedFeeds.UpdatedFeed...)
if len(river.UpdatedFeeds.UpdatedFeed) > updatedFeedCount {
river.UpdatedFeeds.UpdatedFeed = river.UpdatedFeeds.UpdatedFeed[:updatedFeedCount]
}
writer.Write([]byte("onGetRiverStream("))
encoder.Encode(river)
writer.Write([]byte(")"))
writer.Sync()
}
}
func loadRemoteFeedList(input string) []string {
feeds := []string{}
switch path.Ext(input) {
case ".opml":
opml := OPML{}
resp, err := http.Get(input)
if err != nil {
logger.Fatal("couldn't load feed list: %v", err)
}
decoder := xml.NewDecoder(resp.Body)
decoder.CharsetReader = charset.NewReader
decoder.Decode(&opml)
for _, outline := range opml.Body.Outlines {
if outline.XmlUrl != "" {
feeds = append(feeds, outline.XmlUrl)
}
}
case ".txt", ".yaml":
resp, err := http.Get(input)
if err != nil {
logger.Fatal("couldn't load feed list: %v", err)
}
data, _ := ioutil.ReadAll(resp.Body)
yaml.Unmarshal(data, &feeds)
}
return feeds
}
func loadLocalFeedList(input string) []string {
feeds := []string{}
switch path.Ext(input) {
case ".opml":
opml := OPML{}
fp, err := os.Open(input)
if err != nil {
logger.Fatal("couldn't load feed list: %v", err)
}
decoder := xml.NewDecoder(fp)
decoder.CharsetReader = charset.NewReader
decoder.Decode(&opml)
for _, outline := range opml.Body.Outlines {
if outline.XmlUrl != "" {
feeds = append(feeds, outline.XmlUrl)
}
}
case ".txt", ".yaml":
data, err := ioutil.ReadFile(input)
if err != nil {
logger.Fatal("couldn't load feed list: %v", err)
}
yaml.Unmarshal(data, &feeds)
}
return feeds
}
func pollFeedList(input string, feedPoll, listPoll time.Duration, results chan FetchResult) {
ticker := time.Tick(listPoll)
for {
select {
case <-ticker:
logger.Printf("Polling %q", input)
var feeds []string
if startsWith(input, "http://", "https://") {
feeds = loadRemoteFeedList(input)
} else {
feeds = loadLocalFeedList(input)
}
// Build a map of the just fetched feeds.
feedsMap := make(map[string]bool)
for _, url := range feeds {
feedsMap[url] = true
}
// First, check if any feeds have been removed
for url := range activeFeeds {
if !feedsMap[url] {
logger.Printf("Removing feed: %s", url)
fetcher := activeFeeds[url]
fetcher.Ticker.Stop()
delete(activeFeeds, url)
}
}
// Second, check if any feeds have been added
for _, url := range feeds {
// Skip if already present
if _, found := activeFeeds[url]; found {
continue
}
logger.Printf("Adding feed: %s", url)
t := new(time.Ticker)
fetcher := &FeedFetcher{
Poll: feedPoll,
Ticker: t,
Delay: time.After(time.Duration(0)), // check immediately
URL: url,
}
activeFeeds[url] = fetcher
go fetcher.Run(results)
}
}
}
}
func startsWith(s string, prefixes ...string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(s, prefix) {
return true
}
}
return false
}
func main() {
var wg sync.WaitGroup
results := make(chan FetchResult)
rand.Seed(time.Now().UnixNano())
input := flag.String("input", "", "read feed URLs from this file or URL")
poll := flag.Duration("poll", time.Hour, "how often to poll feeds")
listPoll := flag.Duration("listpoll", 15*time.Minute, "how often to check feed list for new feeds")
output := flag.String("output", "river.js", "write output to this file")
quickstart := flag.Bool("quickstart", false, "when true, don't space out initial feed reads")
flag.Parse()
if *input == "" {
fmt.Println("no -input given, exiting...")
return
}
go buildRiver(results, *output)
var feeds []string
if startsWith(*input, "http://", "https://") {
feeds = loadRemoteFeedList(*input)
} else {
feeds = loadLocalFeedList(*input)
}
go pollFeedList(*input, *poll, *listPoll, results)
logger.Printf("Loading %d feeds from %q and writing to %q", len(feeds), *input, *output)
for _, url := range feeds {
wg.Add(1)
if _, found := activeFeeds[url]; found {
logger.Printf("%q already added, skipping", url)
continue
}
var delayDuration time.Duration
if *quickstart {
delayDuration = time.Duration(0)
logger.Printf("%q will update every %v", url, *poll)
} else {
delayDuration = time.Minute * time.Duration(rand.Intn(60))
logger.Printf("%q will first update in %v and then every %v", url, delayDuration, *poll)
}
t := new(time.Ticker)
fetcher := &FeedFetcher{
Poll: *poll,
Ticker: t,
Delay: time.After(delayDuration),
URL: url,
}
activeFeeds[url] = fetcher
go fetcher.Run(results)
}
wg.Wait()
}