This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
rss2hook.go
309 lines (252 loc) · 6.49 KB
/
rss2hook.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// rss2hook is a simple utility which will make HTTP POST
// requests to remote web-hooks when new items appear in an RSS feed.
//
// Steve
//
package main
import (
"bufio"
"bytes"
"crypto/sha1"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"os/signal"
"regexp"
"strings"
"syscall"
"time"
"github.com/mmcdole/gofeed"
"github.com/robfig/cron"
)
// RSSEntry describes a single RSS feed and the corresponding hook
// to POST to.
type RSSEntry struct {
// The URL of the RSS/Atom feed
feed string
// The end-point to make the webhook request to.
hook string
}
// Loaded contains the loaded feeds + hooks, as read from the specified
// configuration file
var Loaded []RSSEntry
// Timeout is the (global) timeout we use when loading remote RSS
// feeds.
var Timeout time.Duration
// loadConfig loads the named configuration file and populates our
// `Loaded` list of RSS-feeds & Webhook addresses
func loadConfig(filename string) {
file, err := os.Open(filename)
if err != nil {
fmt.Printf("Error opening %s - %s\n", filename, err.Error())
return
}
defer file.Close()
//
// Process it line by line.
//
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// Get the next line, and strip leading/trailing space
tmp := scanner.Text()
tmp = strings.TrimSpace(tmp)
//
// Skip lines that begin with a comment.
//
if (tmp != "") && (!strings.HasPrefix(tmp, "#")) {
//
// Otherwise find the feed + post-point
//
parser := regexp.MustCompile("^(.+?)=([^=].+)")
match := parser.FindStringSubmatch(tmp)
//
// OK we found a suitable entry.
//
if len(match) == 3 {
feed := strings.TrimSpace(match[1])
hook := strings.TrimSpace(match[2])
// Append the new entry to our list
entry := RSSEntry{feed: feed, hook: hook}
Loaded = append(Loaded, entry)
}
}
}
}
// fetchFeed fetches the contents of the specified URL.
func fetchFeed(url string) (string, error) {
// Ensure we setup a timeout for our fetch
client := &http.Client{Timeout: Timeout}
// We'll only make a GET request
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", err
}
// We ensure we identify ourself.
req.Header.Set("User-Agent", "rss2email (https://github.com/skx/rss2email)")
// Make the request
resp, err := client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
// Read the body returned
output, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(output), nil
}
// isNew returns TRUE if this feed-item hasn't been notified about
// previously.
func isNew(parent string, item *gofeed.Item) bool {
hasher := sha1.New()
hasher.Write([]byte(parent))
hasher.Write([]byte(item.GUID))
hashBytes := hasher.Sum(nil)
// Hexadecimal conversion
hexSha1 := hex.EncodeToString(hashBytes)
if _, err := os.Stat(os.Getenv("HOME") + "/.rss2hook/seen/" + hexSha1); os.IsNotExist(err) {
return true
}
return false
}
// recordSeen ensures that we won't re-announce a given feed-item.
func recordSeen(parent string, item *gofeed.Item) {
hasher := sha1.New()
hasher.Write([]byte(parent))
hasher.Write([]byte(item.GUID))
hashBytes := hasher.Sum(nil)
// Hexadecimal conversion
hexSha1 := hex.EncodeToString(hashBytes)
dir := os.Getenv("HOME") + "/.rss2hook/seen"
os.MkdirAll(dir, os.ModePerm)
_ = ioutil.WriteFile(dir+"/"+hexSha1, []byte(item.Link), 0644)
}
// checkFeeds is our work-horse.
//
// For each available feed it looks for new entries, and when founds
// triggers `notify` upon the resulting entry
func checkFeeds() {
//
// For each thing we're monitoring
//
for _, monitor := range Loaded {
// Fetch the feed-contents
content, err := fetchFeed(monitor.feed)
if err != nil {
fmt.Printf("Error fetching %s - %s\n",
monitor.feed, err.Error())
continue
}
// Now parse the feed contents into a set of items
fp := gofeed.NewParser()
feed, err := fp.ParseString(content)
if err != nil {
fmt.Printf("Error parsing %s contents: %s\n", monitor.feed, err.Error())
continue
}
// For each entry in the feed
for _, i := range feed.Items {
// If we've not already notified about this one.
if isNew(monitor.feed, i) {
// Trigger the notification
err := notify(monitor.hook, i)
// and if that notification succeeded
// then record this item as having been
// processed successfully.
if err == nil {
recordSeen(monitor.feed, i)
}
}
}
}
}
// notify actually submits the specified item to the remote webhook.
//
// The RSS-item is submitted as a JSON-object.
func notify(hook string, item *gofeed.Item) error {
// We'll post the item as a JSON object.
// So first of all encode it.
jsonValue, err := json.Marshal(item)
if err != nil {
fmt.Printf("notify: Failed to encode JSON:%s\n", err.Error())
return err
}
//
// Post to the specified hook URL.
//
res, err := http.Post(hook,
"application/json",
bytes.NewBuffer(jsonValue))
if err != nil {
fmt.Printf("notify: Failed to POST to %s - %s\n",
hook, err.Error())
return err
}
//
// OK now we've submitted the post.
//
// We should retrieve the status-code + body, if the status-code
// is "odd" then we'll show them.
//
defer res.Body.Close()
_, err = ioutil.ReadAll(res.Body)
if err != nil {
return err
}
status := res.StatusCode
if status != 200 {
fmt.Printf("notify: Warning - Status code was not 200: %d\n", status)
}
return nil
}
// main is our entry-point
func main() {
// Parse the command-line flags
config := flag.String("config", "", "The path to the configuration-file to read")
timeout := flag.Duration("timeout", 5*time.Second, "The timeout used for fetching the remote feeds")
flag.Parse()
// Setup the default timeout.
Timeout = *timeout
if *config == "" {
fmt.Printf("Please specify a configuration-file to read\n")
return
}
//
// Load the configuration file
//
loadConfig(*config)
//
// Show the things we're monitoring
//
for _, ent := range Loaded {
fmt.Printf("Monitoring feed %s\nPosting to %s\n\n",
ent.feed, ent.hook)
}
//
// Make the initial scan of feeds immediately to avoid waiting too
// long for the first time.
//
checkFeeds()
//
// Now repeat that every five minutes.
//
c := cron.New()
c.AddFunc("@every 5m", func() { checkFeeds() })
c.Start()
//
// Now we can loop waiting to be terminated via ctrl-c, etc.
//
sigs := make(chan os.Signal, 1)
done := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
done <- true
}()
<-done
}