diff --git a/post_parser.py b/post_parser.py index bb27ca7..5145b7a 100644 --- a/post_parser.py +++ b/post_parser.py @@ -51,6 +51,7 @@ if not logger.handlers: # https://t.me/red_spades/1222 many media + text #https://t.me/ni404head/1283 file +#http://127.0.0.1:8000/rss/-1002069358234 channel with numeric ID class PostParser: def __init__(self, client): @@ -77,8 +78,13 @@ class PostParser: return async def get_post(self, channel: str, post_id: int, output_type: str = 'json') -> Union[str, Dict[Any, Any]]: + print(f"Getting post {channel}, {post_id}") try: - message = await self.client.get_messages(channel, post_id) + channel_id = channel + if isinstance(channel, str) and channel.startswith('-100'): # Convert numeric channel ID to int + channel_id = int(channel) + + message = await self.client.get_messages(channel_id, post_id) self._debug_message(message) @@ -525,22 +531,16 @@ class PostParser: logger.error(f"file_id_collection_error: message_id {message.id}, error {str(e)}") def get_channel_username(self, message): - """Extract channel username from message""" + """Extract channel username or ID from message""" chat = message.chat if hasattr(message, 'chat') else message - if not chat: - return None - - # Check for usernames - if hasattr(chat, 'usernames') and chat.usernames: - # Return first active username from the list + if not chat: return None + if isinstance(chat.id, int) and str(chat.id).startswith('-100'): return str(chat.id) # Return full ID for channels with numeric ID + if hasattr(chat, 'usernames') and chat.usernames: # Check many usernames active_usernames = [u.username for u in chat.usernames if u.active] if active_usernames: return active_usernames[0] + if hasattr(chat, 'username') and chat.username: return chat.username # Check single username + if isinstance(chat.id, int) and str(chat.id).startswith('-100'): return str(chat.id) # Return full ID if no username found - # Check for single username as fallback - if hasattr(chat, 'username') and chat.username: - return chat.username - - # If no username found - logger.error(f"channel_username_error: no username found for chat in message {getattr(message, 'id', 'unknown')}") + logger.error(f"channel_username_error: no username or valid ID found for chat") return None diff --git a/rss_generator.py b/rss_generator.py index d6ec6b1..fa0fee9 100644 --- a/rss_generator.py +++ b/rss_generator.py @@ -179,7 +179,12 @@ async def generate_channel_rss(channel: str, post_parser: Optional[PostParser] = base_url = Config['pyrogram_bridge_url'] try: - channel_info = await post_parser.client.get_chat(channel) + # Преобразуем числовой ID канала обратно в int, если это возможно + channel_id = channel + if isinstance(channel, str) and channel.startswith('-100'): + channel_id = int(channel) + + channel_info = await post_parser.client.get_chat(channel_id) channel_title = channel_info.title or f"Telegram: {channel}" channel_username = post_parser.get_channel_username(channel_info) if not channel_username: @@ -200,7 +205,7 @@ async def generate_channel_rss(channel: str, post_parser: Optional[PostParser] = # Collect messages messages = [] - async for message in post_parser.client.get_chat_history(channel, limit=limit): + async for message in post_parser.client.get_chat_history(channel_id, limit=limit): messages.append(message) # Process messages into formatted posts @@ -259,7 +264,12 @@ async def generate_channel_html(channel: str, post_parser: Optional[PostParser] base_url = Config['pyrogram_bridge_url'] try: - channel_info = await post_parser.client.get_chat(channel) + # Преобразуем числовой ID канала обратно в int, если это возможно + channel_id = channel + if isinstance(channel, str) and channel.startswith('-100'): + channel_id = int(channel) + + channel_info = await post_parser.client.get_chat(channel_id) channel_title = channel_info.title or f"Telegram: {channel}" channel_username = post_parser.get_channel_username(channel_info) if not channel_username: @@ -276,7 +286,7 @@ async def generate_channel_html(channel: str, post_parser: Optional[PostParser] # Collect messages messages = [] - async for message in post_parser.client.get_chat_history(channel, limit=limit): + async for message in post_parser.client.get_chat_history(channel_id, limit=limit): messages.append(message) # Process messages into formatted posts