telegram_bots/feeds/modules/nerfnow.py

32 lines
1.2 KiB
Python
Raw Normal View History

import re
2019-12-02 10:20:18 +00:00
from datetime import datetime
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:
2019-11-25 12:45:10 +00:00
last_id = ''
feed = feedparser.parse('http://feeds.feedburner.com/nerfnow/full')
for e in reversed(feed['entries']):
2019-12-04 15:38:14 +00:00
if e['feedburner_origlink'] > 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))
2019-12-02 10:23:26 +00:00
comment = comment.replace('<br />', '')
caption = '<b>{}</b>\n\n{}\n\n{}'.format(e['title'], comment, e['feedburner_origlink'])
bot.send_photo(chat_id, url, caption, parse_mode='html')
2019-12-04 15:38:14 +00:00
yield e['feedburner_origlink']