add youtube title

This commit is contained in:
vvzvlad
2025-04-18 22:58:25 +03:00
parent d0584d4525
commit d2870bc0f6
2 changed files with 27 additions and 6 deletions
+5 -1
View File
@@ -258,7 +258,11 @@ class PostParser:
# Handle specific cases for non-meaningful original text (if text block was entered but didn't yield a usable title)
if text_was_processed and not use_text_title:
if re.search(r'(?:youtube\.com|youtu\.be)', text_stripped.lower()): return "🎥 YouTube Link"
if re.search(r'(?:youtube\.com|youtu\.be)', text_stripped.lower()):
if message.web_page and message.web_page.title:
return f"🎥 YouTube: {message.web_page.title}"
else:
return "🎥 YouTube Link"
# Check if original text was just a URL and there's a webpage title
if message.web_page and message.web_page.title:
url_match = re.match(r'^\s*(https?://[^\s<>"\']+)\s*$', text_stripped)
+22 -5
View File
@@ -1,5 +1,10 @@
# pylint: disable=protected-access, wrong-import-position
# flake8: noqa
# pylint: disable=broad-exception-raised, raise-missing-from, too-many-arguments, redefined-outer-name
# pylint: disable=multiple-statements, logging-fstring-interpolation, trailing-whitespace, line-too-long
# pylint: disable=broad-exception-caught, missing-function-docstring, missing-class-docstring
# pylint: disable=f-string-without-interpolation, protected-access
# pylance: disable=reportMissingImports, reportMissingModuleSource
import unittest
from unittest.mock import MagicMock, PropertyMock
import sys
@@ -156,12 +161,24 @@ class TestPostParserGenerateTitle(unittest.TestCase):
self.assertEqual(self.parser._generate_title(message), "🔗 Web link")
def test_generate_title_text_only_youtube_url(self):
message = self._create_mock_message(text="https://www.youtube.com/watch?v=dQw4w9WgXcQ")
self.assertEqual(self.parser._generate_title(message), "🎥 YouTube Link")
# Create mock web_page with title
web_page_mock = MagicMock()
web_page_mock.title = "Rick Astley - Never Gonna Give You Up"
message = self._create_mock_message(
text="https://www.youtube.com/watch?v=dQw4w9WgXcQ",
web_page=web_page_mock
)
self.assertEqual(self.parser._generate_title(message), "🎥 YouTube: Rick Astley - Never Gonna Give You Up")
def test_generate_title_text_only_youtu_be_url(self):
message = self._create_mock_message(text="https://youtu.be/dQw4w9WgXcQ")
self.assertEqual(self.parser._generate_title(message), "🎥 YouTube Link")
# Create mock web_page with title
web_page_mock = MagicMock()
web_page_mock.title = "Rick Astley - Never Gonna Give You Up"
message = self._create_mock_message(
text="https://youtu.be/dQw4w9WgXcQ",
web_page=web_page_mock
)
self.assertEqual(self.parser._generate_title(message), "🎥 YouTube: Rick Astley - Never Gonna Give You Up")
def test_generate_title_caption_with_url_and_text(self):
message = self._create_mock_message(media=MessageMediaType.PHOTO, caption="Look at this photo! https://example.com/image.jpg")