示例#1
0
文件: rss.go 项目: Megadotzip/Jqs7Bot
func (r *Rss) newRss(interval ...string) error {
	rc := conf.Redis
	_, err := feedreader.Fetch(r.Args[1])
	if err != nil {
		log.Println(err)
		return errors.New("弹药检测失败,请检查后重试")
	}
	rc.SAdd("tgRssChats", strconv.Itoa(r.ChatID))
	rc.SAdd("tgRss:"+strconv.Itoa(r.ChatID), r.Args[1])
	if len(interval) > 0 {
		in, err := strconv.Atoi(interval[0])
		if err != nil {
			return errors.New("哔哔!时空坐标参数设置错误!")
		}
		rc.Set("tgRssInterval:"+
			strconv.Itoa(r.ChatID)+":"+r.Args[1], interval[0], -1)
		j, err := scheduler.Every(getInterval(in)).Seconds().
			Run(rssJob(r.Args[1], r.ChatID, r.Bot))
		if err != nil {
			log.Println(err.Error())
			return nil
		}
		jobs.NewJob(strconv.Itoa(r.ChatID)+":"+r.Args[1], j)
		return nil
	}
	j, err := scheduler.Every(getInterval(-1)).Seconds().
		Run(rssJob(r.Args[1], r.ChatID, r.Bot))
	if err != nil {
		log.Println(err.Error())
		return nil
	}
	jobs.NewJob(strconv.Itoa(r.ChatID)+":"+r.Args[1], j)

	return nil
}
示例#2
0
func fetchFeed(url string, fetcher *h.Fetcher) (feed *Feed, items []*Item, err error) {
	fd, err := feedreader.Fetch(url, fetcher)
	if err != nil {
		return
	}

	feed, items = assembleFeed(fd)
	return
}
示例#3
0
文件: rss.go 项目: Megadotzip/Jqs7Bot
func rssJob(feedURL string, chatid int, bot *tgbotapi.BotAPI) func() {
	return func() {
		feed, err := feedreader.Fetch(feedURL)
		if err != nil {
			log.Printf("Error: %s %s", feedURL, err.Error())
			return
		}
		var buf bytes.Buffer
		rc := conf.Redis
		for k, item := range feed.Items {
			if strings.Contains(conf.Redis.Get("tgRssLatest:"+
				strconv.Itoa(chatid)+":"+feedURL).Val(), item.Link) {
				if buf.String() != "" {
					msg := tgbotapi.NewMessage(chatid,
						"*"+feed.Title+"*\n"+buf.String())
					msg.DisableWebPagePreview = true
					msg.ParseMode = tgbotapi.ModeMarkdown
					bot.SendMessage(msg)
				}
				break
			}

			if strings.ContainsAny(item.Title, "[]()") ||
				strings.Contains(item.Title, "://") {
				str := fmt.Sprintf("%s [link](%s)\n",
					markdownEscape(item.Title), item.Link)
				buf.WriteString(str)
			} else {
				str := fmt.Sprintf("[%s](%s)\n",
					item.Title, item.Link)
				buf.WriteString(str)
			}

			itemNumsInMessage := 9
			if k != 0 && k%itemNumsInMessage == 0 || k == len(feed.Items)-1 {
				if buf.String() != "" {
					msg := tgbotapi.NewMessage(chatid,
						"*"+feed.Title+"*\n"+buf.String())
					msg.DisableWebPagePreview = true
					msg.ParseMode = tgbotapi.ModeMarkdown
					bot.SendMessage(msg)
				}
				buf.Reset()
			}
		}

		if feed.Items != nil && len(feed.Items) > 0 {
			var buf []string
			for _, v := range feed.Items {
				buf = append(buf, v.Link)
			}
			rc.Set("tgRssLatest:"+strconv.Itoa(chatid)+":"+feedURL,
				strings.Join(buf, ":"), -1)
		}
	}
}