From 54b3175bdfe6620d02af8d7bbc7888898fbfbd2d Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Mon, 24 Mar 2025 16:51:29 +0300 Subject: [PATCH] Refactor PostParser to improve URL handling and conditional hyperlinking for short texts --- post_parser.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/post_parser.py b/post_parser.py index a119a6c..4bbd430 100644 --- a/post_parser.py +++ b/post_parser.py @@ -126,8 +126,11 @@ class PostParser: text = message.text or message.caption or '' # Check if the text contains only a URL and nothing else - if text.strip() and re.match(r'^https?://[^\s]+$', text.strip()): - return "🔗 Web link" + if text.strip(): + # Remove all URLs from text + text_without_urls = re.sub(r'https?://[^\s<>"\']+', '', text.strip()) + if not text_without_urls: + return "🔗 Web link" if not text: if message.media == MessageMediaType.PHOTO: return "📷 Photo" @@ -224,7 +227,7 @@ class PostParser: # Add flag "video" if the message media is VIDEO or ANIMATION and the body text is up to 100 characters. if (message.media in [MessageMediaType.VIDEO, MessageMediaType.ANIMATION] and - len(message_text.strip()) <= 200): + len((message.text or message.caption or '').strip()) <= 100): flags.append("video") # Add flag for posts without images @@ -400,7 +403,9 @@ class PostParser: else: text = '' text = text.replace('\n', '
') # Replace newlines with
- text = self._add_hyperlinks_to_raw_urls(text) # Add hyperlinks to raw URLs + # Add hyperlinks to raw URLs only if original text is short + if len((message.text or message.caption or '').strip()) <= 10: + text = self._add_hyperlinks_to_raw_urls(text) if text: # Message text content_body.append(f'
{text}
') @@ -493,9 +498,7 @@ class PostParser: if description := getattr(webpage, "description", None): # Process the description to handle line breaks and possibly HTML processed_description = description.replace('\n', '
') - # Check for unlinked URLs and turn them into links only if description is short - if len(processed_description.strip()) <= 10: - processed_description = self._add_hyperlinks_to_raw_urls(processed_description) + processed_description = self._add_hyperlinks_to_raw_urls(processed_description) html_parts.append(f'
{processed_description}
') # Display URL for non-telegram links or when display_url is available