This repository was archived by the owner on Jan 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathopenring.go
More file actions
118 lines (108 loc) · 2.38 KB
/
Copy pathopenring.go
File metadata and controls
118 lines (108 loc) · 2.38 KB
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
package main
import (
"html"
"log"
"net/url"
"os"
"sort"
"strconv"
"time"
"git.sr.ht/~sircmpwn/getopt"
"github.com/BurntSushi/toml"
"github.com/SlyMarbo/rss"
"github.com/mattn/go-runewidth"
"github.com/microcosm-cc/bluemonday"
)
type Article struct {
Date time.Time
Link string
SourceLink string
SourceTitle string
Summary string
Title string
}
func main() {
var (
narticles int = 3
summaryLen int = 256
sources []*url.URL
)
opts, optind, err := getopt.Getopts(os.Args, "l:n:s:")
if err != nil {
panic(err)
}
for _, opt := range opts {
switch opt.Option {
case 'l':
summaryLen, err = strconv.Atoi(opt.Value)
if err != nil {
panic(err)
}
case 'n':
narticles, err = strconv.Atoi(opt.Value)
if err != nil {
panic(err)
}
case 's':
u, err := url.Parse(opt.Value)
if err != nil {
panic(err)
}
sources = append(sources, u)
}
}
if len(os.Args) != optind {
log.Fatalf(
"Usage: %s [-s https://source.rss...] > out.toml",
os.Args[0])
}
log.Println("Fetching feeds...")
var feeds []*rss.Feed
for _, source := range sources {
feed, err := rss.Fetch(source.String())
if err != nil {
log.Printf("Error fetching %s: %s", source.String(), err.Error())
continue
}
feeds = append(feeds, feed)
log.Printf("Fetched %s", feed.Title)
}
if len(feeds) == 0 {
log.Fatal("Expected at least one feed to successfully fetch")
}
policy := bluemonday.StrictPolicy()
var articles []Article
for _, feed := range feeds {
if len(feed.Items) == 0 {
log.Printf("Warning: feed %s has no items", feed.Title)
continue
}
item := feed.Items[0]
rawSummary := item.Summary
if len(rawSummary) == 0 {
rawSummary = html.UnescapeString(item.Content)
}
summary := runewidth.Truncate(
policy.Sanitize(rawSummary), summaryLen, "…")
articles = append(articles, Article{
Date: item.Date,
SourceLink: feed.Link,
SourceTitle: html.UnescapeString(feed.Title),
Summary: summary,
Title: html.UnescapeString(item.Title),
Link: item.Link,
})
}
sort.Slice(articles, func(i, j int) bool {
return articles[i].Date.After(articles[j].Date)
})
if len(articles) < narticles {
narticles = len(articles)
}
articles = articles[:narticles]
if err := toml.NewEncoder(os.Stdout).Encode(struct {
Articles []Article
}{articles}); err != nil {
log.Fatal(err)
}
}