-
Notifications
You must be signed in to change notification settings - Fork 2
/
watcher.go
86 lines (72 loc) · 2.06 KB
/
watcher.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
package main
import (
"log"
"time"
)
func watcher(sub Subscription) {
log.Printf("%s Watcher started", bold(sub.Name))
var lastUpdated = time.Time{}
var latestPublished = time.Time{}
refresh := func(t time.Time) {
log.Printf("%s Refreshing...", bold(sub.Name))
feed, err := fetchPttFeed(sub.FeedUrl)
if err != nil {
log.Printf("%s Failed to fetch feed", bold(sub.Name))
return
}
feedUpdated, err := parsePttTime(feed.Updated)
if err != nil {
log.Printf("%s Failed to parse feed's update time", bold(sub.Name))
return
}
if feedUpdated.Equal(lastUpdated) {
// The feed XML has not changed
log.Printf("%s No updates", bold(sub.Name))
return
}
lastUpdated = feedUpdated
log.Printf("%s Updated at %s", bold(sub.Name), feedUpdated.Local())
var notification = NotificationMessage{Subscription: sub}
size := len(feed.EntryList)
for i := size - 1; i >= 0; i-- {
var entry = feed.EntryList[i]
// Try to parse the publish time of entry
published, err := parsePttTime(entry.Published)
if err != nil {
log.Fatal("%s Error while parsing entry's publish time", bold(sub.Name))
return
}
// This entry has been traversed
if !published.After(latestPublished) {
continue
}
latestPublished = published
// Filtering
if filteredAny(entry.Title, sub.Filters) {
// Add this entry to notification
item := NotificationMessageItem{entry.Link.Href, entry.Title}
notification.Items = append(notification.Items, item)
log.Printf("%s Found new entry: %s", bold(sub.Name), entry.Title)
continue
}
}
// Send notification if any interesting post was found
if len(notification.Items) > 0 {
if contains(sub.NotifyMethods, "slack") {
nSlackChan <- notification
}
if contains(sub.NotifyMethods, "line") {
nLineChan <- notification
}
}
}
// Refresh when the watcher started, and then every ticks
refresh(time.Now())
refreshTime := time.Duration(sub.RefreshTime)
ticker := time.NewTicker(refreshTime * time.Second)
go func() {
for t := range ticker.C {
refresh(t)
}
}()
}