From 2286d7a8d56cd064615411a989ee9fd1f506b0a4 Mon Sep 17 00:00:00 2001 From: "Radu C. Martin" Date: Tue, 15 Apr 2025 14:11:25 +0200 Subject: [PATCH] feat: download songs asyncronously --- download_service.py | 8 ++++++-- main.py | 2 +- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/download_service.py b/download_service.py index 0636905..9f1793d 100644 --- a/download_service.py +++ b/download_service.py @@ -1,3 +1,4 @@ +import asyncio import sys import yt_dlp @@ -22,8 +23,11 @@ class DownloadService: def __init__(self) -> None: self.ydl = yt_dlp.YoutubeDL(ydl_opts) - def download(self, url: str) -> Track: - info = self.ydl.extract_info(url, download=True) + async def download(self, url: str) -> Track: + def extract(): + return self.ydl.extract_info(url, download=True) + + info = await asyncio.to_thread(extract) try: filepath = info["requested_downloads"][-1]["filepath"] # type: ignore diff --git a/main.py b/main.py index 763554b..bcd6acf 100644 --- a/main.py +++ b/main.py @@ -128,7 +128,7 @@ def get_queue(): @app.post("/queue", tags=["queue"]) async def post_to_queue(url: str): - track = dl_service.download(url) + track = await dl_service.download(url) await player.add_to_queue(track)