-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
103 lines (89 loc) · 1.82 KB
/
config.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"strings"
"time"
"github.com/BurntSushi/toml"
)
type (
Config struct {
Channels []string
ChannelsUrl string `toml:"channels_url"`
IncludeFilter []string `toml:"include_filter"`
ExcludeFilter []string `toml:"exclude_filter"`
Mastodon Mastodon
}
Mastodon struct {
Domain string
Token string
}
Channel struct {
Url string
Name string
LastChecked time.Time
Events map[string]Event
}
Event struct {
Title string
Url string
StartsAt time.Time
}
)
var cfg Config
var channels []Channel
func InitConfig() {
if _, err := toml.DecodeFile("config.toml", &cfg); err != nil {
log.Fatal(err)
}
if cfg.ChannelsUrl == "" {
GetChannelsFromConfig()
} else {
GetChannelsFromUrl()
}
log.Println("Loaded " + fmt.Sprint(len(channels)) + " channels.")
}
func GetChannelsFromConfig() {
for _, v := range cfg.Channels {
f := ParseRss(v)
if f == nil {
log.Printf("RSS feed %v no longer exist, skipping.\n", v)
continue
}
new := Channel{
Url: v,
Name: f.Author.Name,
LastChecked: time.Now(),
Events: make(map[string]Event),
}
channels = append(channels, new)
}
}
func GetChannelsFromUrl() {
resp, err := http.Get(cfg.ChannelsUrl)
if err != nil {
log.Fatalf("Failed to get channel list source: %v\n", err)
}
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
c := strings.Split(string(b), "\n")
for _, v := range c {
if !(strings.HasPrefix(v, "http")) {
continue
}
f := ParseRss(v)
if f == nil {
log.Printf("RSS feed %v no longer exist, skipping.\n", v)
continue
}
new := Channel{
Url: v,
Name: f.Author.Name,
LastChecked: time.Now(),
Events: make(map[string]Event),
}
channels = append(channels, new)
}
}