add feed modules

This commit is contained in:
bakatrouble 2019-01-25 20:12:44 +03:00
parent ac925ee704
commit 6b085eee65
4 changed files with 67 additions and 1 deletions

View File

@ -1,4 +1,8 @@
from .echo import EchoFeedModuleConfig from .echo import EchoFeedModuleConfig
from .vk_feed import VKFeedModuleConfig 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]

View File

@ -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

View File

@ -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

21
feeds/modules/wp_comic.py Normal file
View File

@ -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