Commit 82d8d28

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-04-30 03:20:40
feat(telegraph): convert text to HTML before publishing
1 parent e0e2715
Changed files (3)
src/utils.py
@@ -1,5 +1,6 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
+import contextlib
 import json
 import random
 import re
@@ -10,9 +11,10 @@ from pathlib import Path
 from typing import Any
 from zoneinfo import ZoneInfo
 
+import markdown
 import zhconv
 from bs4.element import PageElement
-from glom import PathAccessError, glom
+from glom import Coalesce, PathAccessError, glom
 from loguru import logger
 from pyrogram.client import Client
 from telegraph.aio import Telegraph
@@ -367,11 +369,21 @@ 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:
+async def publish_telegraph(title: str, texts: str | None = None, html: str | None = None, author: str | None = None, url: str | None = None) -> str:
     """Publish to Telegraph."""
-    if not TOKEN.TELEGRAPH or not html:
+    if not TOKEN.TELEGRAPH:
         return ""
+    if not (texts or html):
+        return ""
+    if texts:
+        html = markdown.markdown(texts)
     telegraph = Telegraph(access_token=TOKEN.TELEGRAPH)
+    account_info = {}
+    if not (author and url):
+        with contextlib.suppress(Exception):
+            if account_info := await telegraph.get_account_info():
+                author = glom(account_info, Coalesce("result.short_name", "result.author_name"), default=None)
+                url = glom(account_info, "result.author_url", default=None)
     try:
         page = await telegraph.create_page(title=title, author_name=author, author_url=url, html_content=html)
         return page["url"]
pyproject.toml
@@ -10,6 +10,8 @@ dependencies = [
   "httpx-curl-cffi>=0.1.3",
   "httpx[http2,socks]>=0.28.1",
   "loguru>=0.7.2",
+  "markdown>=3.8",
+  "markitdown[docx,pdf,pptx,xls,xlsx]>=0.1.1",
   "openai>=1.60.1",
   "pillow-heif>=0.18.0",
   "pillow>=10.4.0",
@@ -27,7 +29,6 @@ dependencies = [
   "youtube-transcript-api>=0.6.3",
   "yt-dlp>=2025.1.12rc",
   "zhconv>=1.4.3",
-  "markitdown[docx,pdf,pptx,xls,xlsx]>=0.1.1",
 ]
 name = "bennybot"
 requires-python = ">=3.11"
uv.lock
@@ -228,6 +228,7 @@ dependencies = [
     { name = "httpx", extra = ["http2", "socks"] },
     { name = "httpx-curl-cffi" },
     { name = "loguru" },
+    { name = "markdown" },
     { name = "markitdown", extra = ["docx", "pdf", "pptx", "xls", "xlsx"] },
     { name = "openai" },
     { name = "pillow" },
@@ -265,6 +266,7 @@ requires-dist = [
     { name = "httpx", extras = ["http2", "socks"], specifier = ">=0.28.1" },
     { name = "httpx-curl-cffi", specifier = ">=0.1.3" },
     { name = "loguru", specifier = ">=0.7.2" },
+    { name = "markdown", specifier = ">=3.8" },
     { name = "markitdown", extras = ["docx", "pdf", "pptx", "xls", "xlsx"], specifier = ">=0.1.1" },
     { name = "openai", specifier = ">=1.60.1" },
     { name = "pillow", specifier = ">=10.4.0" },
@@ -1076,6 +1078,15 @@ wheels = [
     { url = "https://files.pythonhosted.org/packages/d0/ab/f8e63fcabc127c6efd68b03633c189ee799a5304fa96c036a325a2894bcb/mammoth-1.9.0-py2.py3-none-any.whl", hash = "sha256:0eea277316586f0ca65d86834aec4de5a0572c83ec54b4991f9bb520a891150f", size = 52901 },
 ]
 
+[[package]]
+name = "markdown"
+version = "3.8"
+source = { registry = "https://pypi.org/simple" }
+sdist = { url = "https://files.pythonhosted.org/packages/2f/15/222b423b0b88689c266d9eac4e61396fe2cc53464459d6a37618ac863b24/markdown-3.8.tar.gz", hash = "sha256:7df81e63f0df5c4b24b7d156eb81e4690595239b7d70937d0409f1b0de319c6f", size = 360906 }
+wheels = [
+    { url = "https://files.pythonhosted.org/packages/51/3f/afe76f8e2246ffbc867440cbcf90525264df0e658f8a5ca1f872b3f6192a/markdown-3.8-py3-none-any.whl", hash = "sha256:794a929b79c5af141ef5ab0f2f642d0f7b1872981250230e72682346f7cc90dc", size = 106210 },
+]
+
 [[package]]
 name = "markdownify"
 version = "1.1.0"