-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
266 lines (222 loc) · 5.8 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
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
package main
import (
"bytes"
"encoding/xml"
"flag"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2"
)
type Rss struct {
XMLName xml.Name `xml:"rss"`
Channel Channel `xml:"channel"`
}
type Channel struct {
Title string `xml:"title"`
Item []Item `xml:"item"`
}
type Item struct {
Title string `xml:"title"`
Link string `xml:"link"`
Published string `xml:"pubDate"`
}
type Atom struct {
XMLName xml.Name `xml:"feed"`
Title string `xml:"title"`
Entries []Entry `xml:"entry"`
}
type Entry struct {
Title string `xml:"title"`
Link Link `xml:"link"`
Published string `xml:"published"`
}
type Link struct {
Href string `xml:"href,attr"`
}
type Feed struct {
URL string `yaml:"url"`
NtfyTopic string `yaml:"ntfy_topic"`
LastUpdate time.Time
}
type Config struct {
Feeds []Feed `yaml:"feeds"`
}
func main() {
log.SetFormatter(&log.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05",
})
var intervalFlag string
var configFile string
flag.StringVar(&intervalFlag, "interval", "10m", "Check interval (e.g., 30s, 20m, 2h)")
flag.StringVar(&configFile, "config", "", "Path to config file")
flag.Parse()
if intervalFlag == "" || configFile == "" {
fmt.Fprintf(os.Stderr, "Usage: %s -interval <duration> [-config <path>]\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\nExample:\n")
fmt.Fprintf(os.Stderr, " %s -interval 10m -config /path/to/feeds.yaml\n", os.Args[0])
os.Exit(1)
}
interval, err := time.ParseDuration(intervalFlag)
if err != nil {
log.Fatalf("Invalid interval format: %v", err)
}
log.Info("Reading config file")
config, err := loadConfig(configFile)
if err != nil {
log.Fatalf("Error loading config: %v", err)
}
log.Infof("Using check interval: %v", interval)
client := &http.Client{
Timeout: time.Second * 30,
}
for {
processFeedsAsync(config.Feeds, client)
log.Infof("Sleeping for %v", interval)
time.Sleep(interval)
}
}
func processFeedsAsync(feeds []Feed, client *http.Client) {
var wg sync.WaitGroup
for i := range feeds {
wg.Add(1)
go func(feed *Feed) {
defer wg.Done()
processFeed(feed, client)
}(&feeds[i])
}
wg.Wait()
}
func loadConfig(filename string) (*Config, error) {
filename = expandTilde(filename)
data, err := os.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("error reading config file: %w", err)
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, fmt.Errorf("error parsing config file: %w", err)
}
now := time.Now()
for i := range config.Feeds {
config.Feeds[i].LastUpdate = now
}
return &config, nil
}
func expandTilde(path string) string {
if strings.HasPrefix(path, "~") {
home, err := os.UserHomeDir()
if err != nil {
return path
}
return filepath.Join(home, path[1:])
}
return path
}
func processFeed(feed *Feed, client *http.Client) {
logger := log.WithFields(log.Fields{"feed": feed.URL})
logger.Infof("Checking feed")
req, err := http.NewRequest("GET", feed.URL, nil)
if err != nil {
logger.Errorf("Error creating request: %v", err)
return
}
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
logger.Errorf("Error fetching feed: %v", err)
return
}
defer resp.Body.Close()
logger.Infof("Response status code: %d", resp.StatusCode)
body, err := io.ReadAll(resp.Body)
if err != nil {
logger.Errorf("Error reading feed: %v", err)
return
}
var rss Rss
var atom Atom
var isAtom bool
if err := xml.Unmarshal(body, &rss); err != nil {
if err := xml.Unmarshal(body, &atom); err != nil {
logger.Errorf("Error parsing feed: %v", err)
return
}
isAtom = true
}
if isAtom {
logger.Infof("Processing as Atom feed")
processAtomFeed(feed, atom, logger)
} else {
logger.Info("Processing as RSS feed")
processRSSFeed(feed, rss, logger)
}
}
func processRSSFeed(feed *Feed, rss Rss, logger *log.Entry) {
for _, item := range rss.Channel.Item {
published, err := parseDate(item.Published)
if err != nil {
logger.Errorf("Error parsing date for item in feed: %v", err)
continue
}
if published.After(feed.LastUpdate) {
feed.LastUpdate = published
sendNotification(feed.NtfyTopic, item.Title, item.Link, logger)
}
}
}
func processAtomFeed(feed *Feed, atom Atom, logger *log.Entry) {
for _, entry := range atom.Entries {
published, err := parseDate(entry.Published)
if err != nil {
logger.Errorf("Error parsing date for entry in feed: %v", err)
continue
}
if published.After(feed.LastUpdate) {
feed.LastUpdate = published
logger.Infof("Updated last published timestamp for: %s", feed.LastUpdate)
sendNotification(feed.NtfyTopic, entry.Title, entry.Link.Href, logger)
}
}
}
func parseDate(dateString string) (time.Time, error) {
formats := []string{
time.RFC1123Z,
time.RFC1123,
time.RFC822,
time.RFC822Z,
"2006-01-02T15:04:05Z07:00",
"2006-01-02 15:04:05",
"2006-01-02T15:04:05.999999Z07:00",
"Mon, 2 Jan 2006 15:04:05 -0700",
}
for _, format := range formats {
if t, err := time.Parse(format, dateString); err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("unable to parse date: %s", dateString)
}
func sendNotification(topic, title, link string, logger *log.Entry) {
message := fmt.Sprintf("%s\n\n%s", title, link)
resp, err := http.Post(topic, "text/plain", bytes.NewBuffer([]byte(message)))
if err != nil {
logger.Errorf("Error sending notification: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
logger.Errorf("Failed to send notification: %s", resp.Status)
} else {
logger.Infof("Notification sent:\n\n%s", message)
}
}