-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
99 lines (89 loc) · 2.84 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
package main
import (
"flag"
"fmt"
"html/template"
"net/http"
"os"
"os/signal"
"time"
"github.com/sirupsen/logrus"
"github.com/ww9/news/feed"
)
var flagDir = flag.String("dir", "", "directory to store html files. By default ./news is used and created if necessary")
var flagTimeout = flag.Int("timeout", 10, "timeout in seconds when fetching feeds")
var flagUpdateInterval = flag.Int("wait", 10, "minutes to wait between updates")
var flagItemsPerPage = flag.Int("items", 500, "number of items per page.html file. A new page.html file is created whenever index.html contains 2x that number")
var flagVerbose = flag.Bool("verbose", false, "verbose mode outputs extra info when enabled")
var flagTemplateFile = flag.String("template", "", "custom Go html/template file to use when generating .html files. See `news/feed/template.go`")
var flagOPMLFile = flag.String("opml", "", "path to OPML file containing feed URLS to be imported. Existing feed URLs are ovewritten, not duplicated")
var flagMinDomainRequestInterval = flag.Int("noflood", 30, "minium seconds between calls to same domain to avoid flooding")
func main() {
flag.Parse()
*flagTimeout = minMax(*flagTimeout, 1, 60)
*flagItemsPerPage = minMax(*flagItemsPerPage, 2, 500)
*flagUpdateInterval = minMax(*flagUpdateInterval, 1, 24*60)
*flagMinDomainRequestInterval = minMax(*flagMinDomainRequestInterval, 10, 24*60*60)
log := logrus.New()
log.SetLevel(logrus.InfoLevel)
if *flagVerbose {
log.SetLevel(logrus.DebugLevel)
}
if *flagTemplateFile != "" {
tpl, err := template.ParseFiles(*flagTemplateFile)
if err != nil {
log.Fatalf("Could not load custom template file: %s", err)
}
feed.Tpl = tpl
}
agg, err := feed.NewWithCustom(
log,
*flagDir,
*flagItemsPerPage,
feed.MakeURLFetcher(
log,
time.Second*time.Duration(*flagMinDomainRequestInterval),
&http.Client{Timeout: time.Second * time.Duration(*flagTimeout)},
),
)
if err != nil {
log.Fatalln(err)
}
if *flagOPMLFile != "" {
importedFeeds, err := agg.ImportOPMLFile(*flagOPMLFile)
if err != nil {
log.Fatalf("Could not import OPML file: %s", err)
} else {
log.Printf("Successfully imported %d feeds from OPML file.", importedFeeds)
}
}
go func() {
for {
log.Infof("Fetching news from %d feed sources...", len(agg.Feeds))
if err := agg.Update(); err != nil {
log.Fatalln(err)
}
log.Infof("Done. Waiting %d minutes for next update...", *flagUpdateInterval)
time.Sleep(time.Duration(*flagUpdateInterval) * time.Minute)
}
}()
pressCTRLCToExit()
fmt.Println("Bye :)")
}
func pressCTRLCToExit() {
exitCh := make(chan os.Signal)
signalCh := make(chan os.Signal)
signal.Notify(signalCh, os.Interrupt)
go func() {
exitCh <- (<-signalCh)
}()
<-exitCh
}
func minMax(value int, min int, max int) int {
if value < min {
return min
} else if value > max {
return max
}
return value
}