feat: hopefully managed to make musicdownloader not blocking

This commit is contained in:
Seaswimmer 2023-08-05 18:04:22 -04:00
parent b2cb6c65f4
commit c2a92d5fba
No known key found for this signature in database
GPG key ID: 5019678FD9CF50D8

View file

@ -3,6 +3,7 @@ import re
import discord import discord
import os import os
import sqlite3 import sqlite3
import concurrent.futures
from yt_dlp import YoutubeDL, utils from yt_dlp import YoutubeDL, utils
from redbot.core import commands, checks, Config, data_manager from redbot.core import commands, checks, Config, data_manager
@ -84,7 +85,11 @@ class MusicDownloader(commands.Cog):
except self.UserBlacklisted as e: except self.UserBlacklisted as e:
await ctx.send(f"You are blacklisted from running this command!\nReason: `{e}`") await ctx.send(f"You are blacklisted from running this command!\nReason: `{e}`")
return return
def youtube_download(self, url: str, path: str, message: discord.Message): async def download_file(self, url: str, path: str):
with concurrent.futures.ThreadPoolExecutor() as executor:
result = await self.bot.loop.run_in_executor(executor, youtube_download, url, path)
return result
def youtube_download(self, url: str, path: str):
"""This function does the actual downloading of the YouTube Video.""" """This function does the actual downloading of the YouTube Video."""
class Logger: class Logger:
def debug(self, msg): def debug(self, msg):
@ -149,7 +154,7 @@ class MusicDownloader(commands.Cog):
msg = ctx.send msg = ctx.send
message = await msg("YouTube Downloader started!") message = await msg("YouTube Downloader started!")
try: try:
ytdlp_output = youtube_download(self, url, data_path, message) ytdlp_output = download_file(self, url, data_path)
except utils.DownloadError or utils.ExtractorError: 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.") 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 return