MangaBot/util/parallel_downloads.py

29 lines
971 B
Python
Raw Permalink Normal View History

2024-07-23 14:19:45 +01:00
import concurrent.futures
import requests
import os.path
2024-07-24 23:07:10 +01:00
import asyncio
2024-07-23 14:19:45 +01:00
import discord
2024-07-23 17:31:23 +01:00
from manga_api import Manga
2024-07-23 14:19:45 +01:00
DOWNLOAD_DIRECTORY = 'tmp/'
2024-07-23 14:19:45 +01:00
def discord_file_from_filename(filename: str) -> discord.File:
return discord.File(f"{DOWNLOAD_DIRECTORY}{filename}",filename)
def download_file(manga: Manga) -> str:
2024-07-24 23:07:10 +01:00
extension = asyncio.run(manga.get_cover_art_extension())
if not os.path.isfile(f'{DOWNLOAD_DIRECTORY}/{manga.id}.{extension}'):
2024-07-24 23:07:10 +01:00
img_data = requests.get(asyncio.run(manga.get_cover_art_url())).content
with open(f'{DOWNLOAD_DIRECTORY}/{manga.id}.{extension}', 'wb') as handler:
2024-07-23 14:19:45 +01:00
handler.write(img_data)
return f"{manga.id}.{extension}"
2024-07-23 14:19:45 +01:00
def parallel_download(manga_list: list[Manga]) -> list[str]:
print("Downloading Images...")
2024-07-23 14:19:45 +01:00
with concurrent.futures.ThreadPoolExecutor() as exector:
result = exector.map(download_file, manga_list)
print("Images Finished Downloading")
2024-07-23 14:19:45 +01:00
return list(result)