-
Notifications
You must be signed in to change notification settings - Fork 3
/
rss.go
51 lines (47 loc) · 1.28 KB
/
rss.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
package inputs
import (
"github.com/SlyMarbo/rss"
"github.com/kennygrant/sanitize"
"github.com/hexbotio/hex/models"
"html"
"log"
"time"
)
type Rss struct {
}
func (x Rss) Read(inputMsgs chan<- models.Message, service models.Service) {
defer Recovery(service)
nextMarker := ""
for {
nextMarker = callRss(nextMarker, inputMsgs, service)
time.Sleep(120 * time.Second)
}
}
func callRss(lastMarker string, inputMsgs chan<- models.Message, service models.Service) (nextMarker string) {
var displayOnStart = 0
feed, err := rss.Fetch(service.Config["URL"])
if err != nil {
log.Print(err)
return
}
for i := len(feed.Items) - 1; i >= 0; i-- {
if lastMarker == "" {
lastMarker = feed.Items[displayOnStart].Date.String()
}
item := feed.Items[i]
if item.Date.String() > lastMarker {
title := html.UnescapeString(sanitize.HTML(item.Title))
message := models.MakeMessage(service.Type, service.Name, "", "", title)
message.Inputs["hex.rss.title"] = title
message.Inputs["hex.rss.content"] = html.UnescapeString(sanitize.HTML(item.Content))
message.Inputs["hex.rss.date"] = item.Date.String()
message.Inputs["hex.rss.link"] = item.Link
inputMsgs <- message
if i == 0 {
lastMarker = item.Date.String()
}
}
}
nextMarker = lastMarker
return nextMarker
}