diff --git a/musicdownloader/musicdownloader.py b/musicdownloader/musicdownloader.py index 1a2b440..ed218b2 100644 --- a/musicdownloader/musicdownloader.py +++ b/musicdownloader/musicdownloader.py @@ -3,7 +3,6 @@ import re import discord import os import sqlite3 -import concurrent.futures from yt_dlp import YoutubeDL, utils from redbot.core import commands, checks, Config, data_manager @@ -67,46 +66,6 @@ class MusicDownloader(commands.Cog): elif os.path.exists(data_path) is False: await ctx.send("The path you've provided doesn't exist!") - def youtube_download(self, url: str, path: str): - """This method does the actual downloading of the YouTube Video.""" - class Logger: - def debug(self, msg): - if msg.startswith('[debug] '): - pass - else: - self.info(msg) - def info(self, msg): - pass - def warning(self, msg): - pass - def error(self, msg): - print(msg) - ydl_opts = { - 'logger': Logger(), - 'format': 'm4a/bestaudio/best', - 'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'm4a',}], - 'paths': {'home': path}, - 'verbose': True - } - with YoutubeDL(ydl_opts) as ydl: - info = ydl.extract_info(url=url, download=False) - title = info['title'] - id = info['id'] - filename = title + f' [{id}].m4a' - full_filename = os.path.join(path, filename) - if os.path.isfile(full_filename): - previously_existed = True - else: - with YoutubeDL(ydl_opts) as ydl: - error_code = ydl.download(url) - previously_existed = False - return filename, previously_existed - - async def download_file(self, url: str, path: str): - with concurrent.futures.ThreadPoolExecutor() as executor: - result = await self.bot.loop.run_in_executor(executor, self.youtube_download, url, path) - return result - @commands.command(aliases=["dl"]) async def download(self, ctx: commands.Context, url: str, delete: bool = False, subfolder: str = None): """This command downloads a YouTube Video as an `m4a` and uploads the file to discord. @@ -125,6 +84,40 @@ class MusicDownloader(commands.Cog): except self.UserBlacklisted as e: await ctx.send(f"You are blacklisted from running this command!\nReason: `{e}`") return + def youtube_download(self, url: str, path: str, message: discord.Message): + """This function does the actual downloading of the YouTube Video.""" + class Logger: + def debug(self, msg): + if msg.startswith('[debug] '): + pass + else: + self.info(msg) + def info(self, msg): + pass + def warning(self, msg): + pass + def error(self, msg): + print(msg) + ydl_opts = { + 'logger': Logger(), + 'format': 'm4a/bestaudio/best', + 'postprocessors': [{'key': 'FFmpegExtractAudio', 'preferredcodec': 'm4a',}], + 'paths': {'home': path}, + 'verbose': True + } + with YoutubeDL(ydl_opts) as ydl: + info = ydl.extract_info(url=url, download=False) + title = info['title'] + id = info['id'] + filename = title + f' [{id}].m4a' + full_filename = os.path.join(data_path, filename) + if os.path.isfile(full_filename): + previously_existed = True + else: + with YoutubeDL(ydl_opts) as ydl: + error_code = ydl.download(url) + previously_existed = False + return filename, previously_existed data_path = await self.config.save_directory() if subfolder and await self.bot.is_owner(ctx.author): data_path = os.path.join(data_path, subfolder) @@ -156,7 +149,7 @@ class MusicDownloader(commands.Cog): msg = ctx.send message = await msg("YouTube Downloader started!") try: - ytdlp_output = await self.download_file(url, data_path) + ytdlp_output = youtube_download(self, url, data_path, message) except utils.DownloadError or utils.ExtractorError: await message.edit(content="Please provide a link to YouTube and not another site.\nThe site you've linked to is known for using DRM protection, so MusicDownloader cannot download from it.") return