Commit 13eaf7c

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-05-06 07:07:51
style: improve Telegraph link format
1 parent 925ab2e
Changed files (6)
src/asr/voice_recognition.py
@@ -149,7 +149,7 @@ async def voice_to_text(
             if to_telegraph:
                 html = "\n".join([f"<p>{s}</p>" for s in texts.split("\n")])
                 if telegraph_url := await publish_telegraph(title=trigger_info["text"] or "语音识别结果", html=html, author=trigger_info["full_name"], url=trigger_info["message_url"]):
-                    caption = f"**⚡️[Telegraph即时预览]({telegraph_url})**"
+                    caption = f"⚡️[Telegraph即时预览]({telegraph_url})"
             with io.BytesIO(texts.encode("utf-8")) as f:
                 await client.send_document(to_int(target_chat), f, file_name="语音识别结果.txt", caption=caption, reply_parameters=reply_parameters)
         await modify_progress(del_status=True, **kwargs)
src/messages/utils.py
@@ -3,13 +3,15 @@
 
 import re
 
+from loguru import logger
+from pyrogram.client import Client
 from pyrogram.enums import ParseMode
 from pyrogram.parser.markdown import BLOCKQUOTE_EXPANDABLE_DELIM, BLOCKQUOTE_EXPANDABLE_END_DELIM
 from pyrogram.parser.parser import Parser
-from pyrogram.types import ReplyParameters
+from pyrogram.types import Message, ReplyParameters
 
 from config import TEXT_LENGTH
-from utils import readable_size, to_int
+from utils import myself, readable_size, to_int
 
 
 def startswith_prefix(text: str | None = None, prefix: list[str] | None = None, ignore_prefix: list[str] | None = None) -> bool:
@@ -157,3 +159,14 @@ def warp_comments(texts: str) -> str:
         return BLOCKQUOTE_EXPANDABLE_DELIM + texts
 
     return texts
+
+
+async def sent_from_me(client: Client, message: Message) -> bool:
+    """Check if this clinet is a bot or not."""
+    try:
+        me = await myself(client)
+        uid = message.from_user.id if message.from_user else 1
+    except Exception as e:
+        logger.error(e)
+        return False
+    return me.id == uid
src/preview/wechat.py
@@ -52,7 +52,7 @@ async def preview_wechat(client: Client, message: Message, url: str = "", db_key
             texts = f"{post_info['header']}"
             telegraph_url = await publish_telegraph(title=post_info["title"], html=post_info["html"], author=post_info["author"], url=url)
             if telegraph_url:
-                texts += f"\n[⚡️Telegraph即时预览]({telegraph_url})"
+                texts += f"\n⚡️[Telegraph即时预览]({telegraph_url})"
             sent_messages.extend(await send2tg(client, message, texts=texts, media=[{"document": post_info["html_path"]}], **kwargs))
     elif length < CAPTION_LENGTH - 8:  # 有图片短文
         texts = f"{post_info['header']}\n{BLOCKQUOTE_EXPANDABLE_DELIM}{post_info['markdown']}\n{BLOCKQUOTE_EXPANDABLE_END_DELIM}"
@@ -61,7 +61,7 @@ async def preview_wechat(client: Client, message: Message, url: str = "", db_key
         texts = f"{post_info['header']}"
         telegraph_url = await publish_telegraph(title=post_info["title"], html=post_info["html"], author=post_info["author"], url=url)
         if telegraph_url:
-            texts += f"\n**⚡️[Telegraph即时预览]({telegraph_url})**"
+            texts += f"\n⚡️[Telegraph即时预览]({telegraph_url})"
         sent_messages.extend(await send2tg(client, message, texts=texts, media=[{"document": post_info["path"]}], **kwargs))
         kwargs["reply_msg_id"] = -1  # do not send as reply
         sent_messages.extend(await send2tg(client, message, texts=texts, media=post_info["media"], **kwargs))
src/preview/ytdlp.py
@@ -265,7 +265,7 @@ async def preview_ytdlp(
                 if to_telegraph:
                     html = "\n".join([f"<p>{s}</p>" for s in subtitles.split("\n")])
                     if telegraph_url := await publish_telegraph(title=info["title"], html=html, author=info["author"], url=url):
-                        caption += f"\n**⚡️[Telegraph即时预览]({telegraph_url})**"
+                        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"{info['title']}.txt", caption=caption)
             else:
src/subtitles/subtitle.py
@@ -99,7 +99,7 @@ async def get_subtitle(client: Client, message: Message, youtube_subtitle_provid
         if to_telegraph:
             html = "\n".join([f"<p>{s}</p>" for s in subtitles.split("\n")])
             if telegraph_url := await publish_telegraph(title=vinfo["title"], html=html, author=vinfo["author"], url=url):
-                caption += f"\n**⚡️[Telegraph即时预览]({telegraph_url})**"
+                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"{vinfo['title']}.txt", caption=caption)
     else:
@@ -107,7 +107,7 @@ async def get_subtitle(client: Client, message: Message, youtube_subtitle_provid
         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):
-                caption += f"\n**⚡️[Telegraph即时预览]({telegraph_url})**"
+                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)
 
src/utils.py
@@ -18,6 +18,7 @@ from bs4.element import PageElement
 from glom import Coalesce, PathAccessError, glom
 from loguru import logger
 from pyrogram.client import Client
+from pyrogram.types import User
 from telegraph.aio import Telegraph
 from yt_dlp.extractor import gen_extractors
 
@@ -228,16 +229,26 @@ def ts_to_dt(ts: str | float | None) -> datetime | None:
         return None
 
 
+async def myself(client: Client) -> User:
+    """Get myself info."""
+    if cache.get("me"):
+        return cache.get("me")
+    try:
+        me = await client.get_me()
+    except Exception as e:
+        logger.error(e)
+        return User(id=1, is_bot=False)
+    cache.set("me", me, ttl=0)
+    return me
+
+
 async def i_am_bot(client: Client) -> bool:
     """Check if this clinet is a bot or not."""
-    if cache.get("i_am_bot"):
-        return cache.get("i_am_bot")
     try:
-        me = await client.get_me()
+        me = await myself(client)
     except Exception as e:
         logger.error(e)
         return False
-    cache.set("i_am_bot", me.is_bot)
     return me.is_bot