Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dest_folder = "images/source-512/"
- async def download_file(session, icon_url_512, file_path, icon):
- retry_count = 0
- max_retries = 5
- wait_seconds = 1
- while retry_count < max_retries:
- try:
- async with session.get(icon_url_512, timeout=1) as response:
- if response.status == 200:
- with open(file_path, "wb") as f:
- f.write(await response.read())
- db_icons.update_one({"_id": icon["_id"]}, {"$set": {"downloaded": True}})
- return
- else:
- print(f"Response status {response.status}. Retrying...")
- except (aiohttp.ClientError, aiohttp.ServerTimeoutError) as e:
- print(f"Caught an error: {e}. Retrying...")
- except Exception as e:
- print(f"An unexpected error occurred: {e}. Stopping retries.")
- break
- retry_count += 1
- await asyncio.sleep(wait_seconds)
- wait_seconds *= 2
- print("Max retries reached. Could not download the file.")
- async def download_icon(icon, session):
- file_name = icon["file_name"]
- file_path = os.path.join(dest_folder, file_name)
- icon_url = icon["url"]
- icon_url_512 = icon_url.replace("/128/", "/512/")
- await download_file(session, icon_url_512, file_path, icon)
- async def download_all_icons():
- skip = 0
- async with aiohttp.ClientSession() as session:
- while True:
- tasks = []
- for _ in range(100):
- icon = db_icons.find_one({"downloaded": False}, skip=skip)
- skip += 1
- if icon:
- task = asyncio.create_task(download_icon(icon, session))
- tasks.append(task)
- else:
- break
- await asyncio.gather(*tasks)
- await download_all_icons()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement