-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (86 loc) · 2.37 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
//go:generate go run generate.go
//go:generate go fmt template.go
package main
import (
"flag"
"html/template"
"log"
"os"
"path"
"sort"
"github.com/bake/goread/feed"
"github.com/bake/goread/funcs"
"gopkg.in/yaml.v2"
)
var version = "development"
type feeds map[string][]string
func main() {
inPath := flag.String("in", "feeds.yml", "Path to a list of feed URLs")
outPath := flag.String("out", ".", "Path to generated HTML")
tmplPath := flag.String("template", "", "Path to the HTML template")
maxItems := flag.Int("max-items", 100, "Max number of items per page")
concurrent := flag.Int64("n", 5, "Number of concurrent downloads")
truncateLen := flag.Int("truncate-length", 256, "Number of characters per feed item")
flag.Parse()
p := page{
out: *outPath,
max: *maxItems,
Version: version,
}
var err error
p.tmpl, err = template.
New(path.Base(*tmplPath)).
Funcs(funcs.FuncMap(*truncateLen)).
Parse(feedTmpl)
if err != nil {
log.Fatalf("could not parse internal template: %v", err)
}
if *tmplPath != "" {
p.tmpl, err = p.tmpl.ParseFiles(*tmplPath)
}
if err != nil {
log.Fatalf("could not parse template: %v", err)
}
r, err := os.Open(*inPath)
if err != nil {
log.Fatalf("could not open feeds: %v", err)
}
defer r.Close()
var fs feeds
if err := yaml.NewDecoder(r).Decode(&fs); err != nil {
log.Fatalf("could not decode %s: %v", path.Base(*inPath), err)
}
var items []*feed.Item
for res := range fetchAll(*concurrent, fs) {
if res.err != nil {
log.Printf("could not get %s: %v", res.url, res.err)
continue
}
for _, item := range res.feed.Items {
item.Category = res.cat
items = append(items, item)
}
}
sort.Sort(sort.Reverse(feed.SortByDate(items)))
cats := map[string][]*feed.Item{"index": items}
feeds := map[string][]*feed.Item{}
hash := funcs.Hash()
for _, item := range items {
cats[item.Category] = append(cats[item.Category], item)
feeds[hash(item.Feed.Link)] = append(feeds[hash(item.Feed.Link)], item)
}
for cat := range cats {
p.Categories = append(p.Categories, cat)
}
sort.Strings(p.Categories)
for cat, items := range cats {
if err := p.render(cat, cat, items); err != nil {
log.Fatalf("could not render %s: %v", cat, err)
}
}
for feed, items := range feeds {
if err := p.render(feed, items[0].Feed.Title, items); err != nil {
log.Fatalf("could not render %s: %v", feed, err)
}
}
}