ref: f75cc73b8b7874d2125ce0b9ec2b650f9022a255 openring/openring.go -rw-r--r-- 2.9 KiB View raw
                                                                                
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
package main

import (
	"html"
	"html/template"
	"io/ioutil"
	"log"
	"net/url"
	"os"
	"sort"
	"strconv"
	"time"

	"github.com/SlyMarbo/rss"
	"github.com/mattn/go-runewidth"
	"github.com/microcosm-cc/bluemonday"
	"git.sr.ht/~sircmpwn/getopt"
)

type Article struct {
	Date        time.Time
	Link        string
	SourceLink  string
	SourceTitle string
	Summary     template.HTML
	Title       string
}

func main() {
	var (
		narticles  int = 3
		perSource  int = 1
		summaryLen int = 256
		sources    []*url.URL
	)

	opts, optind, err := getopt.Getopts(os.Args[1:], "l:n:p: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 'p':
			perSource, 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+1:]) != 0 {
		log.Fatalf(
			"Usage: %s [-s https://source.rss...] < in.html > out.html",
			os.Args[0])
	}

	input, err := ioutil.ReadAll(os.Stdin)
	if err != nil {
		panic(err)
	}

	tmpl, err := template.
		New("template").
		Funcs(map[string]interface{}{
			"date": func(t time.Time) string {
				return t.Format("January 2, 2006")
			},
		}).
		Parse(string(input))
	if err != nil {
		panic(err)
	}

	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
		}
		items := feed.Items
		if len(items) > perSource {
			items = items[:perSource]
		}
		for _, item := range items {
			raw_summary := item.Summary
			if len(raw_summary) == 0 {
				raw_summary = html.UnescapeString(item.Content)
			}
			summary := runewidth.Truncate(
				policy.Sanitize(raw_summary), summaryLen, "…")
			articles = append(articles, &Article{
				Date:        item.Date,
				SourceLink:  feed.Link,
				SourceTitle: feed.Title,
				Summary:     template.HTML(summary),
				Title:       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]
	err = tmpl.Execute(os.Stdout, struct{
		Articles []*Article
	}{
		Articles: articles,
	})
	if err != nil {
		panic(err)
	}
}