-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
314 lines (263 loc) Β· 6.79 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
package main
import (
"context"
_ "embed"
"fmt"
"html/template"
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"regexp"
"sort"
"strings"
"sync"
"time"
"github.com/cixtor/readability"
"github.com/mmcdole/gofeed"
)
var (
wg sync.WaitGroup
reader = readability.New()
nonAlphanumeric = regexp.MustCompile(`([^\w\d])`)
)
//go:embed index.template.html
var indexTmpl string
//go:embed post.template.html
var postTmpl string
//go:embed styles.css
var styles string
type Post struct {
Link string
Title string
Published time.Time
Host string
Content template.HTML
Filename string
}
var (
feeds = []string{
"https://themargins.substack.com/feed.xml",
"https://jvns.ca/atom.xml",
"https://joy.recurse.com/feed.atom",
"https://danluu.com/atom.xml",
"https://blog.veitheller.de/feed.rss",
"https://twobithistory.org/feed.xml",
"https://rachelbythebay.com/w/atom.xml",
"https://scattered-thoughts.net/rss.xml",
// My mum's food blog π
"https://gochugarugirl.com/feed/",
"https://commoncog.com/blog/rss/",
"https://highgrowthengineering.substack.com/feed",
"http://tonsky.me/blog/atom.xml",
"https://www.benkuhn.net/index.xml",
"https://hnrss.org/frontpage?points=50",
"https://solar.lowtechmagazine.com/feeds/all-en.atom.xml",
"https://www.slowernews.com/rss.xml",
"https://macwright.com/rss.xml",
"https://mikehudack.substack.com/feed",
"https://www.wildlondon.org.uk/blog/all/rss.xml",
"https://blaggregator.recurse.com/atom.xml?token=4c4c4e40044244aab4a36e681dfb8fb0",
"https://blog.golang.org/feed.atom?format=xml",
"https://anewsletter.alisoneroman.com/feed",
"https://routley.io/reserialised/great-expectations/2022-08-24/index.xml",
}
// Show up to 60 days of posts
// TODO
relevantDuration = 7 * 24 * time.Hour
outputDir = "docs" // So we can host the site on GitHub Pages
outputFile = "index.html"
logFile = path.Join(outputDir, "log.txt")
// Error out if fetching feeds takes longer than a minute
timeout = time.Minute
)
var blocklist = map[string]bool{
"www.metafilter.com": true,
}
func main() {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := run(ctx); err != nil {
log.Fatal(err)
}
}
func run(ctx context.Context) error {
if err := os.MkdirAll(outputDir, 0700); err != nil {
return err
}
logF, err := os.Create(logFile)
if err != nil {
return err
}
log.SetOutput(logF)
posts := getAllPosts(ctx, feeds)
start := time.Now()
defer func() {
log.Printf("Templated %d posts, took %s", len(posts), time.Since(start))
}()
if err := os.RemoveAll(path.Join(outputDir, "posts")); err != nil {
return err
}
if err := os.MkdirAll(path.Join(outputDir, "posts"), 0700); err != nil {
return err
}
for _, post := range posts {
if string(post.Content) == "" {
log.Printf("Skipping writing post, no content: %s", post.Link)
continue
}
f, err := os.Create(path.Join(outputDir, "posts", post.Filename))
if err != nil {
return err
}
tmpl, err := template.New("post").Parse(postTmpl)
if err != nil {
return err
}
data := map[string]interface{}{
"Styles": template.CSS(styles),
"Content": post.Content,
"Original": post.Link,
"Title": post.Title,
}
if err := tmpl.Execute(f, data); err != nil {
return err
}
f.Close()
}
f, err := os.Create(path.Join(outputDir, outputFile))
if err != nil {
return err
}
defer f.Close()
data := map[string]interface{}{
"Posts": posts,
"Styles": template.CSS(styles),
}
if err := executeTemplate(f, data); err != nil {
return err
}
return nil
}
// getAllPosts returns all posts from all feeds from the last `relevantDuration`
// time period. Posts are sorted chronologically descending.
func getAllPosts(ctx context.Context, feeds []string) []*Post {
postChan := make(chan *Post)
wg.Add(len(feeds))
for _, feed := range feeds {
go getPosts(ctx, feed, postChan)
}
var posts []*Post
seen := map[string]bool{}
go func() {
for post := range postChan {
if seen[post.Link] {
continue
}
posts = append(posts, post)
seen[post.Link] = true
}
}()
wg.Wait()
close(postChan)
// Sort items chronologically descending
sort.Slice(posts, func(i, j int) bool {
if posts[i].Published.Equal(posts[j].Published) {
return posts[i].Title < posts[j].Title
}
return posts[i].Published.After(posts[j].Published)
})
return posts
}
func getPosts(ctx context.Context, feedURL string, posts chan *Post) {
start := time.Now()
defer func() {
log.Printf("Fetched posts from %s, took %s", feedURL, time.Since(start))
}()
defer wg.Done()
parser := gofeed.NewParser()
feed, err := parser.ParseURLWithContext(feedURL, ctx)
if err != nil {
log.Println(fmt.Errorf("error parsing %s: %w", feedURL, err))
return
}
for _, item := range feed.Items {
published := item.PublishedParsed
if published == nil {
published = item.UpdatedParsed
}
if published.Before(time.Now().Add(-relevantDuration)) {
continue
}
parsedLink, err := url.Parse(item.Link)
if err != nil {
log.Println(err)
}
if blocklist[parsedLink.Host] {
continue
}
post := &Post{
Link: item.Link,
Title: item.Title,
Published: *published,
Host: parsedLink.Host,
Content: template.HTML(item.Content),
Filename: titleToFilename(item.Title),
}
// If content isn't available from RSS, try to pull it from the webpage
// itself
if post.Content == "" {
req, err := http.NewRequestWithContext(ctx, "GET", item.Link, nil)
if err != nil {
log.Println(err)
continue
}
req.Header["User-Agent"] = []string{
"Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)",
}
rsp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println(err)
continue
}
// Don't try an parse non-HTML
contentType := rsp.Header.Get("content-type")
if contentType != "" && !strings.HasPrefix(contentType, "text/html") {
log.Printf("Not HTML: %s\n", item.Link)
continue
}
article, err := reader.Parse(rsp.Body, item.Link)
if err != nil {
log.Println(err)
continue
}
post.Content = template.HTML(article.Content)
if post.Content == "" {
log.Printf("Content still empty after HTML reader: %s", item.Link)
}
rsp.Body.Close()
}
posts <- post
}
}
func titleToFilename(s string) string {
s = nonAlphanumeric.ReplaceAllLiteralString(s, " ")
s = strings.ToLower(s)
fields := strings.Fields(s)
s = strings.Join(fields, "-") + ".html"
s = strings.TrimPrefix(s, "show-hn-")
s = strings.TrimPrefix(s, "ask-hn-")
return s
}
func executeTemplate(writer io.Writer, templateData map[string]interface{}) error {
tmpl, err := template.New("index").Parse(indexTmpl)
if err != nil {
return err
}
if err := tmpl.Execute(writer, templateData); err != nil {
return err
}
return nil
}