Telegram-Spotdl-Bot/start.py

79 lines
2.3 KiB
Python
Raw Normal View History

2023-11-13 22:48:01 -05:00
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"
2023-11-13 23:08:46 -05:00
workingdir = "/WHERE/IS/THE/PYTHON/VENV"
2023-11-13 22:48:01 -05:00
# 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()