zhenxun_bot/plugins/parse_bilibili_json.py

62 lines
2.7 KiB
Python
Raw Normal View History

2021-07-30 21:21:51 +08:00
from nonebot import on_message, on_command
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-06-30 19:50:55 +08:00
from utils.utils import get_message_json, get_local_proxy
2021-05-20 19:27:31 +08:00
import json
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 models.group_remind import GroupRemind
from nonebot.adapters.cqhttp.exception import ActionFailed
import time
import aiohttp
2021-06-15 10:57:08 +08:00
from bilibili_api import settings
2021-07-30 21:21:51 +08:00
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)
@parse_bilibili_json.handle()
async def _(bot: Bot, event: GroupMessageEvent, state: T_State):
2021-07-30 21:21:51 +08:00
if await GroupRemind.get_status(event.group_id, "blpar") and get_message_json(
event.json()
):
data = json.loads(get_message_json(event.json())[0]["data"])
2021-05-20 19:27:31 +08:00
if data:
2021-07-30 21:21:51 +08:00
if data.get("desc") == "哔哩哔哩":
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(
data["meta"]["detail_1"]["qqdocurl"],
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-07-30 21:21:51 +08:00
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"] # 投币
2021-05-20 19:27:31 +08:00
# like = vd_info['stat']['like'] # 点赞
# danmu = vd_info['stat']['danmaku'] # 弹幕
2021-07-30 21:21:51 +08:00
date = time.strftime("%Y-%m-%d", time.localtime(vd_info["ctime"]))
2021-05-20 19:27:31 +08:00
try:
await parse_bilibili_json.send(
2021-07-30 21:21:51 +08:00
image(vd_info["pic"]) + f"\nav{aid}\n标题:{title}\n"
f"UP{author}\n"
f"上传日期:{date}\n"
f"回复:{reply},收藏:{favorite},投币:{coin}\n"
f"{url}"
2021-05-20 19:27:31 +08:00
)
except ActionFailed:
2021-07-30 21:21:51 +08:00
logger.warning(f"{event.group_id} 发送bilibili解析失败")
logger.info(
f"USER {event.user_id} GROUP {event.group_id} 解析bilibili转发 {url}"
)