Files
JapariArchive/downloadHelper.py
2026-01-08 22:34:54 +01:00

29 lines
790 B
Python

import os
import shutil
import aiohttp
from exceptions import DOWNLOAD_FAIL
async def downloadFileToMemory(url, session : aiohttp.ClientSession):
print("Downloading", url)
try:
async with session.get(url) as resp:
result = await resp.read()
if len(result) == 0:
raise DOWNLOAD_FAIL("received 0 bytes")
else:
return result
except Exception as e:
print(url, e)
raise DOWNLOAD_FAIL(e)
async def save_to_file(url, out_file, session):
file_bytes = await downloadFileToMemory(url, session)
if os.path.exists(out_file):
shutil.copy(out_file, out_file + "_bck")
with open(out_file, "wb") as binary_file:
binary_file.write(file_bytes)
return file_bytes