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
|
|
|
|
2024-07-24 20:28:19 +01:00
|
|
|
DOWNLOAD_DIRECTORY = 'tmp/'
|
2024-07-23 14:19:45 +01:00
|
|
|
|
2024-07-24 20:28:19 +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())
|
2024-07-24 20:28:19 +01:00
|
|
|
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
|
2024-07-24 20:28:19 +01:00
|
|
|
with open(f'{DOWNLOAD_DIRECTORY}/{manga.id}.{extension}', 'wb') as handler:
|
2024-07-23 14:19:45 +01:00
|
|
|
handler.write(img_data)
|
2024-07-24 20:28:19 +01:00
|
|
|
return f"{manga.id}.{extension}"
|
2024-07-23 14:19:45 +01:00
|
|
|
|
2024-07-24 20:28:19 +01:00
|
|
|
def parallel_download(manga_list: list[Manga]) -> list[str]:
|
2024-07-23 20:20:06 +01:00
|
|
|
print("Downloading Images...")
|
2024-07-23 14:19:45 +01:00
|
|
|
with concurrent.futures.ThreadPoolExecutor() as exector:
|
|
|
|
|
result = exector.map(download_file, manga_list)
|
2024-07-23 20:20:06 +01:00
|
|
|
print("Images Finished Downloading")
|
2024-07-23 14:19:45 +01:00
|
|
|
return list(result)
|
|
|
|
|
|