2021-08-04 15:19:45 +08:00
|
|
|
|
from nonebot import on_message
|
2021-05-20 19:27:31 +08:00
|
|
|
|
from services.log import logger
|
|
|
|
|
|
from nonebot.adapters.cqhttp import Bot, GroupMessageEvent
|
|
|
|
|
|
from nonebot.typing import T_State
|
2021-09-05 02:21:38 +08:00
|
|
|
|
from utils.utils import get_message_json, get_local_proxy, get_message_text, is_number
|
2021-06-30 19:50:55 +08:00
|
|
|
|
from utils.user_agent import get_user_agent
|
2021-05-20 19:27:31 +08:00
|
|
|
|
from nonebot.adapters.cqhttp.permission import GROUP
|
|
|
|
|
|
from bilibili_api import video
|
2021-07-30 21:21:51 +08:00
|
|
|
|
from utils.message_builder import image
|
2021-05-20 19:27:31 +08:00
|
|
|
|
from nonebot.adapters.cqhttp.exception import ActionFailed
|
2021-08-17 23:17:08 +08:00
|
|
|
|
from utils.image_utils import CreateImg
|
|
|
|
|
|
from utils.browser import get_browser
|
|
|
|
|
|
from configs.path_config import IMAGE_PATH
|
|
|
|
|
|
import asyncio
|
2021-05-20 19:27:31 +08:00
|
|
|
|
import time
|
|
|
|
|
|
import aiohttp
|
2021-06-15 10:57:08 +08:00
|
|
|
|
from bilibili_api import settings
|
2021-10-03 14:24:07 +08:00
|
|
|
|
from utils.manager import group_manager
|
2021-08-17 23:17:08 +08:00
|
|
|
|
import ujson as json
|
2021-07-30 21:21:51 +08:00
|
|
|
|
|
2021-10-03 14:24:07 +08:00
|
|
|
|
|
|
|
|
|
|
__zx_plugin_name__ = "B站转发解析"
|
|
|
|
|
|
__plugin_usage__ = """
|
|
|
|
|
|
usage:
|
|
|
|
|
|
B站转发解析,解析b站分享信息,支持bv,bilibili链接,b站手机端转发卡片,cv,b23.tv,且5分钟内不解析相同url
|
|
|
|
|
|
""".strip()
|
|
|
|
|
|
__plugin_des__ = "B站转发解析"
|
|
|
|
|
|
__plugin_type__ = ("被动相关",)
|
|
|
|
|
|
__plugin_version__ = 0.1
|
|
|
|
|
|
__plugin_author__ = "HibiKier"
|
|
|
|
|
|
__plugin_task__ = {"bilibili_parse": "b站转发解析"}
|
|
|
|
|
|
|
2021-05-20 19:27:31 +08:00
|
|
|
|
if get_local_proxy():
|
2021-06-15 10:57:08 +08:00
|
|
|
|
settings.proxy = get_local_proxy()
|
2021-05-20 19:27:31 +08:00
|
|
|
|
|
|
|
|
|
|
parse_bilibili_json = on_message(priority=1, permission=GROUP, block=False)
|
|
|
|
|
|
|
2021-09-05 02:21:38 +08:00
|
|
|
|
_tmp = {}
|
|
|
|
|
|
|
2021-05-20 19:27:31 +08:00
|
|
|
|
|
|
|
|
|
|
@parse_bilibili_json.handle()
|
|
|
|
|
|
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
|
2021-10-03 14:24:07 +08:00
|
|
|
|
if await group_manager.check_group_task_status(event.group_id, "bilibili_parse"):
|
2021-08-17 23:17:08 +08:00
|
|
|
|
vd_info = None
|
2021-09-05 02:21:38 +08:00
|
|
|
|
url = None
|
2021-08-17 23:17:08 +08:00
|
|
|
|
if get_message_json(event.json()):
|
|
|
|
|
|
try:
|
|
|
|
|
|
data = json.loads(get_message_json(event.json())[0]["data"])
|
|
|
|
|
|
except (IndexError, KeyError):
|
|
|
|
|
|
data = None
|
|
|
|
|
|
if data:
|
|
|
|
|
|
# 转发视频
|
|
|
|
|
|
if data.get("desc") == "哔哩哔哩":
|
|
|
|
|
|
async with aiohttp.ClientSession(
|
|
|
|
|
|
headers=get_user_agent()
|
|
|
|
|
|
) as session:
|
|
|
|
|
|
async with session.get(
|
|
|
|
|
|
data["meta"]["detail_1"]["qqdocurl"],
|
|
|
|
|
|
proxy=get_local_proxy(),
|
|
|
|
|
|
timeout=7,
|
|
|
|
|
|
) as response:
|
|
|
|
|
|
url = str(response.url).split("?")[0]
|
|
|
|
|
|
bvid = url.split("/")[-1]
|
|
|
|
|
|
vd_info = await video.Video(bvid=bvid).get_info()
|
|
|
|
|
|
# 转发专栏
|
|
|
|
|
|
if (
|
|
|
|
|
|
data.get("meta")
|
|
|
|
|
|
and data["meta"].get("news")
|
|
|
|
|
|
and data["meta"]["news"].get("desc") == "哔哩哔哩专栏"
|
|
|
|
|
|
):
|
|
|
|
|
|
url = data["meta"]["news"]["jumpUrl"]
|
|
|
|
|
|
page = None
|
|
|
|
|
|
try:
|
|
|
|
|
|
browser = await get_browser()
|
|
|
|
|
|
if not browser:
|
|
|
|
|
|
return
|
|
|
|
|
|
page = await browser.new_page(
|
|
|
|
|
|
user_agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"
|
|
|
|
|
|
" (KHTML, like Gecko) Chrome/93.0.4530.0 Safari/537.36"
|
|
|
|
|
|
)
|
|
|
|
|
|
await page.goto(url, wait_until="networkidle", timeout=10000)
|
|
|
|
|
|
await page.set_viewport_size({"width": 2560, "height": 1080})
|
|
|
|
|
|
await page.click("#app > div")
|
|
|
|
|
|
div = await page.query_selector("#app > div")
|
|
|
|
|
|
await div.screenshot(
|
|
|
|
|
|
path=f"{IMAGE_PATH}/temp/cv_{event.user_id}.png",
|
|
|
|
|
|
timeout=100000,
|
|
|
|
|
|
)
|
|
|
|
|
|
await asyncio.get_event_loop().run_in_executor(
|
|
|
|
|
|
None, resize, f"{IMAGE_PATH}/temp/cv_{event.user_id}.png"
|
|
|
|
|
|
)
|
|
|
|
|
|
await parse_bilibili_json.send(
|
|
|
|
|
|
image(f"cv_{event.user_id}.png", "temp")
|
|
|
|
|
|
)
|
|
|
|
|
|
await page.close()
|
|
|
|
|
|
logger.info(
|
|
|
|
|
|
f"USER {event.user_id} GROUP {event.group_id} 解析bilibili转发 {url}"
|
|
|
|
|
|
)
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"尝试解析bilibili专栏 {url} 失败 {type(e)}:{e}")
|
|
|
|
|
|
if page:
|
|
|
|
|
|
await page.close()
|
|
|
|
|
|
return
|
|
|
|
|
|
# BV
|
|
|
|
|
|
if get_message_text(event.json()):
|
|
|
|
|
|
msg = get_message_text(event.json())
|
2021-09-05 02:21:38 +08:00
|
|
|
|
if "BV" in msg:
|
2021-10-03 14:24:07 +08:00
|
|
|
|
index = msg.find("BV")
|
|
|
|
|
|
if len(msg[index + 2 :]) >= 10:
|
|
|
|
|
|
msg = msg[index : index + 12]
|
|
|
|
|
|
url = f"https://www.bilibili.com/video/{msg}"
|
2021-09-05 02:21:38 +08:00
|
|
|
|
vd_info = await video.Video(bvid=msg).get_info()
|
2021-10-03 14:24:07 +08:00
|
|
|
|
elif "av" in msg:
|
|
|
|
|
|
index = msg.find("av")
|
|
|
|
|
|
if len(msg[index + 2 :]) >= 9:
|
|
|
|
|
|
msg = msg[index + 2 : index + 11]
|
2021-09-05 02:21:38 +08:00
|
|
|
|
if is_number(msg):
|
2021-10-03 14:24:07 +08:00
|
|
|
|
url = f"https://www.bilibili.com/video/{msg}"
|
2021-09-05 02:21:38 +08:00
|
|
|
|
vd_info = await video.Video(aid=int(msg)).get_info()
|
|
|
|
|
|
elif "https://b23.tv" in msg:
|
2021-08-17 23:17:08 +08:00
|
|
|
|
url = "https://" + msg[msg.find("b23.tv") : msg.find("b23.tv") + 13]
|
2021-05-20 19:27:31 +08:00
|
|
|
|
async with aiohttp.ClientSession(headers=get_user_agent()) as session:
|
2021-07-30 21:21:51 +08:00
|
|
|
|
async with session.get(
|
2021-08-17 23:17:08 +08:00
|
|
|
|
url,
|
2021-07-30 21:21:51 +08:00
|
|
|
|
proxy=get_local_proxy(),
|
|
|
|
|
|
timeout=7,
|
|
|
|
|
|
) as response:
|
2021-05-20 19:27:31 +08:00
|
|
|
|
url = str(response.url).split("?")[0]
|
2021-07-30 21:21:51 +08:00
|
|
|
|
bvid = url.split("/")[-1]
|
2021-06-15 10:57:08 +08:00
|
|
|
|
vd_info = await video.Video(bvid=bvid).get_info()
|
2021-08-17 23:17:08 +08:00
|
|
|
|
if vd_info:
|
2021-10-03 14:24:07 +08:00
|
|
|
|
if (
|
|
|
|
|
|
url in _tmp.keys() and time.time() - _tmp[url] > 30
|
|
|
|
|
|
) or url not in _tmp.keys():
|
2021-09-05 02:21:38 +08:00
|
|
|
|
_tmp[url] = time.time()
|
|
|
|
|
|
aid = vd_info["aid"]
|
|
|
|
|
|
title = vd_info["title"]
|
|
|
|
|
|
author = vd_info["owner"]["name"]
|
|
|
|
|
|
reply = vd_info["stat"]["reply"] # 回复
|
|
|
|
|
|
favorite = vd_info["stat"]["favorite"] # 收藏
|
|
|
|
|
|
coin = vd_info["stat"]["coin"] # 投币
|
|
|
|
|
|
# like = vd_info['stat']['like'] # 点赞
|
|
|
|
|
|
# danmu = vd_info['stat']['danmaku'] # 弹幕
|
|
|
|
|
|
date = time.strftime("%Y-%m-%d", time.localtime(vd_info["ctime"]))
|
|
|
|
|
|
try:
|
|
|
|
|
|
await parse_bilibili_json.send(
|
|
|
|
|
|
image(vd_info["pic"]) + f"\nav{aid}\n标题:{title}\n"
|
|
|
|
|
|
f"UP:{author}\n"
|
|
|
|
|
|
f"上传日期:{date}\n"
|
|
|
|
|
|
f"回复:{reply},收藏:{favorite},投币:{coin}\n"
|
|
|
|
|
|
f"{url}"
|
|
|
|
|
|
)
|
|
|
|
|
|
except ActionFailed:
|
|
|
|
|
|
logger.warning(f"{event.group_id} 发送bilibili解析失败")
|
|
|
|
|
|
else:
|
|
|
|
|
|
logger.info(
|
|
|
|
|
|
f"USER {event.user_id} GROUP {event.group_id} 解析bilibili转发 {url}"
|
|
|
|
|
|
)
|
2021-08-17 23:17:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def resize(path: str):
|
|
|
|
|
|
A = CreateImg(0, 0, background=path, ratio=0.5)
|
|
|
|
|
|
A.save(path)
|