-
Notifications
You must be signed in to change notification settings - Fork 57
/
main.go
106 lines (89 loc) · 2.2 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
package main
import (
"encoding/json"
"flag"
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/mmcdole/gofeed"
"os"
"time"
)
// 基础环境配置
var BotToken *string
var ChannelID *int64
func TokenValid() {
if *BotToken == "" || *ChannelID == 0 {
panic("BotToken && ChannelID cannot be empty")
}
}
func init() {
BotToken = flag.String("tg_bot", "", "Telegram bot token")
ChannelID = flag.Int64("tg_channel", 0, "Telegram channel id")
flag.Parse()
TokenValid()
GetRssInfo()
}
// RSS 构成阶段
type RSSInfos struct {
RssInfo []RssInfo `json:"rss_info"`
}
type RssInfo struct {
Title string `json:"title"`
Url string `json:"url"`
FullContent bool `json:"full_content"`
}
var RssInfos = RSSInfos{nil}
// 从 配置文件中获取 rss 链接
// 根据 rss 链接获取更新
func GetRssInfo() {
rssFile, err := os.Open("./rss.json")
if err != nil {
panic(err)
}
err = json.NewDecoder(rssFile).Decode(&RssInfos)
if err != nil {
panic(err)
}
}
// 根据时间筛选昨天一整天的文章
func GetPosts() {
var msg = make([]string, 0)
for _, info := range RssInfos.RssInfo {
msg = append(msg, GetPostInfo(info)...)
}
PushPost(msg)
}
func GetPostInfo(rss RssInfo) []string {
var msg = make([]string, 0)
now := time.Now().UTC()
startTime := now.Add(-4 * time.Hour)
start := time.Date(startTime.Year(), startTime.Month(), startTime.Day(), startTime.Hour(), 0, 0, 0, now.Location()).Unix()
end := time.Date(now.Year(), now.Month(), now.Day(), now.Hour(), 0, 0, 0, now.Location()).Unix()
fp := gofeed.NewParser()
feed, err := fp.ParseURL(rss.Url)
if err != nil {
fmt.Print(err.Error())
} else {
for _, item := range feed.Items {
if item.PublishedParsed != nil && item.PublishedParsed.Unix() >= start && item.PublishedParsed.Unix() < end {
msgItem := fmt.Sprintln(item.Title, item.Link)
msg = append(msg, msgItem)
}
}
}
return msg
}
// 从配置文件获取推送方式
// 使用对应的推送渠道推送文章
func PushPost(msg []string) {
bot, err := tgbotapi.NewBotAPI(*BotToken)
if err != nil {
panic(err)
}
for _, s := range msg {
_, _ = bot.Send(tgbotapi.NewMessage(*ChannelID, s))
}
}
func main() {
GetPosts()
}