telegram_bots/feeds/modules/nerfnow.py

30 lines
1.1 KiB
Python
Raw Normal View History

import re
import feedparser
from telebot import TeleBot
from feeds.models import FeedModuleConfig
IMAGE_RE = re.compile(r'<img alt=".+?" height=".+?" src="(.+?)" title')
CAPTION_RE = re.compile(r'<div>(<p>.*?</p>)</div>')
CAPTION_NEWLINE_RE = re.compile(r'(\w*<p>|</p>\w*){1,2}')
class NerfNowFeedModuleConfig(FeedModuleConfig):
MODULE_NAME = 'NerfNow.com'
def execute(self, bot: TeleBot, chat_id, last_id):
if last_id is None:
last_id = 0
feed = feedparser.parse('http://feeds.feedburner.com/nerfnow/full')
for e in reversed(feed['entries']):
if e['updated'] > last_id:
data = e['content'][0]['value'].replace('\n', '')
url = IMAGE_RE.search(data).group(1).replace('/large', '')
comment = CAPTION_NEWLINE_RE.sub('\n', CAPTION_RE.search(data).group(1))
caption = '<b>{}</b>\n\n{}\n\n{}'.format(e['title'], comment, e['feedburner_origlink'])
bot.send_photo(chat_id, url, caption, parse_mode='html')
yield e['updated']