download.py 709 B

12345678910111213141516171819202122232425262728
  1. import asyncio
  2. from utils import os
  3. from .info import get_playlist
  4. async def download_video(url, folder: str=None, filename=None):
  5. if folder is None:
  6. folder = ""
  7. else:
  8. if not folder.endswith("/") and not folder.endswith("\\"):
  9. folder += "/"
  10. if filename is None:
  11. command = f'youtube-dl -o "{folder}%(title)s.%(ext)s" -x --audio-format mp3 {url}'
  12. else:
  13. command = f'youtube-dl -o "{folder}{filename}" -x --audio-format mp3 {url}'
  14. await os.system(command)
  15. async def download_playlist(url, path) -> list:
  16. tasks = []
  17. playlist = await get_playlist(url, path)
  18. for video in playlist["videos"]:
  19. task = asyncio.create_task(download_video(video["id"], path))
  20. tasks.append(task)
  21. return tasks