From 8b30d1c4ef32597326ca891ca6ad5c5112c7712a Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 3 Oct 2022 01:14:56 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20=E6=94=AF=E6=8C=81=20lofter=20?= =?UTF-8?q?=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- defs/lofter.py | 27 +++++++++++++++++++++++++++ modules/lofter.py | 31 +++++++++++++++++++++++++++++++ modules/twitter_api.py | 2 +- requirements.txt | 2 ++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 defs/lofter.py create mode 100644 modules/lofter.py diff --git a/defs/lofter.py b/defs/lofter.py new file mode 100644 index 0000000..3919eee --- /dev/null +++ b/defs/lofter.py @@ -0,0 +1,27 @@ +from urllib.parse import urlparse + +from bs4 import BeautifulSoup +from pyrogram.types import InputMediaPhoto, InlineKeyboardMarkup, InlineKeyboardButton + +from init import request + + +def gen_button(url): + data = urlparse(url) + return InlineKeyboardMarkup([[ + InlineKeyboardButton(text="Source", url=url), + InlineKeyboardButton(text="Author", url=f"https://{data.hostname}")]]) + + +def input_media(img, text): + return [InputMediaPhoto(img[ff], caption=text if ff == 0 else None) for ff in range(len(img))] + + +async def get_loft(url: str): + res = await request.get(url) + assert res.status_code == 200 + soup = BeautifulSoup(res.text, "lxml") + links = soup.findAll("a", {"class": "imgclasstag"}) + img = [i.get("bigimgsrc") for i in links] + title = soup.findAll("div", {"class": "text"})[-1].getText() + return img, title diff --git a/modules/lofter.py b/modules/lofter.py new file mode 100644 index 0000000..6a29951 --- /dev/null +++ b/modules/lofter.py @@ -0,0 +1,31 @@ +import contextlib + +from pyrogram import Client, filters, ContinuePropagation +from pyrogram.enums import MessageEntityType +from pyrogram.types import Message + +from defs.lofter import get_loft, input_media, gen_button + + +@Client.on_message(filters.incoming & filters.text & + filters.regex(r"lofter.com/post/")) +async def lofter_share(_: Client, message: Message): + if not message.text: + return + with contextlib.suppress(Exception): + for num in range(len(message.entities)): + entity = message.entities[num] + if entity.type == MessageEntityType.URL: + url = message.text[entity.offset:entity.offset + entity.length] + elif entity.type == MessageEntityType.TEXT_LINK: + url = entity.url + else: + continue + img, text = await get_loft(url) + if not img: + continue + if len(img) == 1: + await message.reply_photo(img[0], caption=text, quote=True, reply_markup=gen_button(url)) + else: + await message.reply_media_group(media=input_media(img[:9], text), quote=True) + raise ContinuePropagation diff --git a/modules/twitter_api.py b/modules/twitter_api.py index 1bb3753..659c159 100644 --- a/modules/twitter_api.py +++ b/modules/twitter_api.py @@ -26,7 +26,7 @@ async def twitter_share(client: Client, message: Message): url = urlparse(url) if url.hostname and url.hostname == "twitter.com": if url.path.find('status') >= 0: - status_id = url.path[url.path.find('status') + 7:].split("/")[0] + status_id = (url.path[url.path.find('status') + 7:].split("/")[0]).split("?")[0] url_json = twitter_api.GetStatus(status_id, include_entities=True) text, user_text, media_model, media_list, quoted_status = get_twitter_status(url_json) text = f'Twitter Status Info\n\n{text}\n\n{user_text}' diff --git a/requirements.txt b/requirements.txt index 0e1e5a6..2ff130a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,3 +13,5 @@ jinja2 apscheduler pytz python-twitter +beautifulsoup4 +lxml