From c1679c52a993948cbf335b18552eb4c13afebc2c Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Mon, 4 Feb 2019 12:15:53 +0300 Subject: [PATCH] save last_id more often --- aggregator/urls.py | 2 +- feeds/modules/dank_memes.py | 1 + feeds/modules/echo.py | 1 + feeds/modules/shitty_watercolour.py | 3 +-- feeds/modules/vk_feed.py | 3 +-- feeds/modules/vk_music.py | 3 +-- feeds/modules/wp_comic.py | 3 +-- feeds/tasks.py | 11 ++++++++--- 8 files changed, 15 insertions(+), 12 deletions(-) diff --git a/aggregator/urls.py b/aggregator/urls.py index 7bf3069..9ef4724 100644 --- a/aggregator/urls.py +++ b/aggregator/urls.py @@ -3,7 +3,7 @@ from django.urls import path from .views import AggregationSourceListView, AggregationSourceCreateView, AggregationSourceUpdateView, \ AggregationSourceDeleteView -app_name = 'feeds' +app_name = 'aggregator' urlpatterns = [ path('', AggregationSourceListView.as_view(), name='index'), path('/', AggregationSourceUpdateView.as_view(), name='edit'), diff --git a/feeds/modules/dank_memes.py b/feeds/modules/dank_memes.py index fbe6c2c..ef4a53c 100644 --- a/feeds/modules/dank_memes.py +++ b/feeds/modules/dank_memes.py @@ -15,3 +15,4 @@ class DankMemesFeedModuleConfig(FeedModuleConfig): p = p['data'] bot.send_photo(chat_id, p['url']) break + yield None diff --git a/feeds/modules/echo.py b/feeds/modules/echo.py index a6d67fe..7ae7495 100644 --- a/feeds/modules/echo.py +++ b/feeds/modules/echo.py @@ -11,3 +11,4 @@ class EchoFeedModuleConfig(FeedModuleConfig): def execute(self, bot: TeleBot, chat_id, last_id): bot.send_message(chat_id, self.message) + yield None diff --git a/feeds/modules/shitty_watercolour.py b/feeds/modules/shitty_watercolour.py index 09b1e5b..920b7bb 100644 --- a/feeds/modules/shitty_watercolour.py +++ b/feeds/modules/shitty_watercolour.py @@ -20,5 +20,4 @@ class ShittyWatercolourFeedModuleConfig(FeedModuleConfig): p['data']['post_hint'] == 'image': p = p['data'] bot.send_photo(chat_id, p['url'], p['title']) - last_id = p['created'] - return last_id + yield p['created'] diff --git a/feeds/modules/vk_feed.py b/feeds/modules/vk_feed.py index 588725a..532faf4 100644 --- a/feeds/modules/vk_feed.py +++ b/feeds/modules/vk_feed.py @@ -76,5 +76,4 @@ class VKFeedModuleConfig(FeedModuleConfig): disable_web_page_preview=True) except Exception as e: sentry_sdk.capture_exception(e) - last_id = post['id'] - return last_id + yield post['id'] diff --git a/feeds/modules/vk_music.py b/feeds/modules/vk_music.py index 5218821..c935fd5 100644 --- a/feeds/modules/vk_music.py +++ b/feeds/modules/vk_music.py @@ -66,5 +66,4 @@ class VKMusicFeedModuleConfig(FeedModuleConfig): f = get_file(track['url'], vk_session.http) bot.send_audio(chat_id, f, duration=track['duration'], performer=track['artist'], title=track['title']) - last_id = track['id'] - return last_id + yield track['id'] diff --git a/feeds/modules/wp_comic.py b/feeds/modules/wp_comic.py index 7fbd2d2..846181d 100644 --- a/feeds/modules/wp_comic.py +++ b/feeds/modules/wp_comic.py @@ -17,5 +17,4 @@ class WPComicFeedModuleConfig(FeedModuleConfig): media = requests.get('{}/wp-json/wp/v2/media'.format(self.domain.rstrip('/'))).json() for m in filter(lambda x: x['id'] > last_id, reversed(media)): bot.send_photo(chat_id, photo=m['source_url'], caption=m['title']['rendered']) - last_id = m['id'] - return last_id + yield m['id'] diff --git a/feeds/tasks.py b/feeds/tasks.py index 4833661..3f132fe 100644 --- a/feeds/tasks.py +++ b/feeds/tasks.py @@ -21,6 +21,7 @@ def execute_feed(feed_pk): config._reload_maybe() + feed = None try: feed = Feed.objects.get(pk=feed_pk) @@ -29,9 +30,13 @@ def execute_feed(feed_pk): feed.save() bot = TeleBot(config.feed_bot_token) - feed.last_id = feed.config.execute(bot, feed.chat_id, feed.last_id) + for last_id in feed.config.execute(bot, feed.chat_id, feed.last_id): + if last_id: + feed.last_id = last_id + feed.save() feed.last_check = timezone.now() feed.save() finally: - feed.lock = False - feed.save() + if feed: + feed.lock = False + feed.save()