From b51d06d3056951922d25fc9631abb47ff5e725d3 Mon Sep 17 00:00:00 2001 From: bakatrouble Date: Sun, 31 Mar 2019 22:58:20 +0300 Subject: [PATCH] Add tapas feed module --- feeds/modules/__init__.py | 4 ++- feeds/modules/tapas.py | 62 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 feeds/modules/tapas.py diff --git a/feeds/modules/__init__.py b/feeds/modules/__init__.py index dab9b44..e6b3901 100644 --- a/feeds/modules/__init__.py +++ b/feeds/modules/__init__.py @@ -5,7 +5,9 @@ from .dank_memes import DankMemesFeedModuleConfig from .shitty_watercolour import ShittyWatercolourFeedModuleConfig from .wp_comic import WPComicFeedModuleConfig from .twitter import TwitterFeedModuleConfig +from .tapas import TapasFeedModuleConfig FEED_MODULES = [EchoFeedModuleConfig, VKFeedModuleConfig, VKMusicFeedModuleConfig, DankMemesFeedModuleConfig, - ShittyWatercolourFeedModuleConfig, WPComicFeedModuleConfig, TwitterFeedModuleConfig] + ShittyWatercolourFeedModuleConfig, WPComicFeedModuleConfig, TwitterFeedModuleConfig, + TapasFeedModuleConfig] diff --git a/feeds/modules/tapas.py b/feeds/modules/tapas.py new file mode 100644 index 0000000..c113f0c --- /dev/null +++ b/feeds/modules/tapas.py @@ -0,0 +1,62 @@ +import os +from tempfile import TemporaryDirectory + +import sentry_sdk +from django.db import models +from djconfig import config +from telebot import TeleBot +from telebot.types import InputMediaPhoto, InputMediaVideo +from bs4 import BeautifulSoup +import requests +from PIL import Image + +from feeds.models import FeedModuleConfig + + +class TapasFeedModuleConfig(FeedModuleConfig): + display_name = models.CharField(max_length=256) + + MODULE_NAME = 'Tapas.io comic' + + def execute(self, bot: TeleBot, chat_id, last_id): + config._reload_maybe() + + if last_id is None: + last_id = 0 + + soup = BeautifulSoup(requests.get(f'https://tapas.io/series/{self.display_name}').text, 'html.parser') + for episode in soup.select('#episode-nav .episode'): + eid = int(episode['data-eid']) + if eid <= last_id: + continue + + esoup = BeautifulSoup(requests.get(f'https://tapas.io/episode/{eid}').text, 'html.parser') + # series_title = esoup.select_one('#series-info-wrap a.series-header-title').text + title = esoup.select_one(f'#episodes [data-eid={eid}] h1').text + caption = f'{title}\nhttps://tapas.io/episode/{eid}' + imgs = [] + for img in esoup.select(f'#episodes [data-eid={eid}] .art-image'): + imgs.append(img['src']) + + if len(imgs) == 1: + bot.send_document(chat_id, imgs[0], caption=caption) + if len(imgs) > 1: + with TemporaryDirectory() as d: + ims = [] + for i, img in enumerate(imgs): + img_path = os.path.join(d, f'{i}.jpg') + with open(img_path, 'wb') as f: + f.write(requests.get(img).content) + ims.append(Image.open(img_path)) + widths, heights = zip(*(i.size for i in ims)) + max_width = max(widths) + total_height = max(heights) + new_im = Image.new('RGB', (max_width, total_height)) + offset = 0 + for im in ims: + new_im.paste(im, (offset, 0)) + offset += im.size[0] + new_im_path = os.path.join(d, 'combined.jpg') + new_im.save(new_im_path) + bot.send_document(chat_id, new_im_path, caption=caption) + yield eid