2023-07-03 16:44:55 +00:00
|
|
|
import aiohttp
|
2023-07-02 06:25:49 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from core.builtins import Bot
|
|
|
|
from core.component import module
|
|
|
|
from .bilibili import get_info
|
2023-07-03 17:45:53 +00:00
|
|
|
from urllib.parse import urlparse
|
2023-07-02 06:25:49 +00:00
|
|
|
|
|
|
|
api_url = f'https://api.bilibili.com/x/web-interface/view/detail'
|
|
|
|
|
|
|
|
bili = module('bilibili', alias='bili', developers=['DoroWolf'],
|
|
|
|
desc='{bilibili.help.desc}', support_languages=['zh_cn'])
|
|
|
|
|
|
|
|
|
|
|
|
@bili.command('<video> [-i] {{bilibili.help}}',
|
2023-07-03 05:13:03 +00:00
|
|
|
options_desc={'-i': '{bilibili.help.option.i}'})
|
2023-07-02 07:52:03 +00:00
|
|
|
async def _(msg: Bot.MessageSession, video: str, get_detail=False):
|
2023-07-02 06:25:49 +00:00
|
|
|
if msg.parsed_msg.get('-i', False):
|
|
|
|
get_detail = True
|
|
|
|
if video[:2].upper() == "BV":
|
|
|
|
url = f"{api_url}?bvid={video}"
|
2023-07-03 11:21:37 +00:00
|
|
|
elif video[:2].upper() == "AV":
|
2023-07-02 06:25:49 +00:00
|
|
|
url = f"{api_url}?aid={video[2:]}"
|
|
|
|
else:
|
2023-07-02 06:37:49 +00:00
|
|
|
await msg.finish(msg.locale.t('bilibili.message.error.invalid'))
|
|
|
|
await get_info(msg, url, get_detail)
|
2023-07-02 06:25:49 +00:00
|
|
|
|
|
|
|
|
2023-07-04 08:54:59 +00:00
|
|
|
@bili.handle(re.compile(r"([aA][vV])(\d+)", flags=re.I), mode='A',
|
2023-07-03 05:13:03 +00:00
|
|
|
desc="{bilibili.help.regex.av}")
|
2023-07-02 16:16:07 +00:00
|
|
|
async def _(msg: Bot.MessageSession):
|
2023-07-04 09:11:58 +00:00
|
|
|
res = msg.matched_msg[0]
|
2023-07-02 06:25:49 +00:00
|
|
|
if res:
|
|
|
|
url = f"{api_url}?aid={res.groups()[1]}"
|
2023-07-02 16:16:07 +00:00
|
|
|
await get_info(msg, url, get_detail=False)
|
2023-07-02 06:25:49 +00:00
|
|
|
|
|
|
|
|
2023-07-04 08:54:59 +00:00
|
|
|
@bili.handle(re.compile(r"BV[a-zA-Z0-9]{10}", flags=re.I), mode='A',
|
2023-07-03 05:13:03 +00:00
|
|
|
desc="{bilibili.help.regex.bv}")
|
2023-07-02 16:16:07 +00:00
|
|
|
async def _(msg: Bot.MessageSession):
|
2023-07-04 09:11:58 +00:00
|
|
|
res = msg.matched_msg[0]
|
2023-07-02 06:25:49 +00:00
|
|
|
if res:
|
2023-07-02 08:07:29 +00:00
|
|
|
url = f"{api_url}?bvid={res.group()}"
|
2023-07-03 03:54:17 +00:00
|
|
|
await get_info(msg, url, get_detail=False)
|
|
|
|
|
|
|
|
|
2023-07-04 00:54:36 +00:00
|
|
|
@bili.handle(re.compile(r"https?://(?:www\.|m\.)?bilibili\.com(?:/video|)/(av\d+|BV[A-Za-z0-9]{10})(?:/.*?|)$"), mode="M",
|
2023-07-03 05:13:03 +00:00
|
|
|
desc="{bilibili.help.regex.url}")
|
2023-07-03 03:54:17 +00:00
|
|
|
async def _(msg: Bot.MessageSession):
|
2023-07-03 05:13:03 +00:00
|
|
|
video = msg.matched_msg.group(1)
|
|
|
|
if video[:2] == "BV":
|
|
|
|
url = f"{api_url}?bvid={video}"
|
|
|
|
else:
|
|
|
|
url = f"{api_url}?aid={video[2:]}"
|
|
|
|
|
2023-07-03 03:54:17 +00:00
|
|
|
await get_info(msg, url, get_detail=False)
|
|
|
|
|
|
|
|
|
2023-07-04 00:54:36 +00:00
|
|
|
@bili.handle(re.compile(r"https?://b23\.tv/(av\d+|BV[A-Za-z0-9]{10}|[A-Za-z0-9]{7})(?:/.*?|)$"), mode="M",
|
2023-07-03 05:13:03 +00:00
|
|
|
desc="{bilibili.help.regex.shorturl}")
|
2023-07-03 03:54:17 +00:00
|
|
|
async def _(msg: Bot.MessageSession):
|
|
|
|
res = msg.matched_msg
|
|
|
|
if res:
|
2023-07-03 17:45:53 +00:00
|
|
|
video = res.groups()[0]
|
2023-07-03 03:54:17 +00:00
|
|
|
if video[:2] == "BV":
|
|
|
|
url = f"{api_url}?bvid={video}"
|
|
|
|
elif video[:2] == "av":
|
|
|
|
url = f"{api_url}?aid={video[2:]}"
|
|
|
|
else:
|
2023-07-03 17:45:53 +00:00
|
|
|
url = await parse_shorturl(f"https://b23.tv/{video}")
|
2023-07-03 05:13:03 +00:00
|
|
|
|
|
|
|
await get_info(msg, url, get_detail=False)
|
2023-07-03 16:44:55 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def parse_shorturl(shorturl):
|
|
|
|
async with aiohttp.ClientSession() as session:
|
2023-07-03 17:45:53 +00:00
|
|
|
async with session.get(shorturl, allow_redirects=True) as response:
|
|
|
|
target_url = str(response.url)
|
|
|
|
parsed_url = urlparse(target_url)
|
|
|
|
video = parsed_url.path.split("/")[-2]
|
|
|
|
url = f"{api_url}?bvid={video}"
|
|
|
|
return url
|