支持 lofter 解析

This commit is contained in:
xtaodada 2022-10-03 01:14:56 +08:00
parent bf02ea7daf
commit 8b30d1c4ef
Signed by: xtaodada
GPG Key ID: 4CBB3F4FA8C85659
4 changed files with 61 additions and 1 deletions

27
defs/lofter.py Normal file
View File

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

31
modules/lofter.py Normal file
View File

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

View File

@ -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'<b>Twitter Status Info</b>\n\n{text}\n\n{user_text}'

View File

@ -13,3 +13,5 @@ jinja2
apscheduler
pytz
python-twitter
beautifulsoup4
lxml