From 6b085eee6545b92adc065edbd509896111a5a1dc Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Fri, 25 Jan 2019 20:12:44 +0300 Subject: [PATCH] add feed modules --- feeds/modules/__init__.py | 6 +++++- feeds/modules/dank_memes.py | 17 +++++++++++++++++ feeds/modules/shitty_watercolour.py | 24 ++++++++++++++++++++++++ feeds/modules/wp_comic.py | 21 +++++++++++++++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 feeds/modules/dank_memes.py create mode 100644 feeds/modules/shitty_watercolour.py create mode 100644 feeds/modules/wp_comic.py diff --git a/feeds/modules/__init__.py b/feeds/modules/__init__.py index cf1031a..cd51d5c 100644 --- a/feeds/modules/__init__.py +++ b/feeds/modules/__init__.py @@ -1,4 +1,8 @@ from .echo import EchoFeedModuleConfig from .vk_feed import VKFeedModuleConfig +from .dank_memes import DankMemesFeedModuleConfig +from .shitty_watercolour import ShittyWatercolourFeedModuleConfig +from .wp_comic import WPComicFeedModuleConfig -FEED_MODULES = [EchoFeedModuleConfig, VKFeedModuleConfig] +FEED_MODULES = [EchoFeedModuleConfig, VKFeedModuleConfig, DankMemesFeedModuleConfig, ShittyWatercolourFeedModuleConfig, + WPComicFeedModuleConfig] diff --git a/feeds/modules/dank_memes.py b/feeds/modules/dank_memes.py new file mode 100644 index 0000000..fbe6c2c --- /dev/null +++ b/feeds/modules/dank_memes.py @@ -0,0 +1,17 @@ +import requests +from telebot import TeleBot + +from feeds.models import FeedModuleConfig + + +class DankMemesFeedModuleConfig(FeedModuleConfig): + MODULE_NAME = 'Dank memes (Reddit)' + + def execute(self, bot: TeleBot, chat_id, last_id): + posts = requests.get('https://www.reddit.com/r/dankmemes/top.json?sort=top&t=hour', + headers={'User-agent': 'bakatrouble FeedBot 0.1'}).json() + for p in posts['data']['children']: + if p['kind'] == 't3' and 'post_hint' in p['data'] and p['data']['post_hint'] == 'image': + p = p['data'] + bot.send_photo(chat_id, p['url']) + break diff --git a/feeds/modules/shitty_watercolour.py b/feeds/modules/shitty_watercolour.py new file mode 100644 index 0000000..09b1e5b --- /dev/null +++ b/feeds/modules/shitty_watercolour.py @@ -0,0 +1,24 @@ +import requests +from telebot import TeleBot + +from feeds.models import FeedModuleConfig + + +class ShittyWatercolourFeedModuleConfig(FeedModuleConfig): + MODULE_NAME = 'Shitty Watercolour (Reddit)' + + def execute(self, bot: TeleBot, chat_id, last_id): + if last_id is None: + last_id = 0 + + posts = requests.get('https://www.reddit.com/user/Shitty_Watercolour.json', + headers={'User-agent': 'bakatrouble FeedBot 0.1'}).json() + for p in reversed(posts['data']['children']): + if p['data']['created'] > last_id and \ + p['kind'] == 't3' and \ + 'post_hint' in p['data'] and \ + p['data']['post_hint'] == 'image': + p = p['data'] + bot.send_photo(chat_id, p['url'], p['title']) + last_id = p['created'] + return last_id diff --git a/feeds/modules/wp_comic.py b/feeds/modules/wp_comic.py new file mode 100644 index 0000000..7fbd2d2 --- /dev/null +++ b/feeds/modules/wp_comic.py @@ -0,0 +1,21 @@ +import requests +from django.db import models +from telebot import TeleBot + +from feeds.models import FeedModuleConfig + + +class WPComicFeedModuleConfig(FeedModuleConfig): + domain = models.URLField() + + MODULE_NAME = 'WordPress comic' + + def execute(self, bot: TeleBot, chat_id, last_id): + if last_id is None: + last_id = 0 + + 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