youtube download

This commit is contained in:
2023-05-06 03:16:26 +02:00
parent 71ff713417
commit 0749f742c3
27 changed files with 417 additions and 74 deletions

View File

@ -1 +1,2 @@
from .fileManager import FileManager
from .queueManager import QueueManager

61
service/fileManager.py Normal file
View File

@ -0,0 +1,61 @@
import re
from os import path
from time import time
from discord import Interaction
from entity import Entry
from framework import Downloader, Youtube
class FileManager:
def __init__(
self, youtube: Youtube, downloader: Downloader, downloadDirectory: str
) -> None:
self.youtube = youtube
self.downloader = downloader
self.downloadDirectory = downloadDirectory
self.youtubeVideoRegex = re.compile(
r"(https:\/\/)?(www|music)\.youtube\.com\/(watch\?v=|shorts\/)\w*"
)
self.progressLastUpdate = 0
def getFileName(self, entryId: str) -> str:
return path.join(self.downloadDirectory, entryId)
async def downloadProgress(
self,
interaction: Interaction,
entry: Entry,
current_size: int,
total_size: int,
):
if time() - self.progressLastUpdate > 1:
self.progressLastUpdate = time()
await interaction.edit_original_response(
content=f"Downloading {entry.title.name} [%.2f/%.2f Mo]"
% (current_size / 1024 / 1024, total_size / 1024 / 1024)
)
async def download(self, interaction: Interaction, entries: list[Entry]):
for entry in entries:
entryId = self.youtube.getId(entry.title.url)
fileName = self.getFileName(entryId)
if not path.isfile(fileName) and isinstance(entry.source, str):
if self.youtubeVideoRegex.match(entry.source) is not None:
entry.source = await self.youtube.download(
entry.source,
fileName,
lambda current_size, total_size: self.downloadProgress(
interaction, entry, current_size, total_size
),
)
else:
entry.source = await self.downloader.download(
entry.source,
fileName,
lambda current_size, total_size: self.downloadProgress(
interaction, entry, current_size, total_size
),
)

View File

@ -11,11 +11,11 @@ class QueueManager:
@asynccontextmanager
async def __call__(self, guildId: int) -> Queue:
#await self.acquire(guildId)
# await self.acquire(guildId)
queue = await self.get(guildId)
yield queue
await self.save(guildId)
#await self.release(guildId)
# await self.release(guildId)
async def acquire(self, guildId: int) -> None:
await self.redis.acquire(guildId)