This repository has been archived by the owner on May 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
feedloggr.go
281 lines (242 loc) · 6.26 KB
/
feedloggr.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
package main
import (
"flag"
"fmt"
"html/template"
"net/url"
"os"
"path/filepath"
"regexp"
"strings"
"time"
"github.com/lmas/feedloggr/internal"
)
type command struct {
Cmd string
Help string
Func func([]string)
}
var (
confFile = flag.String("conf", ".feedloggr.yml", "Path to conf file")
confVerbose = flag.Bool("verbose", false, "Print debug messages while running")
commands []command
)
func main() {
commands = []command{
{"discover", "Try discover feeds from <URL>", cmdDiscover},
{"example", "Print example config", cmdExample},
{"help", "Print this help message and exit", cmdHelp},
{"regexp", "Try parsing items from <URL> using <regexp> rule", cmdRegexp},
{"run", "Update feeds and output new page", cmdRun},
{"test", "Try loading config", cmdTest},
{"version", "Print version information", cmdVersion},
}
flag.Usage = printUsage
flag.Parse()
cmd := strings.ToLower(flag.Arg(0))
args := flag.Args()
if len(args) > 0 {
args = args[1:] // Removes the cmd arg
}
for _, c := range commands {
if c.Cmd == cmd {
c.Func(args)
return
}
}
printUsage()
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func printUsage() {
out := flag.CommandLine.Output()
fmt.Fprintf(out, "Usage of %s:\n\n", os.Args[0])
fmt.Fprintln(out, "Flags")
flag.PrintDefaults()
fmt.Fprintln(out, "\nCommands")
for _, c := range commands {
fmt.Fprintf(out, " %s\n\t%s\n", c.Cmd, c.Help)
}
}
func cmdHelp(args []string) {
printUsage()
}
func cmdVersion(args []string) {
// This is supposed to be a toilet/paper roll
fmt.Printf(" ,-. \n"+
" ( O )`~-~-~-~-~-~-~-~-~-, \n"+
" |`-'| -- %s --\t | \n"+
" | | %s\t | \n"+
" `-' `~-~-~-~-~-~-~-~-~-' \n", internal.GeneratorName, internal.GeneratorVersion)
}
func cmdExample(args []string) {
fmt.Println(internal.ExampleConf())
}
func cmdTest(args []string) {
conf, err := internal.LoadConf(*confFile)
if err != nil {
fmt.Printf("Error loading conf %s: %s\n", *confFile, err)
return
}
fmt.Println(conf)
fmt.Printf("No errors while loading: %s\n", *confFile)
}
func cmdDiscover(args []string) {
if len(args) != 1 {
fmt.Printf("Error discover command expects a single argument: URL, but got: %s\n", args)
return
}
url := strings.ToLower(args[0])
feeds, err := internal.DiscoverFeeds(url)
if err != nil {
fmt.Printf("Error discovering feeds at %s: %s\n", url, err)
return
} else if len(feeds) < 1 {
fmt.Println("No feeds found")
} else {
fmt.Println("Possible feeds:")
for i, f := range feeds {
fmt.Printf("#%d\t %s\n", i+1, f)
}
}
}
func cmdRegexp(args []string) {
if len(args) != 2 {
fmt.Printf("Error regexp command expects two arguments: URL, regexp, but got: %s\n", args)
return
}
u, err := url.Parse(args[0])
if err != nil {
fmt.Printf("Error parsing url %s: %s\n", args[0], err)
return
}
gen, err := internal.NewGenerator(internal.Conf{
Settings: internal.Settings{
Timeout: 10,
Jitter: 0,
MaxItems: 30,
},
})
if err != nil {
fmt.Printf("Error creating generator: %s\n", err)
return
}
items, err := gen.FetchItems(internal.Feed{
Url: u.String(),
Parser: internal.Parser{
Rule: args[1],
Host: u.Host,
},
})
if err != nil {
// An error will be returned when the regexp fails to match any items, too
fmt.Printf("Error fetching items from %s: %s\n", u.String(), err)
return
}
fmt.Println("Items found:")
for i, item := range items {
fmt.Printf("#%d\t %s\t (%s)\n", i, item.Title, item.Url)
}
}
func cmdRun(args []string) {
conf, err := internal.LoadConf(*confFile)
if err != nil {
fmt.Printf("Error loading config %s: %s\n", *confFile, err)
return
}
if *confVerbose != conf.Settings.Verbose {
// Weeell if one of 'em is true dey bath gotta be true nao
*confVerbose, conf.Settings.Verbose = true, true
}
debug("Loaded config from: %s", *confFile)
tmpl, err := internal.LoadTemplate(conf.Settings.Template)
if err != nil {
fmt.Printf("Error loading template %s: %s\n", conf.Settings.Template, err)
return
}
feeds, err := fetchFeeds(conf)
if err != nil {
fmt.Printf("Error fetching feeds: %s\n", err)
return
}
if err := writeFiles(conf.Settings.Output, feeds, tmpl); err != nil {
fmt.Printf("Error writing files: %s\n", err)
return
}
if err := removeOldFiles(conf.Settings.Output, conf.Settings.MaxDays); err != nil {
fmt.Printf("Error removing old files: %s\n", err)
return
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
func debug(msg string, args ...interface{}) {
if *confVerbose {
fmt.Printf(msg+"\n", args...)
}
}
func fetchFeeds(conf internal.Conf) (feeds []internal.TemplateFeed, err error) {
gen, err := internal.NewGenerator(conf)
if err != nil {
return
}
for _, feed := range conf.Feeds {
debug("Updating %s (%s)", feed.Title, feed.Url)
items, errFeed := gen.NewItems(feed)
if errFeed != nil {
debug("\tError: %s", errFeed)
} else if len(items) > 0 {
debug("\tItems: %d", len(items))
} else {
debug("No items/errors")
continue
}
feeds = append(feeds, internal.TemplateFeed{
Conf: feed,
Items: items,
Error: errFeed,
})
}
debug("Filter stats: %+v\n", gen.FilterStats())
return
}
func writeFiles(dir string, feeds []internal.TemplateFeed, tmpl *template.Template) error {
v := internal.NewTemplateVars()
v.Feeds = feeds
p := filepath.Join(dir, "news-"+v.Today.Format("2006-01-02")+".html")
if err := internal.WriteTemplate(p, tmpl, v); err != nil {
return err
}
debug("Wrote %s", p)
if err := internal.Symlink(p, filepath.Join(dir, "index.html")); err != nil {
return err
}
return nil
}
var reFile = regexp.MustCompile(`^.*/news-(\d\d\d\d-\d\d-\d\d).html$`)
func removeOldFiles(dir string, maxDays int) error {
if maxDays < 1 {
return nil
}
cutoff := time.Now().AddDate(0, 0, -1*maxDays)
files, err := filepath.Glob(filepath.Join(dir, "news-????-??-??.html"))
if err != nil {
return err
}
for _, f := range files {
s := reFile.FindStringSubmatch(f)
if len(s) != 2 {
continue
}
t, err := time.Parse("2006-01-02", s[1])
if err != nil {
continue
}
if t.After(cutoff) {
continue
}
if err := os.Remove(f); err != nil {
return err
}
debug("Removed %s", f)
}
return nil
}