Skip to content

Commit

Permalink
add muliple links in one message support (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
nett00n authored Dec 31, 2023
1 parent d11d1d4 commit cc66f60
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 34 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Prepare for a magical journey as you set up and deploy the URLFairyBot.

### Prerequisites

- Docker and Docker Compose installed on your system (or your own fairy dust, whichever is handier).
- Docker and Docker Compose are installed on your system (or your fairy dust, whichever is handier).

### Installation

Expand All @@ -32,7 +32,7 @@ Prepare for a magical journey as you set up and deploy the URLFairyBot.
BASE_URL=your_base_url
```

3. Do not forget to create a traefik reverse proxy `docker-compose.yml` file:
3. Do not forget to create a Traefik reverse proxy `docker-compose.yml` file:

```yaml
---
Expand Down
10 changes: 0 additions & 10 deletions TODO.md

This file was deleted.

58 changes: 36 additions & 22 deletions bot/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,30 +67,44 @@ def is_within_size_limit(video_path):
async def process_message(message: types.Message):
message_text = message.text.strip()

if message_text.startswith(("http", "www")):
clean_url = follow_redirects(message_text)
video_path = os.path.join(CACHE_DIR, sanitize_filename(clean_url) + ".mp4")
file_link = f"https://{BASE_URL}/{sanitize_filename(clean_url)}.mp4"

if "tiktok" in clean_url:
clean_url = clean_tiktok_url(clean_url)
if os.path.exists(video_path):
try:
if is_within_size_limit(video_path):
with open(video_path, "rb") as video_file:
await message.reply_video(video_file, caption=clean_url)
else:
await send_large_video(message, clean_url, file_link)
except Exception as e:
error_message = f"Error sending video from URL: {clean_url}. Error details: {str(e)}"
traceback.print_exc()
await message.reply(error_message)
# Split the message by spaces or any other delimiter you prefer
urls = message_text.split()

error_messages = [] # List to store invalid URL format errors

for url in urls:
if url.startswith(("http", "www")):
clean_url = follow_redirects(url)
video_path = os.path.join(CACHE_DIR, sanitize_filename(clean_url) + ".mp4")
file_link = f"https://{BASE_URL}/{sanitize_filename(clean_url)}.mp4"

if "tiktok" in clean_url:
clean_url = clean_tiktok_url(clean_url)
if os.path.exists(video_path):
try:
if is_within_size_limit(video_path):
with open(video_path, "rb") as video_file:
await message.reply_video(video_file, caption=clean_url)
else:
await send_large_video(message, clean_url, file_link)
except Exception as e:
error_message = f"Error sending video from URL: {clean_url}. Error details: {str(e)}"
traceback.print_exc()
await message.reply(error_message)
else:
await yt_dlp_download_and_send(clean_url, message)
else:
await yt_dlp_download_and_send(clean_url, message)
error_messages.append(
f"Invalid URL format: {url}"
) # Accumulate the error messages
else:
await message.reply(clean_url)
else:
await message.reply(message_text)
error_messages.append(
f"Invalid URL format: {url}"
) # Accumulate the error messages

# Check if there are any accumulated error messages and send them as one reply
if error_messages:
await message.reply("\n".join(error_messages))


# Function to download a video using yt_dlp and send it
Expand Down

0 comments on commit cc66f60

Please sign in to comment.