forked from landaire/makebuzz
/
buzz.go
150 lines (118 loc) · 3.13 KB
/
buzz.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
package main
import (
"fmt"
"time"
"bufio"
"strings"
"sync"
"math"
"sort"
rss "github.com/jteeuwen/go-pkg-rss"
"github.com/jteeuwen/go-pkg-xmlx"
)
const (
baseFeedUrl = "http://www.buzzfeed.com/%s.xml"
timeout = 5
)
type Headlines []string
type BuzzFeeds []*BuzzFeedRss
type BuzzFeedRss struct {
*rss.Feed
}
var (
FetchedHeadlines Headlines
FetchedHeadlinesLock sync.RWMutex
uniquesChecked = false
buzzFeedConfig *BuzzFeedConfig
)
func CreateFeeds() BuzzFeeds {
buzzFeedConfig = &GlobalConfig.BuzzFeed
var feeds BuzzFeeds
// initialize the buzzfeed RSS with a timeout of 5 seconds
for i := 0; i < len(buzzFeedConfig.Feeds); i++ {
feeds = append(feeds, &BuzzFeedRss{rss.New(timeout, true, feedChannelHandler, feedItemHandler)})
}
return feeds
}
// Polls a series of feeds for updates
func (this BuzzFeeds) Poll(timeout time.Duration) <-chan bool {
c := make(chan bool)
go func() {
for {
for i, feed := range this {
// Wait until the next update time
<-time.After(time.Duration(feed.SecondsTillUpdate() * 1e9))
feedUrl := fmt.Sprintf(baseFeedUrl, buzzFeedConfig.Feeds[i])
feed.Poll(feedUrl, nil)
}
// Write to the output file
SaveHeadlines()
uniquesChecked = true
c <- true
}
}()
return c
}
// Polls the RSS feed for new channels and items
func (feed *BuzzFeedRss) Poll(feedUrl string, cr xmlx.CharsetFunc) {
Logger.Println("Fetching feed ", feedUrl)
if err := feed.Fetch(feedUrl, cr); err != nil {
Logger.Errorf("%s: %s\n", feedUrl, err)
}
}
// Rounding utility function
// Courtesy of https://gist.github.com/DavidVaini/10308388
func Round(val float64, roundOn float64, places int) (newVal float64) {
var round float64
pow := math.Pow(10, float64(places))
digit := pow * val
_, div := math.Modf(digit)
if div >= roundOn {
round = math.Ceil(digit)
} else {
round = math.Floor(digit)
}
newVal = round / pow
return
}
// Calculates the average length of each headline
func (h Headlines) AverageWords() int {
if len(h) == 0 {
return 0
}
length := 0
for _, str := range h {
length += WordCount(str)
}
return int(Round(float64(length)/float64(len(h)), 1, 0))
}
func WordCount(s string) int {
length := 0
scanner := bufio.NewScanner(strings.NewReader(s))
scanner.Split(bufio.ScanWords)
for scanner.Scan() {
length++
}
return length
}
func feedChannelHandler(feed *rss.Feed, newchannels []*rss.Channel) {
// really do nothing with these -- they're not useful
}
func feedItemHandler(feed *rss.Feed, ch *rss.Channel, newitems []*rss.Item) {
ChainLock.Lock()
FetchedHeadlinesLock.Lock()
defer FetchedHeadlinesLock.Unlock()
defer ChainLock.Unlock()
for _, item := range newitems {
if !uniquesChecked && len(FetchedHeadlines) > 0 {
sort.Strings(FetchedHeadlines)
if index := sort.SearchStrings(FetchedHeadlines, item.Title); index < len(FetchedHeadlines) && FetchedHeadlines[index] == item.Title {
continue
}
}
Logger.Println("Adding headline", item.Title)
HeadlineChain.Build(strings.NewReader(item.Title))
FetchedHeadlines = append(FetchedHeadlines, item.Title)
}
Logger.Printf("%d new item(s) in %s\n", len(newitems), feed.Url)
}