Commit bbb54f9
Changed files (2)
src
subtitles
src/subtitles/base.py
@@ -21,12 +21,12 @@ async def match_url(client: Client, message: Message) -> str:
return ""
# /subtitle "link"
matched = await match_social_media_link(info["text"])
+ if matched["platform"] in ["youtube", "bilibili"]:
+ return matched["url"]
for entity_url in info["entity_urls"]:
matched = await match_social_media_link(entity_url)
if matched["platform"] in ["youtube", "bilibili"]:
return matched["url"]
- if matched["platform"] in ["youtube", "bilibili"]:
- return matched["url"]
# is replying to message?
if not message.reply_to_message:
@@ -37,12 +37,12 @@ async def match_url(client: Client, message: Message) -> str:
for msg in reply_messages:
info = parse_msg(msg, silent=True)
matched = await match_social_media_link(info["text"])
+ if matched["platform"] in ["youtube", "bilibili"]:
+ return matched["url"]
for entity_url in info["entity_urls"]:
matched = await match_social_media_link(entity_url)
if matched["platform"] in ["youtube", "bilibili"]:
return matched["url"]
- if matched["platform"] in ["youtube", "bilibili"]:
- return matched["url"]
return ""
src/subtitles/subtitle.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
+import contextlib
import io
from loguru import logger
@@ -60,19 +61,18 @@ async def get_subtitle(client: Client, message: Message, youtube_subtitle_provid
res = await fetch_subtitle(url, youtube_subtitle_provider)
if error := res.get("error", ""):
- await modify_progress(text=error, force_update=True, **kwargs)
if this_info["mtype"] in ["audio", "video"] or reply_info.get("mtype", "") in ["audio", "video"]:
+ await modify_progress(text=error + "\n正在通过ASR识别字幕", force_update=True, **kwargs)
msg = message if this_info["mtype"] in ["audio", "video"] else message.reply_to_message
- fpath: str = await client.download_media(message) # type: ignore
+ fpath: str = await client.download_media(msg) # type: ignore
engine = "gemini" if platform == "youtube" else "" # use gemini to bypass censorship
- asr_res = await asr_file(fpath, engine=engine, client=client, message=message, **kwargs)
- if asr_res.get("error"):
- await modify_progress(text=asr_res["error"], force_update=True, **kwargs)
+ res = await asr_file(fpath, engine=engine, client=client, message=message, silent=True, **kwargs)
+ if res.get("error"):
+ await modify_progress(text=res["error"], force_update=True, **kwargs)
return
- res = {"subtitles": asr_res["texts"], "num_chars": len(asr_res["texts"]), "reading_minutes": len(asr_res["texts"]) / READING_SPEED}
- if asr_res.get("telegraph"):
- res["telegraph"] = asr_res["telegraph"]
+ res |= {"subtitles": res["texts"], "num_chars": len(res["texts"]), "reading_minutes": len(res["texts"]) / READING_SPEED}
else:
+ await modify_progress(text=error + "\n正在通过下载音频后ASR识别字幕", force_update=True, **kwargs)
kwargs |= {
"show_progress": False,
"url": url,
@@ -110,6 +110,6 @@ async def get_subtitle(client: Client, message: Message, youtube_subtitle_provid
caption += f"\n⚡️[Telegraph即时预览]({telegraph_url})"
with io.BytesIO(subtitles.encode("utf-8")) as f:
await client.send_document(to_int(target_chat), f, file_name=f"{vid}字幕.txt", caption=caption)
-
- [await modify_progress(msg, del_status=True) for msg in res.get("sent_messages", [])]
- await modify_progress(del_status=True, **kwargs)
+ with contextlib.suppress(Exception):
+ [await modify_progress(msg, del_status=True) for msg in res.get("sent_messages", [])]
+ await modify_progress(del_status=True, **kwargs)