qq_bot/handler/rss/job.go

79 lines
2.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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()
//每个整点和半点执行一次
for {
now := time.Now()
if now.Minute() == 0 || now.Minute() == 30 {
go CheckNewRss()
}
nextAwake := 30 - now.Minute()%30
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)
if items[0].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),
},
},
},
})
db.Model(&group).Update("last_item_hash", items[0].Hash)
}
}
}