Skip to content

Commit

Permalink
direct support for video id parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsmh authored Mar 10, 2024
1 parent 97a5288 commit 9b6ad8e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
14 changes: 9 additions & 5 deletions biliparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,10 +539,12 @@ async def live_parser(client: httpx.AsyncClient, url: str):
@safe_parser
async def video_parser(client: httpx.AsyncClient, url: str):
match = re.search(
r"(?:bilibili\.com/(?:video|bangumi/play)|b23\.tv|acg\.tv)/(?:(?P<bvid>BV\w+)|av(?P<aid>\d+)|ep(?P<epid>\d+)|ss(?P<ssid>\d+))",
r"(?:bilibili\.com/(?:video|bangumi/play)|b23\.tv|acg\.tv)/(?:(?P<bvid>BV\w{10})|av(?P<aid>\d+)|ep(?P<epid>\d+)|ss(?P<ssid>\d+))",
url,
)
match_fes = re.search(r"bilibili\.com/festival/(?P<festivalid>\w+)\?(?:bvid=(?P<bvid>BV\w+))",url)
match_fes = re.search(
r"bilibili\.com/festival/(?P<festivalid>\w+)\?(?:bvid=(?P<bvid>BV\w{10}))", url
)
if match_fes:
festivalid = match_fes.group("festivalid")
bvid = match_fes.group("bvid")
Expand Down Expand Up @@ -850,8 +852,8 @@ async def relink(img):
@safe_parser
async def feed_parser(client: httpx.AsyncClient, url: str):
# bypass b23 short link
if re.search(r"b23\.tv/(?:av|BV)", url):
return await video_parser(client, url)
if re.search(r"BV\w{10}|av\d+|ep\d+|ss\d+", url):
return await video_parser(client, url if "/" in url else f"b23.tv/{url}")
r = await client.get(url)
url = str(r.url)
logger.debug(f"URL: {url}")
Expand Down Expand Up @@ -889,7 +891,9 @@ async def biliparser(urls) -> list[feed]:
tasks = list(
feed_parser(
client,
f"http://{url}" if not url.startswith(("http:", "https:")) else url,
f"http://{url}"
if not url.startswith(("http:", "https:", "av", "BV"))
else url,
)
for url in list(set(urls))
)
Expand Down
30 changes: 20 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
from database import cache_clear, db_close, db_init, db_status
from utils import LOCAL_MODE, compress, escape_markdown, headers, logger, referer_url

regex = r"(?i)[\w\.]*?(?:bilibili(?:bb)?\.com|(?:b23(?:bb)?|acg)\.tv)\S+"
regex = r"(?i)[\w\.]*?(?:bilibili(?:bb)?\.com|(?:b23(?:bb)?|acg)\.tv)\S+|av\d+|BV\w{10}"


sourcecodemarkup = InlineKeyboardMarkup(
Expand Down Expand Up @@ -445,7 +445,7 @@ async def inline_query_answer(inline_query, msg):
query = inline_query.query
helpmsg = [
InlineQueryResultArticle(
id=str(uuid4()),
id=uuid4().hex,
title="帮助",
description="将 Bot 添加到群组可以自动匹配消息, 请注意 Inline 模式存在限制: 只可发单张图,消耗设备流量。",
reply_markup=sourcecodemarkup,
Expand All @@ -468,7 +468,7 @@ async def inline_query_answer(inline_query, msg):
logger.warning(f"解析错误! {f}")
results = [
InlineQueryResultArticle(
id=str(uuid4()),
id=uuid4().hex,
title="解析错误!",
description=escape_markdown(f.__str__()),
input_message_content=InputTextMessageContent(
Expand All @@ -483,7 +483,7 @@ async def answer_results(f: feed, fallback: bool = False):
if not f.mediaurls:
results = [
InlineQueryResultArticle(
id=str(uuid4()),
id=uuid4().hex,
title=f.user,
description=f.content,
reply_markup=origin_link(f.url),
Expand All @@ -496,8 +496,18 @@ async def answer_results(f: feed, fallback: bool = False):
else:
if f.mediatype == "video":
results = [
InlineQueryResultPhoto(
id=uuid4().hex,
caption=captions(f, fallback, True),
title=f.user,
description=f.content,
parse_mode=None if fallback else ParseMode.MARKDOWN_V2,
photo_url=f.mediathumb + "@1280w.jpg",
reply_markup=origin_link(f.url),
thumbnail_url=f.mediathumb + "@512w_512h.jpg",
),
InlineQueryResultVideo(
id=str(uuid4()),
id=uuid4().hex,
caption=captions(f, fallback, True),
title=f.mediatitle,
description=f"{f.user}: {f.content}",
Expand All @@ -517,26 +527,26 @@ async def answer_results(f: feed, fallback: bool = False):
if f.mediadimention["rotate"]
else f.mediadimention["height"]
),
)
),
]
elif f.mediatype == "audio":
results = [
InlineQueryResultAudio(
id=str(uuid4()),
id=uuid4().hex,
caption=captions(f, fallback, True),
title=f.mediatitle,
audio_duration=f.mediaduration,
audio_url=referer_url(f.mediaurls[0], f.url),
parse_mode=None if fallback else ParseMode.MARKDOWN_V2,
performer=f.user,
reply_markup=origin_link(f.url),
)
),
]
else:
results = [
(
InlineQueryResultGif(
id=str(uuid4()),
id=uuid4().hex,
caption=captions(f, fallback, True),
title=f"{f.user}: {f.content}",
gif_url=img,
Expand All @@ -546,7 +556,7 @@ async def answer_results(f: feed, fallback: bool = False):
)
if ".gif" in img
else InlineQueryResultPhoto(
id=str(uuid4()),
id=uuid4().hex,
caption=captions(f, fallback, True),
title=f.user,
description=f.content,
Expand Down
2 changes: 2 additions & 0 deletions test/test_biliparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ async def test_dynamic_parser():
"https://www.bilibili.com/festival/bnj2024?bvid=BV1at421p79N", # 视频(活动)
"https://www.bilibili.com/video/BV1bW411n7fY/", # 视频(活动)
"https://b23.tv/BV1bW411n7fY", # 视频(活动)
"av912905698", # 视频(短链)
"BV1bW411n7fY", # 视频(短链)
]
for i in urls:
result = await biliparser(i)
Expand Down

0 comments on commit 9b6ad8e

Please sign in to comment.