-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
113 lines (100 loc) · 2.61 KB
/
main.go
File metadata and controls
113 lines (100 loc) · 2.61 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
"net/url"
"os"
"strings"
"time"
"github.com/mmcdole/gofeed"
)
var historyPath = "./histories/"
type DiscordPayload struct {
Content string `json:"content"`
}
func fetchRssItems(rssUrl string) ([]*gofeed.Item, error) {
fp := gofeed.NewParser()
feed, err := fp.ParseURL(rssUrl)
if err != nil {
log.Printf("Warning: Failed to parse RSS Url, %v", err)
return nil, err
}
if len(feed.Items) == 0 {
return nil, nil
}
return feed.Items, nil
}
func isNewItem(item gofeed.Item, fname string) (bool, error) {
bytes, err := os.ReadFile(historyPath + fname)
if err != nil {
log.Printf("Error: Failed to read %v, %v", historyPath+fname, err)
return false, err
}
// 新着コンテンツ
if string(bytes) != item.Link {
return true, nil
} else {
return false, nil
}
}
func SendToDiscord(item *gofeed.Item, webHookUrls []string) error {
author := ""
if item.Authors != nil {
author = "**" + item.Authors[0].Name + "**"
}
content := fmt.Sprintf("new post from %s: %s", author, item.Link)
payload := DiscordPayload{Content: content}
b, _ := json.Marshal(payload)
for _, url := range webHookUrls {
resp, err := http.Post(url, "application/json", bytes.NewBuffer(b))
if err != nil {
log.Printf("Warning: Failed to fetch RSS, %v", err)
}
defer resp.Body.Close()
}
return nil
}
func main() {
config := ParseConfig()
log.Println("Info: Starting rss2discord...")
_, err := os.Stat("./histories")
if err != nil {
os.Mkdir("./histories", os.ModePerm)
}
for {
var items = map[string][]*gofeed.Item{}
for _, rssUrl := range config.RssSourceUrls {
url, _ := url.Parse(rssUrl)
hostname := url.Host
path := url.Path
fname := hostname + strings.ReplaceAll(path, "/", "-")
fname = strings.ReplaceAll(fname, ".", "-")
_, err := os.Stat(historyPath + fname)
// Not exists
if err != nil {
os.WriteFile(historyPath+fname, []byte(""), 0666)
}
items[rssUrl], err = fetchRssItems(rssUrl)
if err != nil || items[rssUrl] != nil {
log.Printf("Warning: Failed to fetch RSS, %v", err)
continue
}
latestItem := items[rssUrl][0]
flag, err := isNewItem(*latestItem, fname)
if err != nil {
log.Printf("Warning: Failed to check item, %v", err)
}
if flag {
log.Printf("Info: New post coming!: %v %v", latestItem.Content, latestItem.Link)
SendToDiscord(latestItem, config.WebhookUrls)
os.WriteFile(historyPath+fname, []byte(latestItem.Link), 0666)
} else {
log.Println("Info: No new post, skipping.")
}
}
time.Sleep(time.Duration(config.FetchIntervalMin * int(time.Minute)))
}
}