88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
package rss
|
||
|
||
import (
|
||
"fmt"
|
||
"log"
|
||
"time"
|
||
|
||
"git.lxtend.com/lixiangwuxian/qqbot/action"
|
||
"git.lxtend.com/lixiangwuxian/qqbot/model"
|
||
"git.lxtend.com/lixiangwuxian/qqbot/qq_message"
|
||
"git.lxtend.com/lixiangwuxian/qqbot/sqlite3"
|
||
"git.lxtend.com/lixiangwuxian/qqbot/util"
|
||
)
|
||
|
||
func init() {
|
||
go CheckRssJob()
|
||
}
|
||
|
||
/*
|
||
定时检测最新的rss数据是否有更新,若有则向对应群发送消息
|
||
取出所有订阅信息,并根据订阅信息中的feed_id获取对应的rss源信息,并根据rss源信息中的url获取最新的rss数据
|
||
比较最新的rss数据与订阅信息中的last_item_hash,若有更新则向对应群发送消息,并更新订阅信息中的last_item_hash
|
||
*/
|
||
func CheckRssJob() {
|
||
defer util.ReportPanicToDev()
|
||
//每个整5分钟执行一次
|
||
for {
|
||
now := time.Now()
|
||
if now.Minute()%5 == 0 {
|
||
go CheckNewRss()
|
||
//inhibit 2 minutes
|
||
time.Sleep(time.Minute * 2)
|
||
}
|
||
nextAwake := 5 - now.Minute()%5
|
||
if nextAwake == 0 {
|
||
nextAwake = 5
|
||
}
|
||
time.Sleep(time.Minute * time.Duration(nextAwake))
|
||
}
|
||
}
|
||
|
||
func CheckNewRss() {
|
||
defer util.ReportPanicToDev()
|
||
db := sqlite3.GetGormDB()
|
||
//查询所有群组订阅数据
|
||
var groups []RssSubscribe
|
||
db.Find(&groups)
|
||
for _, group := range groups {
|
||
//根据feed_id查询对应的rss源信息
|
||
var feed RssFeed
|
||
db.Where("id = ?", group.FeedID).First(&feed)
|
||
if feed.ID == 0 {
|
||
continue
|
||
}
|
||
log.Println("groupID:", group.GroupID, "feed.FeedURL:", feed.FeedURL)
|
||
//获取最新的rss数据
|
||
title, items, err := ParseFeed(feed.FeedURL)
|
||
if err != nil {
|
||
continue
|
||
}
|
||
//比较最新的rss数据与订阅信息中的last_item_hash,若有更新则向对应群发送消息,并更新订阅信息中的last_item_hash
|
||
log.Println("localHash:", group.LastItemHash, "remoteHash:", items[0].Hash)
|
||
db.Model(&group).Update("last_item_hash", items[0].Hash)
|
||
for _, item := range items {
|
||
if item.Hash != group.LastItemHash {
|
||
action.ActionManager.SendMsg(&model.Reply{
|
||
FromMsg: model.Message{
|
||
GroupInfo: model.GroupInfo{
|
||
GroupId: int64(group.GroupID),
|
||
IsGroupMsg: true,
|
||
},
|
||
},
|
||
ReplyMsg: []qq_message.QQMessage{
|
||
&qq_message.TextMessage{
|
||
Type: qq_message.TypeText,
|
||
Data: qq_message.TextMessageData{
|
||
Text: fmt.Sprintf("您订阅的%s发布了新的文章: %s\n%s", title, items[0].Title, items[0].Link),
|
||
},
|
||
},
|
||
},
|
||
})
|
||
} else {
|
||
break
|
||
}
|
||
}
|
||
}
|
||
}
|