-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
317 lines (274 loc) · 7.51 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
package main
import (
"context"
"fmt"
"io"
"log"
"net/url"
"os"
"path"
"sort"
"strings"
"sync"
"text/template"
"time"
strip "github.com/grokify/html-strip-tags-go"
"github.com/mmcdole/gofeed"
)
var (
wg sync.WaitGroup
)
type TemplateData struct {
Posts []*Post
}
type Post struct {
Link string
Title string
Published time.Time
Host string
Description string
Taglist string
}
var (
feeds = []string{
"https://www.inoreader.com/stream/user/1005349717/tag/save",
}
// Show up to 60 days of posts
relevantDuration = 180 * 24 * time.Hour
outputDir = "docs" // So we can host the site on GitHub Pages
outputFile = "index.html"
// Error out if fetching feeds takes longer than a minute
timeout = time.Minute
)
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 {
posts := getAllPosts(ctx, feeds)
if err := os.MkdirAll(outputDir, 0700); err != nil {
return err
}
f, err := os.Create(path.Join(outputDir, outputFile))
if err != nil {
return err
}
defer f.Close()
templateData := &TemplateData{
Posts: posts,
}
if err := executeTemplate(f, templateData); 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
go func() {
for post := range postChan {
posts = append(posts, post)
}
}()
wg.Wait()
close(postChan)
// Sort items chronologically descending
sort.Slice(posts, func(i, j int) bool {
return posts[i].Published.After(posts[j].Published)
})
return posts
}
func mycombine(is ...string) {
for i := 0; i < len(is); i++ {
fmt.Println(is[i])
}
}
func getPosts(ctx context.Context, feedURL string, posts chan *Post) {
defer wg.Done()
parser := gofeed.NewParser()
feed, err := parser.ParseURLWithContext(feedURL, ctx)
if err != nil {
log.Println(err)
return
}
for _, item := range feed.Items {
published := item.PublishedParsed
Description := strip.StripTags(item.Description)
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)
}
categoryStart := strings.Join(item.Categories[:], " ")
post := &Post{
Link: item.Link,
Title: item.Title,
Published: *published,
Host: parsedLink.Host,
Description: Description,
Taglist: categoryStart,
}
posts <- post
}
}
func executeTemplate(writer io.Writer, templateData *TemplateData) error {
htmlTemplate := `
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>IDEAS | Feed</title>
<style>
@import url("https://fonts.googleapis.com/css2?family=Nanum+Myeongjo&display=swap");
body {
font-family: "Nanum Myeongjo", serif;
line-height: 1.7;
max-width: 600px;
margin: 50px auto 50px;
padding: 0 12px 0;
height: 100%;
}
li {
padding-bottom: 16px;
}
.container {
overflow: hidden;
}
.filterDiv {
float: left;
width: 100%;
text-align: left;
margin: 2px;
display: none; /* Hidden by default */
}
/* The "show" class is added to the filtered elements */
.show {
display: block;
}
/* Style the buttons */
.btn {
border: none;
outline: none;
padding: 12px 16px;
background-color: #f1f1f1;
cursor: pointer;
}
/* Add a light grey background on mouse-over */
.btn:hover {
background-color: #ddd;
}
/* Add a dark background to the active button */
.btn.active {
background-color: #666;
color: white;
}
</style>
<script>
filterSelection("all")
function filterSelection(c) {
var x, i;
x = document.getElementsByClassName("filterDiv");
if (c == "all") c = "";
// Add the "show" class (display:block) to the filtered elements, and remove the "show" class from the elements that are not selected
for (i = 0; i < x.length; i++) {
w3RemoveClass(x[i], "show");
if (x[i].className.indexOf(c) > -1) w3AddClass(x[i], "show");
}
}
// Show filtered elements
function w3AddClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
if (arr1.indexOf(arr2[i]) == -1) {
element.className += " " + arr2[i];
}
}
}
// Hide elements that are not selected
function w3RemoveClass(element, name) {
var i, arr1, arr2;
arr1 = element.className.split(" ");
arr2 = name.split(" ");
for (i = 0; i < arr2.length; i++) {
while (arr1.indexOf(arr2[i]) > -1) {
arr1.splice(arr1.indexOf(arr2[i]), 1);
}
}
element.className = arr1.join(" ");
}
// Add active class to the current control button (highlight it)
var btnContainer = document.getElementById("myBtnContainer");
var btns = btnContainer.getElementsByClassName("btn");
for (var i = 0; i < btns.length; i++) {
btns[i].addEventListener("click", function() {
var current = document.getElementsByClassName("active");
current[0].className = current[0].className.replace(" active", "");
this.className += " active";
});
}
</script>
</head>
<body>
<P></P><cite><a href="https://wakeforestid.com/">home</a></cite>
<h1>Latest Literature</h1>
<div id="myBtnContainer">
<button class="btn active" id="al2l" onclick="filterSelection('all')"> Show all</button>
<button class="btn" id="all" onclick="filterSelection('amr')"> AMR</button>
<button class="btn" onclick="filterSelection('fungal')"> Fungal</button>
<button class="btn" onclick="filterSelection('vector')"> Vector</button>
<button class="btn" onclick="filterSelection('bacteria')"> Bacterial</button>
<button class="btn" onclick="filterSelection('std')"> STD</button>
<button class="btn" onclick="filterSelection('surveillance')"> Surveillance</button>
<button class="btn" onclick="filterSelection('modelling')"> Modelling</button>
<button class="btn" onclick="filterSelection('viral')"> Viral</button>
<button class="btn" onclick="filterSelection('omics')"> Omics and Biomarkers</button>
</div>
<div class="container">
<ol>
{{ range .Posts }}<div class="filterDiv all {{ .Taglist}}">
<li><a href="{{ .Link }}">{{ .Title }}</a><p>{{ .Description }}</p> ({{ .Host }})</li>
</div>
{{ end }}
</ol>
</div>
<script>
filterSelection("all")
</script>
<footer>
<p><a href="https://feed.wakeforestid.com/bibliography.bibtex" download>Download this bibliography</a></p>
<p><a href="https://github.com/wf-id/ideas-feed">What is this?</a></p>
<p><a href="https://www.zotero.org/groups/4900647/ideas-feed/library">Zotero Library</a></p>
<p><a href="https://www.researchrabbit.ai/">Research Rabbit</a></p>
<p><a href="https://github.com/jamesroutley/news.routley.io">What is this based on</a></p>
<p><a href="https://wakeforestid.com">Main Website</a></p>
</footer>
</body>
</html>
`
tmpl, err := template.New("webpage").Parse(htmlTemplate)
if err != nil {
return err
}
if err := tmpl.Execute(writer, templateData); err != nil {
return err
}
return nil
}