From fd63775d586de1027c8f564b28d8e54b5965b29f Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Mon, 25 Nov 2019 15:39:45 +0300 Subject: [PATCH] update requirements, implement nerfnow feed --- .../0008_nerfnowfeedmoduleconfig.py | 22 ++++++++++++++ feeds/modules/__init__.py | 3 +- feeds/modules/nerfnow.py | 29 +++++++++++++++++++ requirements.txt | 3 +- 4 files changed, 55 insertions(+), 2 deletions(-) create mode 100644 feeds/migrations/0008_nerfnowfeedmoduleconfig.py create mode 100644 feeds/modules/nerfnow.py diff --git a/feeds/migrations/0008_nerfnowfeedmoduleconfig.py b/feeds/migrations/0008_nerfnowfeedmoduleconfig.py new file mode 100644 index 0000000..8bd9614 --- /dev/null +++ b/feeds/migrations/0008_nerfnowfeedmoduleconfig.py @@ -0,0 +1,22 @@ +# Generated by Django 2.1.5 on 2019-11-25 12:39 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('feeds', '0007_remove_feed_lock'), + ] + + operations = [ + migrations.CreateModel( + name='NerfNowFeedModuleConfig', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ], + options={ + 'abstract': False, + }, + ), + ] diff --git a/feeds/modules/__init__.py b/feeds/modules/__init__.py index e6b3901..4ccac0c 100644 --- a/feeds/modules/__init__.py +++ b/feeds/modules/__init__.py @@ -6,8 +6,9 @@ from .shitty_watercolour import ShittyWatercolourFeedModuleConfig from .wp_comic import WPComicFeedModuleConfig from .twitter import TwitterFeedModuleConfig from .tapas import TapasFeedModuleConfig +from .nerfnow import NerfNowFeedModuleConfig FEED_MODULES = [EchoFeedModuleConfig, VKFeedModuleConfig, VKMusicFeedModuleConfig, DankMemesFeedModuleConfig, ShittyWatercolourFeedModuleConfig, WPComicFeedModuleConfig, TwitterFeedModuleConfig, - TapasFeedModuleConfig] + TapasFeedModuleConfig, NerfNowFeedModuleConfig] diff --git a/feeds/modules/nerfnow.py b/feeds/modules/nerfnow.py new file mode 100644 index 0000000..f22b0d7 --- /dev/null +++ b/feeds/modules/nerfnow.py @@ -0,0 +1,29 @@ +import re + +import feedparser +from telebot import TeleBot + +from feeds.models import FeedModuleConfig + + +IMAGE_RE = re.compile(r'.+?(

.*?

)') +CAPTION_NEWLINE_RE = re.compile(r'(\w*

|

\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 = '{}\n\n{}\n\n{}'.format(e['title'], comment, e['feedburner_origlink']) + bot.send_photo(chat_id, url, caption, parse_mode='html') + yield e['updated'] diff --git a/requirements.txt b/requirements.txt index 5fd3705..a377e5e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,6 +24,7 @@ django-redis==4.10.0 django-timezone-field==3.0 django-yamlfield==1.0.3 enum34==1.1.6 +feedparser==5.2.1 future==0.17.1 idna==2.8 json-rpc==1.12.1 @@ -54,5 +55,5 @@ tornado==6.0.3 urllib3==1.24.1 uWSGI==2.0.17.1 vine==1.2.0 -vk-api==11.6.0 +vk-api==11.6.1 Werkzeug==0.14.1