-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
273 lines (255 loc) · 6.76 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
267
268
269
270
271
272
273
package main
import (
"errors"
"flag"
"fmt"
"github.com/joho/godotenv"
"github.com/mmcdole/gofeed"
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"path/filepath"
"plugin"
"regexp"
"time"
)
type FeedFilter struct {
Element string `yaml:"element"`
Matches string `yaml:"matches"`
}
type Feed struct {
Name string `yaml:"name"`
Url string `yaml:"url"`
Interval int `yaml:"interval"`
Filters []struct {
Include FeedFilter
Exclude FeedFilter
} `yaml:"filters"`
Notifiers []map[string]map[string]string `yaml:"notifiers,omitempty"`
}
type FeedActive struct {
Feed
lastPulled int64
lastItemTitle string
}
type MonitorSchema struct {
Monitor []struct {
Feed Feed `yaml:"feed"`
} `yaml:"monitor"`
}
type Notification struct {
Send func(*gofeed.Item)
}
type Notifier struct {
Name string
Config string
}
func main() {
fileName := flag.String("config", "", "config.yml")
mode := flag.String("mode", "dev", "dev or prod")
flag.Parse()
fmt.Println("Reading config file: ", *fileName)
if *fileName == "" {
fmt.Println("No config file specified")
return
}
if *mode != "prod" {
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Error loading .env file")
}
}
activeFeeds, err := parseConfig(*fileName)
if err != nil {
fmt.Println("Error parsing config file: ", err)
os.Exit(1)
}
plugins, err := loadPlugins()
if err != nil {
fmt.Println("Error loading plugins: ", err)
os.Exit(1)
}
runService(plugins, activeFeeds)
}
// Loads all plugins in the plugins directory.
// Set OS environment variable 'FEED_MONITOR_PLUGIN_DIR' to the directory containing the plugins, or default to "./plugins"
func loadPlugins() (map[string]plugin.Symbol, error) {
fmt.Println("Loading plugins...")
var pluginList []string
pluginMap := make(map[string]plugin.Symbol)
pluginPath := os.Getenv("FEED_MONITOR_PLUGIN_DIR")
if pluginPath == "" {
pluginPath = "./plugins"
}
err := filepath.Walk(pluginPath, func(path string, info os.FileInfo, err error) error {
if filepath.Ext(path) == ".so" {
pluginList = append(pluginList, path)
}
return nil
})
if err != nil {
fmt.Println("Unable to access the plugin directory: ", err)
return nil, err
}
pluginLen := len(pluginList)
fmt.Println("Found ", pluginLen, " plugins")
for _, pluginPath := range pluginList {
fmt.Println("Loading plugin: ", pluginPath)
plug, err := plugin.Open(pluginPath)
if err != nil {
fmt.Println("Error loading plugin: ", err)
}
sendNotification, err := plug.Lookup("Send")
if err != nil {
fmt.Println("Error looking up Send function in plugin")
return nil, err
}
pluginMap[pluginPath] = sendNotification
}
return pluginMap, nil
}
// The main service loop.
// Checks the feed for new items, and sends notifications if there are any.
func runService(plugins map[string]plugin.Symbol, activeFeeds []FeedActive) {
fmt.Println("Starting service")
ActiveFeeds := activeFeeds
for {
for i, feed := range ActiveFeeds {
if time.Now().Unix()-feed.lastPulled > int64(feed.Interval) {
fmt.Println("Last updated: ", time.Unix(feed.lastPulled, 0))
matches, err := checkFeed(feed.Feed, feed.lastItemTitle)
if err != nil {
fmt.Println("Error checking feed: ", err)
continue
}
if len(matches) > 0 {
ActiveFeeds[i].lastPulled = time.Now().Unix()
ActiveFeeds[i].lastItemTitle = matches[len(matches)-1].Title
}
for _, match := range matches {
sendFeedNotifications(feed.Feed, match, plugins)
}
}
}
}
}
// Parses the YAML config file and returns a list of Feeds.
func parseConfig(fileName string) ([]FeedActive, error) {
var config MonitorSchema
configFile, err := ioutil.ReadFile(fileName)
var activeFeeds []FeedActive
if err != nil {
fmt.Println("Error reading config file: ", err)
return nil, err
}
err = yaml.Unmarshal(configFile, &config)
if err != nil {
fmt.Println("Error parsing config file: ", err)
return nil, err
}
for _, feed := range config.Monitor {
activeFeeds = append(activeFeeds, FeedActive{feed.Feed, 0, ""})
}
return activeFeeds, nil
}
// Checks a feed and returns any new items
func checkFeed(feed Feed, lastItem string) ([]*gofeed.Item, error) {
var fp = gofeed.NewParser()
parsedFeed, err := fp.ParseURL(feed.Url)
fmt.Println("Checking feed: ", feed.Name)
var newItems []*gofeed.Item
var matchedItems []*gofeed.Item
if err != nil {
fmt.Println("Error parsing feed")
return nil, err
}
fmt.Println("Feed length: ", len(parsedFeed.Items))
for _, item := range parsedFeed.Items {
fmt.Println("Checking item: ", item.Title)
if item.Title == lastItem {
fmt.Println("Item already processed")
break
} else {
fmt.Println("Adding item to new items")
newItems = append(newItems, item)
}
}
for _, item := range newItems {
isMatchItem := true
for _, filter := range feed.Filters {
if filter.Include.Element != "" {
if !checkFilter(item, filter.Include, true) {
isMatchItem = false
continue
}
}
if filter.Exclude.Element != "" {
if checkFilter(item, filter.Exclude, false) {
isMatchItem = false
continue
}
}
}
if isMatchItem {
matchedItems = append(matchedItems, item)
}
}
return matchedItems, nil
}
// Send a notification for a feed item via all supplied "Notifiers"
func sendFeedNotifications(feed Feed, item *gofeed.Item, plugins map[string]plugin.Symbol) (string, error) {
if len(plugins) == 0 {
return "", errors.New("no plugins loaded")
}
if len(feed.Notifiers) == 0 {
return "", errors.New("no notifiers configured")
}
for _, y := range feed.Notifiers {
for k, v := range y {
selectedPlugin := getPlugin(k)
if plugins[selectedPlugin] != nil {
err := plugins[selectedPlugin].(func(*gofeed.Item, string, map[string]string) error)(item, feed.Name, v)
if err != nil {
fmt.Println("Error sending notification: ", err)
return "", err
}
}
}
}
return "ok", nil
}
// Returns the plugin path from the name.
func getPlugin(pluginName string) string {
pluginPath := os.Getenv("FEED_MONITOR_PLUGIN_DIR")
if pluginPath == "" {
pluginPath = "plugins"
}
return pluginPath + "/" + pluginName + "/" + pluginName + ".so"
}
// Checks an item against a filter and returns true if it matches.
func checkFilter(item *gofeed.Item, filter FeedFilter, inclusive bool) bool {
var elementString string
re := regexp.MustCompile(filter.Matches)
switch filter.Element {
case "title":
elementString = item.Title
case "link":
elementString = item.Link
case "published":
elementString = item.Published
case "content":
elementString = item.Content
case "author":
elementString = item.Authors[0].Name
case "Updated":
elementString = item.Updated
case "description":
elementString = item.Description
}
matched := re.Match([]byte(elementString))
if inclusive {
return matched
} else {
return !matched
}
}