Enhance post retrieval functions to include optional debug parameter for logging raw messages; update README.md with usage example for debug mode.
This commit is contained in:
@@ -88,6 +88,7 @@ SHOW_POST_FLAGS - optional, if set to true, will show post flags in html post vi
|
||||
``` curl https://pgbridge.example.com/rss/DragorWW_space?exclude_flags=video,stream,donat,clown ``` with exclude_flags parameter
|
||||
``` curl https://pgbridge.example.com/rss/DragorWW_space?exclude_text=реклама,акция ``` with exclude_text parameter
|
||||
``` curl https://pgbridge.example.com/rss/DragorWW_space?exclude_text="специальное предложение",акция ``` with exclude_text parameter containing phrases with spaces
|
||||
``` curl https://pgbridge.example.com/html/DragorWW_space/123?debug=true ``` with debug parameter to print raw message to logs
|
||||
|
||||
Warning: TG API has rate limit, and bridge will wait time before http response, if catch FloodWait exception. Increase http timeout in your client prevention timeout error. Examply, in miniflux: ENV HTTP_CLIENT_TIMEOUT=200
|
||||
|
||||
|
||||
+4
-4
@@ -443,7 +443,7 @@ def calculate_cache_stats():
|
||||
@app.get("/post/html/{channel}/{post_id}", response_class=HTMLResponse)
|
||||
@app.get("/html/{channel}/{post_id}/{token}", response_class=HTMLResponse)
|
||||
@app.get("/post/html/{channel}/{post_id}/{token}", response_class=HTMLResponse)
|
||||
async def get_post_html(channel: str, post_id: int, token: str | None = None):
|
||||
async def get_post_html(channel: str, post_id: int, token: str | None = None, debug: bool = False):
|
||||
if Config["token"]:
|
||||
if token != Config["token"]:
|
||||
logger.error(f"Invalid token for HTML post: {token}, expected: {Config['token']}")
|
||||
@@ -453,7 +453,7 @@ async def get_post_html(channel: str, post_id: int, token: str | None = None):
|
||||
|
||||
try:
|
||||
parser = PostParser(client.client)
|
||||
html_content = await parser.get_post(channel, post_id, 'html')
|
||||
html_content = await parser.get_post(channel, post_id, 'html', debug)
|
||||
if not html_content:
|
||||
raise HTTPException(status_code=404, detail="Post not found")
|
||||
return html_content
|
||||
@@ -467,7 +467,7 @@ async def get_post_html(channel: str, post_id: int, token: str | None = None):
|
||||
@app.get("/post/json/{channel}/{post_id}")
|
||||
@app.get("/json/{channel}/{post_id}/{token}")
|
||||
@app.get("/post/json/{channel}/{post_id}/{token}")
|
||||
async def get_post(channel: str, post_id: int, token: str | None = None):
|
||||
async def get_post(channel: str, post_id: int, token: str | None = None, debug: bool = False):
|
||||
if Config["token"]:
|
||||
if token != Config["token"]:
|
||||
logger.error(f"Invalid token for JSON post: {token}, expected: {Config['token']}")
|
||||
@@ -477,7 +477,7 @@ async def get_post(channel: str, post_id: int, token: str | None = None):
|
||||
|
||||
try:
|
||||
parser = PostParser(client.client)
|
||||
json_content = await parser.get_post(channel, post_id, 'json')
|
||||
json_content = await parser.get_post(channel, post_id, 'json', debug)
|
||||
if not json_content:
|
||||
raise HTTPException(status_code=404, detail="Post not found")
|
||||
return json_content
|
||||
|
||||
+12
-11
@@ -59,13 +59,12 @@ class PostParser:
|
||||
self.client = client
|
||||
|
||||
def _debug_message(self, message: Message) -> Message:
|
||||
if Config["debug"]:
|
||||
debug_message = copy.deepcopy(message)
|
||||
debug_message.sender_chat = None
|
||||
debug_message.caption_entities = None
|
||||
debug_message.reactions = None
|
||||
debug_message.entities = None
|
||||
print(debug_message)
|
||||
debug_message = copy.deepcopy(message)
|
||||
debug_message.sender_chat = None
|
||||
debug_message.caption_entities = None
|
||||
debug_message.reactions = None
|
||||
debug_message.entities = None
|
||||
print(debug_message)
|
||||
return
|
||||
|
||||
def channel_name_prepare(self, channel: str):
|
||||
@@ -75,13 +74,15 @@ class PostParser:
|
||||
else:
|
||||
return channel
|
||||
|
||||
async def get_post(self, channel: str, post_id: int, output_type: str = 'json') -> Union[str, Dict[Any, Any]]:
|
||||
async def get_post(self, channel: str, post_id: int, output_type: str = 'json', debug: bool = False) -> Union[str, Dict[Any, Any]]:
|
||||
print(f"Getting post {channel}, {post_id}")
|
||||
try:
|
||||
channel = self.channel_name_prepare(channel)
|
||||
message = await self.client.get_messages(channel, post_id)
|
||||
|
||||
self._debug_message(message)
|
||||
# Выводим отладочную информацию, если включен глобальный debug или параметр debug
|
||||
if Config["debug"] or debug:
|
||||
self._debug_message(message)
|
||||
|
||||
if not message:
|
||||
logger.error(f"post_not_found: channel {channel}, post_id {post_id}")
|
||||
@@ -519,12 +520,12 @@ class PostParser:
|
||||
links.append(f'<a href="tg://resolve?domain=c/{channel_id}&post={message.id}">Open in Telegram</a>')
|
||||
links.append(f'<a href="https://t.me/c/{channel_id}/{message.id}">Open in Web</a>')
|
||||
if Config['show_bridge_link']:
|
||||
links.append(f'<a href="{base_url}/html/{channel_identifier}/{message.id}">Open in Bridge</a>')
|
||||
links.append(f'<a href="{base_url}/html/{channel_identifier}/{message.id}?debug=true">Open in Bridge</a>')
|
||||
else: # For channels with username
|
||||
links.append(f'<a href="tg://resolve?domain={channel_identifier}&post={message.id}">Open in Telegram</a>')
|
||||
links.append(f'<a href="https://t.me/{channel_identifier}/{message.id}">Open in Web</a>')
|
||||
if Config['show_bridge_link']:
|
||||
links.append(f'<a href="{base_url}/html/{channel_identifier}/{message.id}">Open in Bridge</a>')
|
||||
links.append(f'<a href="{base_url}/html/{channel_identifier}/{message.id}?debug=true">Open in Bridge</a>')
|
||||
second_line_parts.extend(links)
|
||||
|
||||
if second_line_parts:
|
||||
|
||||
Reference in New Issue
Block a user