diff --git a/services/youtube_downloaders/.gitignore b/services/youtube_downloaders/.gitignore new file mode 100644 index 0000000..d4e4f0b --- /dev/null +++ b/services/youtube_downloaders/.gitignore @@ -0,0 +1 @@ +.telegram.env \ No newline at end of file diff --git a/services/youtube_downloaders/docker-compose.yml b/services/youtube_downloaders/docker-compose.yml index 812a50e..2a66bcb 100644 --- a/services/youtube_downloaders/docker-compose.yml +++ b/services/youtube_downloaders/docker-compose.yml @@ -1,20 +1,17 @@ # Docker Compose for service youtube_downloaders created at Wed Apr 23 10:18:19 PM EEST 2025 services: - metatube: - container_name: yt_download_stack_metatube - restart: always - image: jvt038/metatube:latest - ports: - - '${SVC_PORT_1}:5000' - environment: - - UID=1001 - volumes: - - '${METATUBE_DOWNLOADS}:/downloads:rw' - - '${METATUBE_DB}:/database:rw' - - '${METATUBE_MIGRATIONS}:/config/migrations:rw' + metube-tg-gui: + build: + context: telegram-bot-fetcher + container_name: metube_telegram_frontend + restart: unless-stopped + env_file: .telegram.env + depends_on: + - metube + metube: image: ghcr.io/alexta69/metube - container_name: yt_download_stack_metube + container_name: metube restart: unless-stopped environment: - UID=1001 diff --git a/services/youtube_downloaders/telegram-bot-fetcher/Dockerfile b/services/youtube_downloaders/telegram-bot-fetcher/Dockerfile new file mode 100644 index 0000000..f4dc4bb --- /dev/null +++ b/services/youtube_downloaders/telegram-bot-fetcher/Dockerfile @@ -0,0 +1,21 @@ +# Use the official Python image from the Docker Hub +FROM python:3.9-slim + +# Set the working directory in the container +WORKDIR /app + +# Copy the requirements file into the container +COPY requirements.txt . + +# Install the dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Copy the rest of the application code into the container +COPY . . + +# Set environment variables (optional, can also be set in docker-compose.yml) +# ENV TELEGRAM_API_TOKEN=your_telegram_api_token +# ENV METUBE_URL=http://metube.local.com/add + +# Command to run the bot +CMD ["python", "bot.py"] diff --git a/services/youtube_downloaders/telegram-bot-fetcher/bot.py b/services/youtube_downloaders/telegram-bot-fetcher/bot.py new file mode 100644 index 0000000..65aa3ac --- /dev/null +++ b/services/youtube_downloaders/telegram-bot-fetcher/bot.py @@ -0,0 +1,91 @@ +import os +import json +import requests +import logging +from telegram import Update +from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, CallbackContext + +# Set up logging +logging.basicConfig( + format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', + level=logging.DEBUG +) + +logger = logging.getLogger(__name__) + +# Function to start the bot +def start(update: Update, context: CallbackContext) -> None: + logger.debug("Received /start command.") + update.message.reply_text('Hello! I will download YouTube links in mp3.') + +# Function to handle messages +def handle_message(update: Update, context: CallbackContext) -> None: + message_text = update.message.text + logger.debug(f"Received message: {message_text}") + + # Check if the message contains a YouTube link + if "youtube.com" in message_text or "youtu.be" in message_text: + url = message_text.strip() # Get the URL from the message + logger.debug(f"Detected YouTube link: {url}") + + # Prepare the JSON payload + payload = { + "url": url, + "quality": "best", + "format": "mp3", + "playlist_strict_mode": False, + "auto_start": True + } + + # Read the MeTube URL from environment variable + metube_url = os.getenv('METUBE_URL') + logger.debug(f"Using MeTube URL: {metube_url}") + + # Send a POST request + try: + response = requests.post(f'{metube_url}/add', json=payload) + logger.debug(f"POST request sent. Response status code: {response.status_code}") + + # Check the response status + if response.status_code == 200: + update.message.reply_text(f"Download started for {url}") + logger.info(f"Download started for {url}") + else: + update.message.reply_text("Failed to start download.") + logger.error(f"Failed to start download for {url}. Response: {response.text}") + except Exception as e: + logger.exception("An error occurred while sending the POST request.") + update.message.reply_text("An error occurred while trying to start the download.") + else: + # Echo the message if it's not a YouTube link + update.message.reply_text('No YouTube link provided. Please use youtube.com or youtu.be') + logger.warning("No valid YouTube link provided.") + +def main() -> None: + # Read the Telegram API token from environment variable + api_token = os.getenv('TELEGRAM_API_TOKEN') + + # Check if the API token is set + if not api_token: + logger.error("TELEGRAM_API_TOKEN environment variable not set.") + return + + updater = Updater(api_token) + + # Get the dispatcher to register handlers + dispatcher = updater.dispatcher + + # Register command and message handlers + dispatcher.add_handler(CommandHandler("start", start)) + dispatcher.add_handler(MessageHandler(Filters.text & ~Filters.command, handle_message)) + + # Start the Bot + updater.start_polling() + logger.info("Bot started and polling for messages.") + + # Run the bot until you send a signal to stop + updater.idle() + logger.info("Bot stopped.") + +if __name__ == '__main__': + main() diff --git a/services/youtube_downloaders/telegram-bot-fetcher/requirements.txt b/services/youtube_downloaders/telegram-bot-fetcher/requirements.txt new file mode 100644 index 0000000..c9d1268 --- /dev/null +++ b/services/youtube_downloaders/telegram-bot-fetcher/requirements.txt @@ -0,0 +1,2 @@ +python-telegram-bot==13.13 # or the latest version you are using +requests==2.28.1 # or the latest version you are using \ No newline at end of file