Commit 1e90349

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-02-02 04:19:38
fix(subtitle): fix subtitle link detection
1 parent 9bf0e4f
Changed files (3)
src/others/subtitle.py
@@ -39,9 +39,9 @@ async def get_subtitle(client: Client, message: Message, **kwargs):
     if equal_prefix(message.text, prefix=[PREFIX.SUBTITLE]) and not message.reply_to_message:
         await send2tg(client, message, texts=HELP, **kwargs)
         return
-
     if not (vid := await find_yt_vid(client, message)):
         return
+
     yt_url = f"https://www.youtube.com/watch?v={vid}"
     msg = f"🔍**正在获取字幕**\n{yt_url}"
     if kwargs.get("show_progress"):
@@ -58,7 +58,7 @@ async def get_subtitle(client: Client, message: Message, **kwargs):
         if subtitles := res.get("subtitle", ""):
             length = len(subtitles)
             with io.BytesIO(subtitles.encode("utf-8")) as f:
-                await client.send_document(to_int(target_chat), f, file_name=f"vtt字幕-{length}字符.txt", caption=f"{vid}[{yt_url}]")
+                await client.send_document(to_int(target_chat), f, file_name=f"vtt字幕-{length}字符.txt", caption=f"原视频: [{vid}]({yt_url})")
         elif error := res.get("error", ""):
             await modify_progress(text=error, force_update=True, **kwargs)
             await asyncio.sleep(3)
@@ -72,13 +72,14 @@ async def find_yt_vid(client: Client, message: Message) -> str:
     info = parse_msg(message)
     if not startswith_prefix(info["text"], prefix=[PREFIX.SUBTITLE]):
         return ""
-
     # /subtitle "link"
-    if entity_urls := info["entity_urls"]:
-        url = entity_urls[0]
-        matched = await match_social_media_link(url, flatten_first=True)
+    matched = await match_social_media_link(info["text"], flatten_first=True)
+    for entity_url in info["entity_urls"]:
+        matched = await match_social_media_link(entity_url, flatten_first=True)
         if matched["platform"] == "youtube":
             return matched["vid"]
+    if matched["platform"] == "youtube":
+        return matched["vid"]
 
     # is replying to message?
     if not message.reply_to_message:
@@ -88,10 +89,11 @@ async def find_yt_vid(client: Client, message: Message) -> str:
     reply_messages = await client.get_media_group(message.chat.id, message.id) if message.media_group_id else [reply_message]
     for msg in reply_messages:
         info = parse_msg(msg, silent=True)
-        if not info["entity_urls"]:
-            continue
-        url = info["entity_urls"][0]
-        matched = await match_social_media_link(url, flatten_first=True)
+        matched = await match_social_media_link(info["text"], flatten_first=True)
+        for entity_url in info["entity_urls"]:
+            matched = await match_social_media_link(entity_url, flatten_first=True)
+            if matched["platform"] == "youtube":
+                return matched["vid"]
         if matched["platform"] == "youtube":
             return matched["vid"]
     return ""
src/handler.py
@@ -186,7 +186,7 @@ async def handle_social_media(
         if xhs and matched["platform"] == "xiaohongshu" and ENABLE.XHS:
             await preview_xhs(client, message, **kwargs)
         try:
-            if ytdlp and matched["platform"] == "ytdlp" and ENABLE.YTDLP:
+            if ytdlp and ENABLE.YTDLP and any(matched["platform"] == x for x in ["bilibili", "youtube", "ytdlp"]):
                 await preview_ytdlp(client, message, **kwargs)
         except ProxyError:
             logger.error(f"🚫{matched['platform']}代理错误")
src/networking.py
@@ -386,17 +386,17 @@ async def match_social_media_link(text: str, *, flatten_first: bool = False) ->
         queries = parse_qs(urlparse(matched.group(0)).query)
         pid = queries.get("p", ["1"])[0]
         url = f"https://www.bilibili.com/video/{bvid}?p={pid}".removesuffix("?p=1")
-        return {"url": url, "db_key": bare_url(url), "bvid": bvid, "pid": pid, "platform": "ytdlp"}
+        return {"url": url, "db_key": bare_url(url), "bvid": bvid, "pid": pid, "platform": "bilibili"}
 
     # https://www.youtube.com/watch?v=D6aE2E0RHTc
     if matched := re.search(r"(https?://)?(:?m\.|www\.)?youtube\.com/watch([^,,.。\s]+)", text):
         queries = parse_qs(urlparse(matched.group(0)).query)
         if vid := queries.get("v", [""])[0]:
-            return {"url": f"https://www.youtube.com/watch?v={vid}", "db_key": f"www.youtube.com/watch?v={vid}", "vid": vid, "platform": "ytdlp"}
+            return {"url": f"https://www.youtube.com/watch?v={vid}", "db_key": f"www.youtube.com/watch?v={vid}", "vid": vid, "platform": "youtube"}
     # https://youtube.com/shorts/lFKHbluAlJw
     if matched := re.search(r"(https?://)?(:?m\.|www\.)?youtube\.com/shorts/([^,,.。?\s]+)", text):
         vid = matched.group(3)
-        return {"url": f"https://www.youtube.com/watch?v={vid}", "db_key": f"www.youtube.com/watch?v={vid}", "vid": vid, "platform": "ytdlp"}
+        return {"url": f"https://www.youtube.com/watch?v={vid}", "db_key": f"www.youtube.com/watch?v={vid}", "vid": vid, "platform": "youtube"}
 
     # if all above pre-defined patterns failed, try to match ytdlp link
     if urls := match_urls(text):