diff --git a/api_server.py b/api_server.py index b025b5b..2cf611d 100644 --- a/api_server.py +++ b/api_server.py @@ -1,4 +1,5 @@ from fastapi import FastAPI, HTTPException, Response +from fastapi.responses import HTMLResponse from telegram_client import TelegramClient from config import get_settings import logging @@ -17,10 +18,35 @@ async def startup(): async def shutdown(): await client.stop() -@app.get("/post/{channel}/{post_id}") +@app.get("/html/{channel}/{post_id}", response_class=HTMLResponse) +async def get_post_html(channel: str, post_id: int): + try: + post = await client.get_post(channel, post_id) + if post.get("error"): + raise HTTPException( + status_code=404, + detail=post["details"] + ) + return f""" + +
+ {post['text']} + + + """ + except Exception as e: + logger.error(f"HTML endpoint error: {str(e)}") + raise HTTPException(status_code=500, detail=str(e)) + +@app.get("/json/{channel}/{post_id}") async def get_post(channel: str, post_id: int): try: data = await client.get_post(channel, post_id) + if data.get("error"): + raise HTTPException( + status_code=404, + detail=data + ) return Response( content=json.dumps(data), media_type="application/json" diff --git a/config.py b/config.py index 46c3a2c..a3fc832 100644 --- a/config.py +++ b/config.py @@ -8,6 +8,13 @@ API_PORT = int(os.getenv("API_PORT", 8000)) REQUEST_TIMEOUT = int(os.getenv("REQUEST_TIMEOUT", 30)) SESSION_STRING = os.getenv("TG_SESSION_STRING", "") +DEFAULT_CONFIG = { + "session_string": "", + "tg_api_id": 12345, + "tg_api_hash": "xxxxxxxx", + "file_access_flags": ["can_access_files"] +} + def get_settings(): return { "tg_api_id": TG_API_ID, diff --git a/requirements.txt b/requirements.txt index 2671fd3..9bb8c01 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,5 @@ fastapi uvicorn -pyrogram +kurigram tgcrypto -pillow>=10.0.0 \ No newline at end of file +pillow \ No newline at end of file diff --git a/telegram_client.py b/telegram_client.py index 4c1f09b..4fb8629 100644 --- a/telegram_client.py +++ b/telegram_client.py @@ -39,30 +39,40 @@ class TelegramClient: async def _parse_message(self, message: Message) -> dict: # Extract text from different sources - text = message.text or "" + text = message.text.html if message.text else "" # Get HTML formatted text # Try to get caption for media messages - if not text and hasattr(message, "caption"): - text = message.caption or "" + if not text and hasattr(message, "caption") and message.caption: + text = message.caption.html # Try to get text from other attributes if not text and hasattr(message, "action"): - text = str(message.action) # For service messages + text = str(message.action) + elif not text and hasattr(message, "entities"): + # Extract HTML from entities + text = message.text.html if message.text else "" # Parse media and add video markers media = [] if message.media: - media_obj = message.media - if hasattr(message, "video"): - media.append(await self._parse_media(message.video)) + # Handle animations directly from message object + if hasattr(message, "animation") and message.animation: + animation = message.animation + media.append(await self._parse_animation(animation)) + # Handle videos directly from message object + elif hasattr(message, "video") and message.video: + video = message.video + media.append(await self._parse_media(video)) + # Handle photos directly from message object + elif hasattr(message, "photo") and message.photo: + photo = message.photo + media.append(await self._parse_media(photo)) else: - if isinstance(media_obj, list): - for m in media_obj: - media.append(await self._parse_media(m)) - elif hasattr(media_obj, "photo"): - media.append(await self._parse_media(media_obj.photo)) - else: - media.append(await self._parse_media(media_obj)) + media_obj = message.media + parsed_media = await self._parse_media(media_obj) + if parsed_media["type"] not in ["webpage", "nonetype"]: + media.append(parsed_media) + logger.debug(f"Parsed media: {parsed_media}") video_count = sum(1 for m in media if m.get("type") == "video") if video_count > 0: @@ -77,19 +87,35 @@ class TelegramClient: ) text += reactions_text + # Add media previews to text + if media: + previews = [] + for m in media: + if m.get('thumbnail'): + previews.append(f"