/
kotaku-uk-rss.go
198 lines (162 loc) · 4.07 KB
/
kotaku-uk-rss.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
package main
import (
"fmt"
"log"
"net/http"
"sort"
"sync"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/gorilla/feeds"
)
/*
Structs
*/
// Article holds various info about an article.
type Article struct {
Title string
Date time.Time
Author string
Summary string
MediaURL string
URL string
Href string
}
// Articles is a collection of Article structs.
type Articles []Article
// SectionsByArticleLength enables sorting section nodes by number of article
// child nodes.
type SectionsByArticleLength []*goquery.Selection
func (s SectionsByArticleLength) Len() int {
return len(s)
}
func (s SectionsByArticleLength) Less(i int, j int) bool {
return s[i].Find("article").Length() > s[j].Find("article").Length()
}
func (s SectionsByArticleLength) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
// RssCache is a string cache with mutex locks.
type RssCache struct {
sync.RWMutex
body string
}
// Set allows setting the RssCache body.
func (r *RssCache) Set(value string) {
r.Lock()
defer r.Unlock()
r.body = value
}
// Get allows getting the RssCache body.
func (r *RssCache) Get() string {
r.RLock()
defer r.RUnlock()
return r.body
}
/*
Functions
*/
func fetchDocument(url string) *goquery.Document {
doc, err := goquery.NewDocument(url)
if err != nil {
log.Fatal(err)
}
return doc
}
func extractArticleSection(doc *goquery.Document) *goquery.Selection {
var sections = SectionsByArticleLength{}
doc.Find("section").Each(func(i int, s *goquery.Selection) {
sections = append(sections, s)
})
sort.Sort(sections)
return sections[0]
}
func parseArticleSection(section *goquery.Selection) Articles {
result := Articles{}
section.Find("article").Each(func(i int, s *goquery.Selection) {
result = append(result, parseArticle(s))
})
return result
}
func parseArticle(s *goquery.Selection) Article {
href, _ := s.Find(".media__body h2 a").Attr("href")
if href == "" {
href, _ = s.Find("figure a").Attr("href")
}
url := rootURL + href
summary := s.Find(".media__body p").Text()
title := s.Find(".media__body h2").Text()
if title == "" {
title = truncateString(summary, 60) + "..."
}
mediaURL, _ := s.Find("figure").Attr("data-media992")
timeString, _ := s.Find(".meta__limited time").Attr("datetime")
parsedTime, _ := time.Parse(time.RFC3339, timeString)
return Article{
Title: title,
Date: parsedTime,
Author: s.Find(".meta__full a.is-author").Text(),
Summary: summary,
MediaURL: mediaURL,
URL: url,
Href: href,
}
}
func truncateString(s string, l int) string {
end := len(s)
if end > l {
end = l
}
return s[:end]
}
func getArticlesFromURL(url string) Articles {
doc := fetchDocument(url)
section := extractArticleSection(doc)
return parseArticleSection(section)
}
func buildFeed(articles Articles) *feeds.Feed {
feed := &feeds.Feed{
Title: "Kotaku UK",
Link: &feeds.Link{Href: "http://www.kotaku.co.uk/"},
Description: "Kotaku UK is the UK version of Kotaku",
Created: time.Now(),
}
feed.Items = []*feeds.Item{}
for _, article := range articles {
feed.Items = append(feed.Items, buildFeedItem(article))
}
return feed
}
func buildFeedItem(article Article) *feeds.Item {
description := article.Summary
if article.MediaURL != "" {
description = fmt.Sprintf("<img href=\"%s\" /> %s",
article.MediaURL, description)
}
return &feeds.Item{
Id: article.Href,
Title: article.Title,
Link: &feeds.Link{Href: article.URL},
Description: description,
Author: &feeds.Author{Name: article.Author},
Created: article.Date,
}
}
func serveRss(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, rssCache.Get())
}
func updateRssLoop() {
for {
articles := Articles{}
for _, url := range pageUrls {
fmt.Printf("fetching and parsing articles from: %s\n", url)
articles = append(articles, getArticlesFromURL(url)...)
}
fmt.Printf("building feed cache... ")
feed, _ := buildFeed(articles).ToRss()
rssCache.Set(feed)
fmt.Println("done")
fmt.Println("taking a nap for 60 seconds ^_^")
time.Sleep(60 * time.Second)
}
}