-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtl2t.go
46 lines (41 loc) · 1.1 KB
/
tl2t.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
package main
import (
"flag"
"fmt"
"log"
"os"
"regexp"
"strings"
"github.com/mmcdole/gofeed"
)
func main() {
logsDir := "./logs/"
defaultCategoriesRegexp := ("Foo|Bar")
defaultFeedURL := "https://localhost/invalid"
flagFeedURL := flag.String("feedurl", defaultFeedURL, "Default feed URL")
flagCategoriesRegexp := flag.String("categoriesregexp", defaultCategoriesRegexp, "Default categories regexp")
flag.Parse()
feedURL := *flagFeedURL
stringCategoriesRegexp := "^(" + *flagCategoriesRegexp + ")$"
categoriesRegexp := regexp.MustCompile(stringCategoriesRegexp)
fp := gofeed.NewParser()
feed, err := fp.ParseURL(feedURL)
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
for _, item := range feed.Items {
if categoriesRegexp.MatchString(item.Categories[0]) {
outputFile := logsDir + strings.ReplaceAll(strings.ToLower(item.Categories[0]), " ", "_")
f, err := os.OpenFile(outputFile,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
if _, err := f.WriteString(item.Title + "\n"); err != nil {
log.Println(err)
}
f.Close()
}
}
}