Commit e0e2715
src/preview/wechat.py
@@ -8,7 +8,6 @@ from loguru import logger
from pyrogram.client import Client
from pyrogram.parser.markdown import BLOCKQUOTE_EXPANDABLE_DELIM, BLOCKQUOTE_EXPANDABLE_END_DELIM
from pyrogram.types import Message
-from telegraph.aio import Telegraph
from config import API, CAPTION_LENGTH, DB, DOWNLOAD_DIR, PROXY, TEXT_LENGTH, TOKEN
from database import get_db
@@ -17,7 +16,7 @@ from messages.progress import modify_progress
from messages.sender import send2tg
from messages.utils import count_without_entities, summay_media
from networking import download_file, download_media, hx_req
-from utils import nowstr, rand_string
+from utils import nowstr, publish_telegraph, rand_string
async def preview_wechat(client: Client, message: Message, url: str = "", db_key: str = "", **kwargs):
@@ -116,16 +115,3 @@ async def get_wechat_info(url: str, **kwargs) -> dict:
logger.error(e)
return {"error": str(e)}
return {"html": htmls, "path": txt_path.as_posix(), "markdown": markdowns, "media": media, "title": title, "author": author, "header": header}
-
-
-async def publish_telegraph(title: str, html: str, author: str = "", url: str = "") -> str:
- """Publish to Telegraph."""
- if not TOKEN.TELEGRAPH or not html:
- return ""
- telegraph = Telegraph(access_token=TOKEN.TELEGRAPH)
- try:
- page = await telegraph.create_page(title=title, author_name=author, author_url=url, html_content=html)
- return page["url"]
- except Exception as e:
- logger.error(f"Telegraph publish error: {e}")
- return ""
src/utils.py
@@ -1,6 +1,5 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
-
import json
import random
import re
@@ -16,9 +15,10 @@ from bs4.element import PageElement
from glom import PathAccessError, glom
from loguru import logger
from pyrogram.client import Client
+from telegraph.aio import Telegraph
from yt_dlp.extractor import gen_extractors
-from config import DOWNLOAD_DIR, TZ, cache
+from config import DOWNLOAD_DIR, TOKEN, TZ, cache
# ruff: noqa: RUF001
@@ -367,6 +367,19 @@ def cleanup_old_files(root: Path | str | None = None, duration: int = 7200) -> N
path.unlink(missing_ok=True)
+async def publish_telegraph(title: str, html: str, author: str | None = None, url: str | None = None) -> str:
+ """Publish to Telegraph."""
+ if not TOKEN.TELEGRAPH or not html:
+ return ""
+ telegraph = Telegraph(access_token=TOKEN.TELEGRAPH)
+ try:
+ page = await telegraph.create_page(title=title, author_name=author, author_url=url, html_content=html)
+ return page["url"]
+ except Exception as e:
+ logger.error(f"Telegraph publish error: {e}")
+ return ""
+
+
def zhcn(text: str) -> str:
"""Convert zh-tw to zh-cn."""
return zhconv.convert_for_mw(text, locale="zh-cn")