2019-11-25 12:39:45 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
import feedparser
|
2021-03-20 13:21:13 +00:00
|
|
|
from telegram import Bot
|
2019-12-08 12:45:03 +00:00
|
|
|
from telegram.constants import MAX_CAPTION_LENGTH
|
2019-11-25 12:39:45 +00:00
|
|
|
|
|
|
|
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'
|
|
|
|
|
2021-03-20 13:21:13 +00:00
|
|
|
def execute(self, bot: Bot, chat_id, last_id):
|
2019-11-25 12:39:45 +00:00
|
|
|
if last_id is None:
|
2019-11-25 12:45:10 +00:00
|
|
|
last_id = ''
|
2019-11-25 12:39:45 +00:00
|
|
|
|
|
|
|
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:
|
2019-11-25 12:39:45 +00:00
|
|
|
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 />', '')
|
2019-11-25 12:39:45 +00:00
|
|
|
caption = '<b>{}</b>\n\n{}\n\n{}'.format(e['title'], comment, e['feedburner_origlink'])
|
2019-12-08 12:45:03 +00:00
|
|
|
if len(caption) <= MAX_CAPTION_LENGTH:
|
|
|
|
bot.send_photo(chat_id, url, caption, parse_mode='html')
|
|
|
|
else:
|
|
|
|
m = bot.send_photo(chat_id, url)
|
2019-12-08 12:46:22 +00:00
|
|
|
bot.send_message(chat_id, caption, parse_mode='html', reply_to_message_id=m.message_id)
|
2019-12-04 15:38:14 +00:00
|
|
|
yield e['feedburner_origlink']
|