mirror of
https://github.com/nadimkobeissi/mkbsd.git
synced 2024-12-22 18:55:33 +00:00
correct use of asyncio
This commit is contained in:
parent
82e50c64f0
commit
97ffaabcc1
7
mkbsd.py
7
mkbsd.py
|
@ -21,6 +21,8 @@ async def download_image(session, image_url, file_path):
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error downloading image: {str(e)}")
|
print(f"Error downloading image: {str(e)}")
|
||||||
|
|
||||||
|
print(f"🖼️ Saved image to {file_path}")
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
try:
|
try:
|
||||||
async with aiohttp.ClientSession() as session:
|
async with aiohttp.ClientSession() as session:
|
||||||
|
@ -39,6 +41,7 @@ async def main():
|
||||||
print(f"📁 Created directory: {download_dir}")
|
print(f"📁 Created directory: {download_dir}")
|
||||||
|
|
||||||
file_index = 1
|
file_index = 1
|
||||||
|
async with asyncio.TaskGroup() as group:
|
||||||
for key, subproperty in data.items():
|
for key, subproperty in data.items():
|
||||||
if subproperty and subproperty.get('dhd'):
|
if subproperty and subproperty.get('dhd'):
|
||||||
image_url = subproperty['dhd']
|
image_url = subproperty['dhd']
|
||||||
|
@ -48,11 +51,9 @@ async def main():
|
||||||
filename = f"{file_index}{ext}"
|
filename = f"{file_index}{ext}"
|
||||||
file_path = os.path.join(download_dir, filename)
|
file_path = os.path.join(download_dir, filename)
|
||||||
|
|
||||||
await download_image(session, image_url, file_path)
|
group.create_task(download_image(session, image_url, file_path))
|
||||||
print(f"🖼️ Saved image to {file_path}")
|
|
||||||
|
|
||||||
file_index += 1
|
file_index += 1
|
||||||
await delay(250)
|
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error: {str(e)}")
|
print(f"Error: {str(e)}")
|
||||||
|
|
Loading…
Reference in a new issue