From a90448b0a1211187d7bf4daa9b0610429ed56f54 Mon Sep 17 00:00:00 2001 From: Vladimir nett00n Budylnikov Date: Mon, 28 Oct 2024 10:16:52 +0400 Subject: [PATCH] 2024-10-28 10:16:52+04:00 --- app/bot.py | 7 ++++--- app/url_processing.py | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/app/bot.py b/app/bot.py index 71b8b0b..8d1dad1 100644 --- a/app/bot.py +++ b/app/bot.py @@ -27,13 +27,14 @@ async def handle_message(message: types.Message): await message.reply( "Please do not be mad at me 🥺. I am not very clever bot 👉👈" + "\n\n" - + "I am very sorry if I did not help you" + + "I am very sorry if I did not help you 😢" + "\n\n" + "Sometimes I use external tools to help you, but they can " + "be offline or could not parse media too. " - + "Especially if we are talking about Facebook 🤬" + + "Especially if we are talking about 🤬🤬🤬🤬ing Facebook " + "\n\n" - + "Please donate to [Centre T](https://translyaciya.com/help_eng)", + + "Please donate to " + + "[Centre T](https://translyaciya.com/help_eng) 🫶", parse_mode=types.ParseMode.MARKDOWN, ) return diff --git a/app/url_processing.py b/app/url_processing.py index cf491fd..a2940d7 100644 --- a/app/url_processing.py +++ b/app/url_processing.py @@ -4,13 +4,16 @@ import os import re from urllib.parse import urlparse, urlunparse + import requests from app.config import settings + from .download import UnsupportedUrlError, yt_dlp_download logger = logging.getLogger(__name__) + def follow_redirects(url: str, timeout=settings.FOLLOW_REDIRECT_TIMEOUT) -> str: try: response = requests.head(url, allow_redirects=True, timeout=timeout) @@ -23,10 +26,17 @@ def follow_redirects(url: str, timeout=settings.FOLLOW_REDIRECT_TIMEOUT) -> str: logger.warning(f"Timeout for URL: {url} after {timeout} seconds") return url + def transform_youtube_url(url: str) -> str: youtube_patterns = [ - (r"^https://music\.youtube\.com/watch\?v=([a-zA-Z0-9_-]+)", r"https://music.yfxtube.com/watch?v=\1"), - (r"^https://www\.youtube\.com/watch\?v=([a-zA-Z0-9_-]+)", r"https://www.yfxtube.com/watch?v=\1"), + ( + r"^https://music\.youtube\.com/watch\?v=([a-zA-Z0-9_-]+)", + r"https://music.yfxtube.com/watch?v=\1", + ), + ( + r"^https://www\.youtube\.com/watch\?v=([a-zA-Z0-9_-]+)", + r"https://www.yfxtube.com/watch?v=\1", + ), (r"^https://youtu\.be/([a-zA-Z0-9_-]+)", r"https://fxyoutu.be/\1"), ] for pattern, replacement in youtube_patterns: @@ -34,6 +44,7 @@ def transform_youtube_url(url: str) -> str: return re.sub(pattern, replacement, url) return None + def apply_rewrite_map(final_url: str) -> str: rewrite_map = { r"^https://(open\.)?spotify.com": "https://fxspotify.com", @@ -49,6 +60,7 @@ def apply_rewrite_map(final_url: str) -> str: return re.sub(pattern, replacement, final_url, count=1) return final_url + async def attempt_download(final_url: str) -> str: try: video_os_path = await yt_dlp_download(final_url) @@ -62,6 +74,7 @@ async def attempt_download(final_url: str) -> str: raise UnsupportedUrlError("Download failed unexpectedly.") return None + async def process_url_request(url: str, is_group_chat: bool = False) -> str: url = str(url) # Ensure url is a string