From 9fd80a272d417d2cef2ee377bba22a8b9b28273a Mon Sep 17 00:00:00 2001 From: Clay Date: Mon, 13 Nov 2023 22:48:01 -0500 Subject: [PATCH] first commit --- README.md | 43 ++++++++++++++++++++++++++++++ start.py | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 README.md create mode 100644 start.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..02512ff --- /dev/null +++ b/README.md @@ -0,0 +1,43 @@ +1. Create a working dir + `mkdir /absolute/path/to/` +3. Create a python virtual environment + `python3 -m venv /absolute/path/to/venv` +5. Install depends in venv + `/absolute/path/to/venv/bin/pip install spotdl` + `/absolute/path/to/venv/bin/pip install python-telegram-bot` + script was written with version 4.14.0 +`/absolute/path/to/venv/bin/pip install python-telegram-bot==4.14.0` +6. Edit start.py with correct telegram API token and change directories + +start the script +`/absolute/path/to/venv/bin/python start.py` + + +## Unit file + +``` +[Unit] +Description=tg_spotdl +After=network-online.target + +[Service] +User=clay +ExecStart=/absolute/path/to/venv/bin/python /absolute/path/to/start.py +Restart=always +RestartSec=5s +RuntimeMaxSec=12h + +[Install] +WantedBy=multi-user.target + +``` + +## start script +``` +#!/bin/bash +/absolute/path/to/venv/bin/python /absolute/path/to/start.py +exit +``` + +## useful links +https://github.com/eternnoir/pyTelegramBotAPI diff --git a/start.py b/start.py new file mode 100644 index 0000000..c64997f --- /dev/null +++ b/start.py @@ -0,0 +1,78 @@ +import telebot +import os +import datetime +import subprocess + +# Replace 'YOUR_API_TOKEN' with your actual Telegram Bot API token +bot = telebot.TeleBot("TOKEN GOES HERE") +dldir = "/WHERE/TO/PUT/THE/MUSIC" +workingdir = "WHERE/IS/THE/PYTHON/VENV" + + +# Get the current day of the month +current_day = datetime.date.today().day + +# Check if the day is divisible by 3 +if current_day % 3 == 0: + # Replace the following command with the command you want to execute + command_to_run = f"{workingdir}bin/pip install --upgrade spotdl" + + # Execute the command + try: + subprocess.run(command_to_run, shell=True, check=True) + print(f"Updated SpotDL!.") + except subprocess.CalledProcessError as e: + print(f"Error updating SpotDL: {e}") +else: + print("Not upgrading SpotDL today.") + + + +# Define conversation states +ARTIST, URL = range(2) +user_data = {} + +print("tg_spotdl bot is running.") + +@bot.message_handler(commands=['start']) + +def start(message): + bot.send_message(message.chat.id, "Hi! I'm a bot that can download music for you.") + bot.send_message(message.chat.id, "Please enter the artist name:") + bot.register_next_step_handler(message, get_artist) + print("User pushed start.") + +def get_artist(message): + user_data['artist'] = message.text + bot.send_message(message.chat.id, "Please enter the Spotify URL for the artist:") + bot.register_next_step_handler(message, get_url) + +def get_url(message): + artist = user_data['artist'] + url = message.text + + bot.send_message(message.chat.id, "This is going to take a while. Please wait!") + + # Create a directory for the artist + artist_directory = f"{dldir}{artist}" + os.makedirs(artist_directory, exist_ok=True) + os.chdir(artist_directory) + + # Download the music using spotdl + subprocess.run([ + f"{workingdir}bin/spotdl", + "--format", "opus", + "--bitrate", "80k", + url + ]) + + bot.send_message(message.chat.id, f"Finished downloading {artist}") + print("Download completed.") + bot.send_message(message.chat.id, "Use the /start command to download another artist.") + +@bot.message_handler(func=lambda message: True) +def echo_all(message): + bot.reply_to(message, "I don't understand. Please use the /start command to begin.") + +if __name__ == "__main__": + bot.polling()