From 78255e51a28409b939d8146a7d96bd137b21bba5 Mon Sep 17 00:00:00 2001 From: vvzvlad Date: Fri, 11 Apr 2025 02:48:42 +0300 Subject: [PATCH] Enhance post parser to flag foreign channels for boost links --- post_parser.py | 16 ++++++++++++-- tests/test_post_parser_flags.py | 39 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 2 deletions(-) diff --git a/post_parser.py b/post_parser.py index 06603cd..d275e16 100644 --- a/post_parser.py +++ b/post_parser.py @@ -353,15 +353,27 @@ class PostParser: hidden_links = re.findall(r'https?://(?:www\.)?t\.me/\+([A-Za-z0-9]+)', message_text) # Find links without a '+' after t.me/ indicating an open (foreign) channel link. open_links = re.findall(r'https?://(?:www\.)?t\.me/(?!\+)([A-Za-z0-9_]+)', message_text) + + # Find links with pattern t.me/boost/channel_name + boost_links = re.findall(r'https?://(?:www\.)?t\.me/boost/([A-Za-z0-9_]+)', message_text) + if hidden_links: flags.append("hid_channel") current_channel = self.get_channel_username(message) - # Add the flag "foreign_channel" if there's an open link that differs from the current channel. + + # Check regular open links for open_link in open_links: - if current_channel is None or open_link.lower() != current_channel.lower(): + if current_channel is None or (open_link.lower() != current_channel.lower() and open_link.lower() != 'boost'): flags.append("foreign_channel") break + + # Check boost links separately - only consider as foreign if the boosted channel is not current channel + if not "foreign_channel" in flags: + for boost_channel in boost_links: + if current_channel is None or boost_channel.lower() != current_channel.lower(): + flags.append("foreign_channel") + break except Exception as e: logger.error(f"tme_link_extraction_error: message_id {message.id}, error {str(e)}") diff --git a/tests/test_post_parser_flags.py b/tests/test_post_parser_flags.py index a4efd98..363b3c9 100644 --- a/tests/test_post_parser_flags.py +++ b/tests/test_post_parser_flags.py @@ -295,6 +295,45 @@ class TestPostParserExtractFlags(unittest.TestCase): self.parser.get_channel_username = MagicMock(return_value=None) # Simulate no current channel found self.assertIn("foreign_channel", self.parser._extract_flags(message)) # Should still flag as foreign if current unknown + def test_flag_foreign_channel_boost_other_channel(self): + """Test that boost link to foreign channel is flagged.""" + message = self._create_mock_message(text="Boost another channel: https://t.me/boost/other_channel", chat_username="test_channel") + self.parser.get_channel_username = MagicMock(return_value="test_channel") # Ensure current channel is mocked + self.assertIn("foreign_channel", self.parser._extract_flags(message)) + + def test_flag_foreign_channel_boost_same_channel(self): + """Test that boost link to own channel is not flagged.""" + message = self._create_mock_message(text="Boost our channel: https://t.me/boost/test_channel", chat_username="test_channel") + self.parser.get_channel_username = MagicMock(return_value="test_channel") # Ensure current channel is mocked + self.assertNotIn("foreign_channel", self.parser._extract_flags(message)) + + def test_flag_foreign_channel_boost_link_case_insensitive(self): + """Test that boost link to own channel is not flagged (case insensitive).""" + message = self._create_mock_message(text="Boost our channel: https://t.me/boost/TEST_CHANNEL", chat_username="test_channel") + self.parser.get_channel_username = MagicMock(return_value="test_channel") # Ensure current channel is mocked + self.assertNotIn("foreign_channel", self.parser._extract_flags(message)) + + def test_flag_foreign_channel_multiple_links(self): + """Test with multiple links - should flag if any is foreign.""" + message = self._create_mock_message( + text="Links: https://t.me/test_channel and https://t.me/other_channel and https://t.me/boost/test_channel", + chat_username="test_channel" + ) + self.parser.get_channel_username = MagicMock(return_value="test_channel") # Ensure current channel is mocked + self.assertIn("foreign_channel", self.parser._extract_flags(message)) + + def test_flag_foreign_channel_only_boost_word(self): + """Test that the word 'boost' is not flagged.""" + message = self._create_mock_message(text="Check out https://t.me/boost", chat_username="test_channel") + self.parser.get_channel_username = MagicMock(return_value="test_channel") # Ensure current channel is mocked + self.assertNotIn("foreign_channel", self.parser._extract_flags(message)) + + def test_flag_foreign_channel_boost_no_channel(self): + """Test with boost/ but no channel after - should be flagged as foreign.""" + message = self._create_mock_message(text="Strange link: https://t.me/boost/", chat_username="test_channel") + self.parser.get_channel_username = MagicMock(return_value="test_channel") # Ensure current channel is mocked + self.assertNotIn("foreign_channel", self.parser._extract_flags(message)) + def test_flag_multiple_flags(self): message = self._create_mock_message( media=MessageMediaType.VIDEO,