Commit 18ecdee

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-05-05 12:25:02
feat(subtitle): add `force_file` option to send subtitle as file
1 parent 4485297
Changed files (4)
src/preview/ytdlp.py
@@ -70,6 +70,7 @@ async def preview_ytdlp(
     append_transcription: bool = True,
     ytdlp_transcription_engine: str = "gemini",
     transcription_only: bool = False,
+    transcription_force_file: bool = False,
     to_telegraph: bool = True,
     **kwargs,
 ):
@@ -89,6 +90,7 @@ async def preview_ytdlp(
         append_transcription (bool, optional): Also append transcription.
         ytdlp_transcription_engine (str, optional): Method to get transcription.
         transcription_only (str, optional): If True, skip send video and audio file.
+        transcription_force_file (str, optional): If True, force to send transcription as file.
         to_telegraph (bool, optional): Whether to publish the subtitle or transcription to telegraph.
         delete_files (bool, optional): Whether to delete video & audio after uploading.
     """
@@ -260,7 +262,7 @@ async def preview_ytdlp(
             res = await asr_file(audio_path, ytdlp_transcription_engine, duration, client=client, message=message, slient=True)
             subtitles = res.get("texts", "")
         if subtitles:
-            if len(subtitles) > TEXT_LENGTH:
+            if len(subtitles) > TEXT_LENGTH or transcription_force_file:
                 caption = f"{emoji}[{info['author']}]({info['author_url']})\n🕒{create_time}\n📝[{info['title']}]({url})\n字符数: {len(subtitles)}\n阅读时长: {len(subtitles) / READING_SPEED:.1f}分钟"
                 if to_telegraph:
                     html = "\n".join([f"<p>{s}</p>" for s in subtitles.split("\n")])
src/subtitles/base.py
@@ -17,7 +17,7 @@ from networking import hx_req, match_social_media_link
 
 async def match_url(client: Client, message: Message) -> str:
     """Find valid url from message."""
-    info = parse_msg(message)
+    info = parse_msg(message, silent=True)
     if not startswith_prefix(info["text"], prefix=[PREFIX.SUBTITLE]):
         return ""
     # /subtitle "link"
src/subtitles/subtitle.py
@@ -29,7 +29,7 @@ HELP = f"""📃**提取字幕**
 """
 
 
-async def get_subtitle(client: Client, message: Message, youtube_subtitle_provider: str = PROVIDER.YOUTUBE_SUBTITLE, *, to_telegraph: bool = True, **kwargs):
+async def get_subtitle(client: Client, message: Message, youtube_subtitle_provider: str = PROVIDER.YOUTUBE_SUBTITLE, *, to_telegraph: bool = True, force_file: bool = False, **kwargs):
     """Get YouTube Subtitle."""
     target_chat = kwargs["target_chat"] if kwargs.get("target_chat") else message.chat.id
     # send docs if message == "/subtitle", without reply
@@ -58,7 +58,7 @@ async def get_subtitle(client: Client, message: Message, youtube_subtitle_provid
         await modify_progress(text=error, force_update=True, **kwargs)
         if this_info["mtype"] in ["audio", "video"] or reply_info.get("mtype", "") in ["audio", "video"]:
             msg = message if this_info["mtype"] in ["audio", "video"] else message.reply_to_message
-            fpath: str = await msg.download()  # type: ignore
+            fpath: str = await client.download_media(message)  # type: ignore
             asr_res = await asr_file(fpath, engine="gemini", client=client, message=message, **kwargs)
             if asr_res.get("error"):
                 await modify_progress(text=asr_res["error"], force_update=True, **kwargs)
@@ -72,6 +72,7 @@ async def get_subtitle(client: Client, message: Message, youtube_subtitle_provid
                 "url": url,
                 "append_transcription": True,
                 "transcription_only": True,
+                "transcription_force_file": force_file,
                 "youtube_comments_provider": False,
                 "bilibili_comments_provider": False,
                 "proxy": None,
@@ -85,8 +86,9 @@ async def get_subtitle(client: Client, message: Message, youtube_subtitle_provid
         return
     logger.success(subtitles)
     matched = await match_social_media_link(url)
-    vid = matched["vid"]
-    if vinfo := await fetch_youtube_video_info(vid):
+    platform = matched["platform"]
+    vid = matched.get("vid", matched.get("bvid", url))
+    if platform == "youtube" and (vinfo := await fetch_youtube_video_info(vid)):
         caption = f"🔴[{vinfo['author']}]({vinfo['channel']})\n🕒{vinfo['date']:%Y-%m-%d %H:%M:%S}\n"
         caption += f"📝[{vinfo['title']}]({url})\n字符数: {res['num_chars']}\n阅读时长: {res['reading_minutes']:.1f}分钟"
         if to_telegraph:
@@ -96,7 +98,7 @@ async def get_subtitle(client: Client, message: Message, youtube_subtitle_provid
         with io.BytesIO(subtitles.encode("utf-8")) as f:
             await client.send_document(to_int(target_chat), f, file_name=f"{vinfo['title']}.txt", caption=caption)
     else:
-        caption = f"原视频: [YouTube链接]({url})\n字符数: {res['num_chars']}\n阅读时长: {res['reading_minutes']:.1f}分钟"
+        caption = f"来源: [视频链接]({url})\n字符数: {res['num_chars']}\n阅读时长: {res['reading_minutes']:.1f}分钟"
         if to_telegraph:
             html = "\n".join([f"<p>{s}</p>" for s in subtitles.split("\n")])
             if telegraph_url := await publish_telegraph(title=f"{vid}字幕", html=html, url=url):
src/handler.py
@@ -81,6 +81,8 @@ async def handle_utilities(
         detail_progress (bool, optional): Show detailed progress (Only if show_proress is set to True). Defaults to False.
     """
     kwargs |= {"target_chat": target_chat, "reply_msg_id": reply_msg_id, "show_progress": show_progress, "detail_progress": detail_progress}
+    info = parse_msg(message)
+    kwargs |= params_from_msg_text(info["text"])  # merge the parameters from the message text
     if ai:
         await gpt_response(client, message, **kwargs)  # /ai /gpt /gemini /ds /qwen /doubao /grok
     if asr: