/
main.go
408 lines (309 loc) · 11.7 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
398
399
400
401
402
403
404
405
406
407
408
package main
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"regexp"
"strconv"
"strings"
"time"
"unicode"
"github.com/andybalholm/cascadia"
"golang.org/x/net/html"
"golang.org/x/net/html/charset"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
//Word Contains Marshalled Mongo document data: parsed words of a feed
type Word struct {
Name string `bson:"_id" json:"name"`
Count int `json:"count"`
}
type Grouping struct {
Date string `bson:"date" json:"date"`
Category string `bson:"category" json:"category"`
}
type jsonWordFreq struct {
Groupings Grouping `bson:"_id" json:"grouping"`
Count int `json:"count"`
}
//Feed Contains Marshalled Mongo document data: parent feed data
type Feed struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Title string `xml:"title"`
Link string `xml:"link"`
PubDate string `xml:"pubDate"`
Date time.Time
Category string
ArticleId string
Words []Word `bson:"Words"`
Processed bool
}
//FeedCount container for feed count
type FeedCount struct {
Id bson.ObjectId `bson:"_id"`
count string `bson:"count"`
}
//Result For storing rss xml Marshalled data
type Result struct {
XMLName xml.Name `xml:"rss"`
Version string `xml:"version,attr"`
// Required
Title string `xml:"channel>title"`
Link string `xml:"channel>link"`
Description string `xml:"channel>description"`
// Optional
PubDate string `xml:"channel>pubDate"`
ItemList []Feed `xml:"channel>item"`
}
type Page struct {
Title string
Body []byte
}
//FeedSetting contains: articleId is the class or id name of the element in which to get the articles words from (include . or #)
type FeedSetting struct {
Name string
URL string
ArticleId string
}
func main() {
feedSettings := []FeedSetting{
{Name: "CNN", URL: "http://rss.cnn.com/rss/cnn_topstories.rss", ArticleId: ".zn-body__paragraph"},
{Name: "CBS", URL: "http://www.cbsnews.com/latest/rss/main", ArticleId: "#article-entry"},
{Name: "BBC", URL: "http://feeds.bbci.co.uk/news/rss.xml", ArticleId: ".story-body__inner"},
{Name: "FOX", URL: "http://feeds.foxnews.com/foxnews/latest?format=xml", ArticleId: ".article-text"},
{Name: "NPR", URL: "http://www.npr.org/rss/rss.php?id=1001", ArticleId: "#storytext"}}
//time inverval when check for new feeds
go startFeeder(60000, feedSettings)
http.HandleFunc("/", handler)
http.HandleFunc("/api/getwords", getWordHandler)
http.HandleFunc("/api/getArticleCount", getArticleCount)
http.HandleFunc("/api/getTimeLine", getTimeLine)
http.ListenAndServe(":8080", nil)
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
func startFeeder(seconds int, feedSettings []FeedSetting) {
for true {
//only can use utf-8 encoded xml files
for _, feed := range feedSettings {
fmt.Printf("Looking for new feeds in... %v \n", feed.Name)
getFeeds(feed)
}
session, _ := mgo.Dial("localhost")
feedCollection := session.DB("wcproc").C("feeds")
feeds := []Feed{}
feedCollection.Find(bson.M{"processed": false}).All(&feeds)
session.Close()
//work through all the sites
for _, feed := range feeds {
processWords(feed)
fmt.Printf("processed..: %v from: %v \n", feed.Title, feed.Category)
}
time.Sleep(time.Duration(seconds) * time.Millisecond)
}
}
func loadPage(title string) []byte {
filename := title
body, _ := ioutil.ReadFile(filename)
fmt.Printf("loading: %v \n", title)
return body
}
func handler(w http.ResponseWriter, r *http.Request) {
title := r.URL.Path[1:]
p := loadPage(title)
fmt.Fprintf(w, string(p))
}
func getArticleCount(w http.ResponseWriter, r *http.Request) {
//categoryParam := r.URL.Query()["category"][0]
session, _ := mgo.Dial("localhost")
feeds := session.DB("wcproc").C("feeds")
//match := bson.M{"$match": bson.M{"category": "CBS"}}
group := bson.M{"$group": bson.M{"_id": 1, "count": bson.M{"$sum": 1}}}
//group := bson.M{"$group": bson.M{"_id": "$Words._id", "count": bson.M{"$sum": bson.M{"$add": "$Words.count"}}}}
operations := []bson.M{group}
pipe := feeds.Pipe(operations)
var results FeedCount
err := pipe.One(&results)
checkError(err)
session.Close()
jsonWords, err := json.Marshal(results)
if err != nil {
fmt.Printf("%v", err.Error())
}
fmt.Fprintf(w, string(jsonWords))
}
func getTimeLine(w http.ResponseWriter, r *http.Request) {
//db.feeds.aggregate({$project: {dater: { $dateToString: { format: "%Y-%m-%d", date: "$date" }}, Words:1, category:1}}, {$unwind: "$Words"}, {$match: {$and: [{ category: "FOX"}, {"Words._id" : "trump" }]}}, {$group : {_id: "$dater", count: { $sum: {$add: "$Words.count"}}}}, {$sort: {dater: -1}})
wordParam := strings.ToLower(r.URL.Query()["word"][0])
fmt.Printf("Querying Timeline for: %v \n", wordParam)
session, _ := mgo.Dial("localhost")
wordsCollection := session.DB("wcproc").C("feeds")
project := bson.M{"$project": bson.M{"dater": bson.M{"$dateToString": bson.M{"format": "%Y-%m-%d", "date": "$date"}}, "Words": 1, "category": 1}}
unWind := bson.M{"$unwind": "$Words"}
match := bson.M{"$match": bson.M{"Words._id": wordParam}}
group := bson.M{"$group": bson.M{"_id": bson.M{"date": "$dater", "category": "$category"}, "count": bson.M{"$sum": bson.M{"$add": "$Words.count"}}}}
sort := bson.M{"$sort": bson.M{"_id": 1}}
operations := []bson.M{project, unWind, match, group, sort}
pipe := wordsCollection.Pipe(operations)
var results []jsonWordFreq
err := pipe.All(&results)
checkError(err)
session.Close()
jsonWords, err := json.Marshal(results)
checkError(err)
fmt.Fprintf(w, string(jsonWords))
}
func getWordHandler(w http.ResponseWriter, r *http.Request) {
//mongo query used below
//db.feeds.aggregate({$match: { category: "BBC"}}, { $project: { Words: 1 }}, { $unwind: "$Words" }, { $group: { _id: "$Words._id", count: { $sum: {$add: "$Words.count"} } }});
//this gets a specific word and category for each date
matchParam := r.URL.Query()["category"][0]
filterParam := r.URL.Query()["filter"][0]
filterParamAry := strings.Split(filterParam, ",")
fmt.Printf("Querying Wordhandler for: %v \n", filterParam)
session, _ := mgo.Dial("localhost")
wordsCollection := session.DB("wcproc").C("feeds")
project := bson.M{"$project": bson.M{"Words": 1}}
match := bson.M{"$match": bson.M{"category": matchParam}}
unWind := bson.M{"$unwind": "$Words"}
group := bson.M{"$group": bson.M{"_id": "$Words._id", "count": bson.M{"$sum": bson.M{"$add": "$Words.count"}}}}
sort := bson.M{"$sort": bson.M{"count": -1}}
limit := bson.M{"$limit": 50}
operations := []bson.M{}
if len(filterParamAry[0]) != 0 {
tempAry := []interface{}{}
for _, i := range filterParamAry {
tempAry = append(tempAry, bson.M{"_id": strings.Trim(i, " ")})
}
filter := bson.M{"$match": bson.M{"$or": tempAry}}
operations = []bson.M{match, project, unWind, group, filter, sort, limit}
} else {
operations = []bson.M{match, project, unWind, group, sort, limit}
}
pipe := wordsCollection.Pipe(operations)
var results []Word
err := pipe.All(&results)
checkError(err)
session.Close()
jsonWords, err := json.Marshal(results)
checkError(err)
fmt.Fprintf(w, string(jsonWords))
}
func getFeeds(feedSetting FeedSetting) {
resp, err := http.Get(feedSetting.URL)
//there's no reason to panic on this
if err != nil {
fmt.Printf("Couldn't reach URL: %v \n\n", feedSetting.URL)
return
}
var results Result
decoder := xml.NewDecoder(resp.Body)
decoder.CharsetReader = charset.NewReaderLabel
err = decoder.Decode(&results)
checkError(err)
//xml.Unmarshal([]byte(tempStr), &results)
session, _ := mgo.Dial("localhost")
feeds := session.DB("wcproc").C("feeds")
feed := Feed{}
feeds.Find(bson.M{"category": feedSetting.Name}).Sort("-date").One(&feed)
//convert the feed.date string to time.Time since unmarshal wont do it for me
for iter, element := range results.ItemList {
lastThree := element.PubDate[len(element.PubDate)-3:]
results.ItemList[iter].Link = strings.Trim(element.Link, " ")
if _, err := strconv.Atoi(lastThree); err == nil {
results.ItemList[iter].Date, err = time.Parse(time.RFC1123Z, element.PubDate)
checkError(err)
} else {
results.ItemList[iter].Date, err = time.Parse(time.RFC1123, element.PubDate)
checkError(err)
}
}
//check to see if any articles exist past the last article date in db
for iter, element := range results.ItemList {
if element.Date.After(feed.Date.Local()) {
//check title name against db for duplicates, some news feeds like to change the pubdate over the length of 6 hours
matchup := Feed{}
feeds.Find(bson.M{"title": element.Title, "category": feedSetting.Name}).One(&matchup)
if matchup.Category == "" {
fmt.Printf("adding: %v -- %v \n", element.Date, element.Title)
results.ItemList[iter].ArticleId = feedSetting.ArticleId
results.ItemList[iter].Category = feedSetting.Name
_ = feeds.Insert(results.ItemList[iter])
}
}
}
session.Close()
}
//array functionality with strings.contains
func containWords(word string, words []string) bool {
for _, ele := range words {
if strings.Compare(strings.ToLower(strings.Trim(word, ". ,")), ele) == 0 {
return true
}
}
return false
}
func processWords(feed Feed) {
//placeholder
session, _ := mgo.Dial("localhost")
feeds := session.DB("wcproc").C("feeds")
resp, err := http.Get(feed.Link)
//there's no reason to panic on this
if err != nil {
fmt.Printf("Couldn't reach URL: %v \n\n", feed.Link)
return
}
doc, err := html.Parse(resp.Body)
checkError(err)
body := cascadia.MustCompile(feed.ArticleId).MatchAll(doc)
var strBuffer bytes.Buffer
re := regexp.MustCompile("\\<[^>]*\\>")
for _, element := range body {
var buf bytes.Buffer
html.Render(&buf, element)
strBuffer.WriteString(" " + re.ReplaceAllString(html.UnescapeString(buf.String()), ""))
//fmt.Printf("... %v ... \n", re.ReplaceAllString(html.UnescapeString(buf.String()), ""))
}
f := func(c rune) bool {
return !unicode.IsLetter(c) && unicode.IsNumber(c)
}
strings.FieldsFunc(strBuffer.String(), f)
words := make(map[string]int)
for _, w := range strings.Fields(strBuffer.String()) {
words[w]++
}
omitWords := []string{"the", "of", "a", "at", "as", "with", "been", "in", "that", "and", "with", "from", "more", "been", "we", "not", "by", "he", "who", "were",
"so", "just", "also", "his", "will", "up", "had", "out", "if", "an", "to", "on", "which", "just", "they", "is", "it", "but", "its", "could", "us",
"him", "next", "time", "like", "...", "both", "stil", "why", "it", "even", "no", "do", "first", "two", "for", "or", "our", "did", "very", "yet",
"most", "new", "how", "you", "i", "we", "sure", "move", "close", "until", "my", "get", "go", "those", "though", "be", "me", "met", "recent",
"rest", "end", "put", "seen", "else", "should", "met", "center", "over", "would", "much", "lot", "room", "three", "four", "five", "six", "seven",
"eight", "nine", "ten", "see", "set", "mr", "few", "old", "key", "sent", "tell", "ever", "under", "through", "led", "own", "such", "people",
"due", "role", "never", "look", "full", "try", "was", "said", "this", "are", "their", "when", "can", "now", "after", "than", "some", "when",
"her", "image", "about", "she", "i", "all", "one", "have", "has", "your", "what", "other", "there", "caption", "copyright"}
//fmt.Printf("OMITTING:")
for key, value := range words {
//get rid of words that have these in them
if !strings.ContainsAny(key, "-<>/_{}=;#&()*%$@1234567890") {
if !containWords(key, omitWords) {
//keep these words but trim off these chars
item := Word{Name: strings.ToLower(strings.Trim(key, ". ,\"")), Count: value}
feed.Words = append(feed.Words, item)
} else {
//fmt.Printf("%v \n", key)
}
} else {
//fmt.Printf("%v \n", key)
}
}
feed.Processed = true
feeds.Update(bson.M{"_id": feed.Id}, feed)
session.Close()
}