From 2bda99e05fe1dfe101c39589ea2bd0ea14053188 Mon Sep 17 00:00:00 2001 From: xtaodada Date: Mon, 3 Oct 2022 16:31:20 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20=E6=94=AF=E6=8C=81?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E6=9B=B4=E5=A4=9A=E7=B1=BB=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- defs/lofter.py | 107 ++++++++++++++++++++++++++++++++++++---------- modules/lofter.py | 25 +++++++---- modules/start.py | 5 ++- 3 files changed, 105 insertions(+), 32 deletions(-) diff --git a/defs/lofter.py b/defs/lofter.py index dca6152..b4ec0a1 100644 --- a/defs/lofter.py +++ b/defs/lofter.py @@ -1,22 +1,34 @@ import os +import re + from io import BytesIO from typing import List from urllib.parse import urlparse - from bs4 import BeautifulSoup -from pyrogram.types import InputMediaPhoto, InlineKeyboardMarkup, InlineKeyboardButton, InputMediaDocument, \ - InputMediaAnimation, InputMediaVideo, Message -from init import request, bot +from pyrogram.types import InputMediaPhoto, InlineKeyboardMarkup, InlineKeyboardButton, InputMediaDocument, \ + InputMediaAnimation, Message + +from init import request class LofterItem: - def __init__(self, url, title): + def __init__(self, url, audio_link, title: str, origin_url, username, name, comment, tags): self.url = url + self.audio_link = f"https://music.163.com/#/song?id={audio_link}" + self.only_text = url is None self.file = None - self.text = title + self.origin_url = origin_url + self.username = username + self.text = f"Lofter Status Info\n\n" \ + f"{title.strip()}\n\n" \ + f"✍️ {name}\n" \ + f"{tags}\n" \ + f"{comment}" async def init(self): + if self.only_text: + return file = await request.get(self.url, timeout=30) file = BytesIO(file.content) file.name = os.path.basename(self.url).split('?')[0] @@ -26,13 +38,17 @@ class LofterItem: if not self.file: await self.init() if static: - await message.reply_document(self.file, caption=self.text, quote=True, reply_markup=gen_button(self.url)) + await message.reply_document(self.file, caption=self.text, quote=True, + reply_markup=lofter_link(self.url, self.origin_url, self.username)) + elif self.only_text: + await message.reply_text(self.text, quote=True, disable_web_page_preview=True, + reply_markup=lofter_link(self.audio_link, self.origin_url, self.username)) elif self.file.name.endswith('.gif'): - await message.reply_animation(self.file, caption=self.text, quote=True, reply_markup=gen_button(self.url)) - elif self.file.name.endswith('.mp4'): - await message.reply_video(self.file, caption=self.text, quote=True, reply_markup=gen_button(self.url)) + await message.reply_animation(self.file, caption=self.text, quote=True, + reply_markup=lofter_link(self.url, self.origin_url, self.username)) else: - await message.reply_photo(self.file, caption=self.text, quote=True, reply_markup=gen_button(self.url)) + await message.reply_photo(self.file, caption=self.text, quote=True, + reply_markup=lofter_link(self.url, self.origin_url, self.username)) async def export(self, static: bool = False, first: bool = False): if not self.file: @@ -41,19 +57,10 @@ class LofterItem: return InputMediaDocument(self.file, caption=self.text if first else None) elif self.file.name.endswith('.gif'): return InputMediaAnimation(self.file, caption=self.text if first else None) - elif self.file.name.endswith('.mp4'): - return InputMediaVideo(self.file, caption=self.text if first else None) else: return InputMediaPhoto(self.file, caption=self.text if first else None) -def gen_button(url): - data = urlparse(url) - return InlineKeyboardMarkup([[ - InlineKeyboardButton(text="Source", url=url), - InlineKeyboardButton(text="Author", url=f"https://{data.hostname}")]]) - - async def input_media(img: List[LofterItem], static: bool = False): return [(await img[ff].export(static, ff == 0)) for ff in range(len(img))] @@ -61,7 +68,61 @@ async def input_media(img: List[LofterItem], static: bool = False): async def get_loft(url: str) -> List[LofterItem]: res = await request.get(url) assert res.status_code == 200 - soup = BeautifulSoup(res.text, "lxml") - title = soup.findAll("div", {"class": "text"})[-1].getText() + username, avatar, name, bio, soup = parse_loft_user(url, res.text) + title = soup.findAll("div", {"class": "text"})[-1].getText().strip() links = soup.findAll("a", {"class": "imgclasstag"}) - return [LofterItem(i.get("bigimgsrc"), title) for i in links] + audio_link = None + audio = soup.find("div", {"class": "img"}) + if audio and audio.getText().strip(): + title = f"分享音乐:{audio.getText().strip()}\n\n{title}" if title else f"分享音乐:{audio.getText().strip()}" + audio_link = re.findall(r"%26id%3D(.*?)%26", audio.findAll("div")[1].get("onclick"))[0] + comment = soup.findAll("h3", {"class": "nctitle"}) + comment_text = "".join(f"{i.getText()} " for i in comment) + if "(" not in comment_text: + comment_text = "" + tags = soup.find("meta", {"name": "Keywords"}).get("content") + tags_text = "".join(f"#{i} " for i in tags.split(",")) + return [LofterItem(i.get("bigimgsrc"), audio_link, title, url, username, name, comment_text, tags_text) + for i in links] if links else [ + LofterItem(None, audio_link, title, url, username, name, comment_text, tags_text)] + + +def parse_loft_user(url: str, content: str): + username = urlparse(url).hostname.split(".")[0] + soup = BeautifulSoup(content, "lxml") + user = soup.find("div", {"class": "selfinfo"}) + avatar = user.find("img").get("src").split("?")[0] + name = user.find("h1").getText() + bio = user.find("div", {"class": "text"}).getText() + return username, avatar, name, bio, soup + + +async def get_loft_user(url: str): + res = await request.get(url) + assert res.status_code == 200 + username, avatar, name, bio, soup = parse_loft_user(url, res.text) + status_link = None + for i in soup.findAll("a"): + url = i.get("href") + if url and "lofter.com/post/" in url: + status_link = url + break + text = f"Lofter User Info\n\n" \ + f"Name: {name}\n" \ + f"Username: {username}\n" \ + f"Bio: " + return text, avatar, username, status_link + + +def lofter_link(url, origin, username): + return InlineKeyboardMarkup([[InlineKeyboardButton(text="Source", url=origin), + InlineKeyboardButton(text="Origin", url=url), + InlineKeyboardButton(text="Author", url=f"https://{username}.lofter.com")]]) if url \ + else InlineKeyboardMarkup([[InlineKeyboardButton(text="Source", url=origin), + InlineKeyboardButton(text="Author", url=f"https://{username}.lofter.com")]]) + + +def lofter_user_link(username, status_link): + return InlineKeyboardMarkup([[InlineKeyboardButton(text="Author", url=f"https://{username}.lofter.com"), + InlineKeyboardButton(text="Status", url=status_link)]]) if status_link else \ + InlineKeyboardMarkup([[InlineKeyboardButton(text="Author", url=f"https://{username}.lofter.com")]]) diff --git a/modules/lofter.py b/modules/lofter.py index b42a1b3..30c90b9 100644 --- a/modules/lofter.py +++ b/modules/lofter.py @@ -2,11 +2,11 @@ from pyrogram import Client, filters, ContinuePropagation from pyrogram.enums import MessageEntityType from pyrogram.types import Message -from defs.lofter import get_loft, input_media +from defs.lofter import get_loft, input_media, get_loft_user, lofter_user_link @Client.on_message(filters.incoming & filters.text & - filters.regex(r"lofter.com/post/")) + filters.regex(r"lofter.com")) async def lofter_share(_: Client, message: Message): if not message.text: return @@ -20,13 +20,22 @@ async def lofter_share(_: Client, message: Message): url = entity.url else: continue - img = await get_loft(url) - if not img: - continue - if len(img) == 1: - await img[0].reply_to(message, static=static) + if "/post/" in url: + img = await get_loft(url) + if not img: + continue + if len(img) == 1: + await img[0].reply_to(message, static=static) + else: + await message.reply_media_group(media=await input_media(img[:9], static), quote=True) else: - await message.reply_media_group(media=await input_media(img[:9], static), quote=True) + text, avatar, username, status_link = await get_loft_user(url) + await message.reply_photo( + avatar, + caption=text, + quote=True, + reply_markup=lofter_user_link(username, status_link) + ) except Exception as e: print(e) breakpoint() diff --git a/modules/start.py b/modules/start.py index bbca543..13f34b8 100644 --- a/modules/start.py +++ b/modules/start.py @@ -4,7 +4,10 @@ from defs.button import gen_button, Button des = """本机器人特性: -★ 解析 bilibili 视频 +★ 解析 bilibili 视频、动态 +★ 解析 twitter 推文、用户 +★ 解析 lofter 日志、用户 +★ 汇率查询 ★ 复读机(3条) ★ 答案之书 ★ 鲁迅说过