Skip to content

Commit

Permalink
2024-10-28 10:16:52+04:00
Browse files Browse the repository at this point in the history
  • Loading branch information
nett00n committed Oct 28, 2024
1 parent 3b8cab6 commit a90448b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
7 changes: 4 additions & 3 deletions app/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 15 additions & 2 deletions app/url_processing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -23,17 +26,25 @@ 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:
if re.match(pattern, url):
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",
Expand All @@ -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)
Expand All @@ -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

Expand Down

0 comments on commit a90448b

Please sign in to comment.