go 定时任务,解析百度新闻,推送到钉钉群里
package main
import (
"bytes"
"fmt"
"github.com/PuerkitoBio/goquery"
"github.com/astaxie/beego/toolbox"
"github.com/go-redis/redis"
"log"
"net/http"
)
var (
redisClient *redis.Client //redis 缓存
dingdingURL ="https://oapi.dingtalk.com/robot/send?access_token=3bae30973664b5f2dac0e144c5317af02ed748a5c5b4b4032b51733ba636f0a1"
//百度新闻搜索关键字URL
baiduNewsUrlWithSearchKeyword = "http://news.baidu.com/ns?cl=2&rn=20&tn=news&word=%E7%89%A9%E8%81%94%E7%BD%91"
)
const (
newsFeed = "news_feed"//爬取到的百度新闻redis key
newsPost = "news_post"//已发送的百度新闻redis key
newsList = "iot_news" //储存了的百度新闻redis key
)
func init(){
redisClient =redis.NewClient(&redis.Options{
Addr:"127.0.0.1:6379",
Password:"",
DB:0,
})
}
func newsBot() error {
fmt.Println("开始读取")
// 获取html doc
doc, err := goquery.NewDocument(baiduNewsUrlWithSearchKeyword)
if err != nil {
return nil
}
//使用redis pipeline 减少redis连接数
pipe := redisClient.Pipeline()
// 使用selector xpath 语法获取有用信息
// 储存新闻到redis中 newsList
// 储存新闻ur到redis-set 建newfeed 为以后是用sdiff 找出没有发送的新闻
doc.Find("div.result").Each(func(i int, s *goquery.Selection) {
// For each item found, get the band and title
URL, _ := s.Find("h3 > a").Attr("href")
Source := s.Find("p.c-author").Text()
Title := s.Find("h3 > a").Text()
markdown := fmt.Sprintf("- [%s](%s) _%s_", Title, URL, Source)
pipe.HSet(newsList, URL, markdown)
pipe.SAdd(newsFeed, URL)
})
pipe.Exec()
//执行redis pipeline
//使用redis sdiff找出没有发送的新闻url
unSendNewsUrls := redisClient.SDiff(newsFeed, newsPost).Val()
//新闻按dingding文档markdonw 规范拼接
content := ""
for _, url := range unSendNewsUrls {
md := redisClient.HGet(newsList, url).Val()
content = content + " \n " + md
//记录已发送新闻的url地址
pipe.SAdd(newsPost, url)
}
pipe.Exec()
//如果有未发送新闻 请求钉钉webhook
if content != "" {
formt := `
{
"msgtype": "markdown",
"markdown": {
"title":"IOT每日新闻",
"text": "%s"
}
}`
body := fmt.Sprintf(formt, content)
jsonValue := []byte(body)
//发送消息到钉钉群使用webhook
//钉钉文档 https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.karFPe&treeId=257&articleId=105735&docType=1
resp, err := http.Post(dingdingURL, "application/json", bytes.NewBuffer(jsonValue))
if (err != nil) {
return err
}
log.Println(resp)
}
return nil
}
func main() {
//销毁redisClient
defer redisClient.Close()
//创建定时任务
//每天 8点 13点 18点 自动执行爬虫和机器人 六个参数 分别代表 秒 分 小时 天 月 周
//Second uint64
// Minute uint64
Hour uint64
Day uint64
Month uint64
Week uint64
// dingdingNewsBot := toolbox.NewTask("dingding-news-bot", "0 18 8,13,15,18 * * *", newsBot) //dingdingNewsBot := toolbox.NewTask("dingding-news-bot", "0 40 */1 * * *", newsBot) //err := dingdingNewsBot.Run() //检测定时任务 // if err != nil { // log.Fatal(err) // } //添加定时任务 toolbox.AddTask("dingding-news-bot", dingdingNewsBot) //启动定时任务 toolbox.StartTask() defer toolbox.StopTask() select {} }

浙公网安备 33010602011771号