correct use of asyncio

This commit is contained in:
Om Lanke 2024-09-28 01:10:36 +05:30 committed by GitHub
parent 82e50c64f0
commit 97ffaabcc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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:
@ -29,7 +31,7 @@ async def main():
raise Exception(f"⛔ Failed to fetch JSON file: {response.status}") raise Exception(f"⛔ Failed to fetch JSON file: {response.status}")
json_data = await response.json() json_data = await response.json()
data = json_data.get('data') data = json_data.get('data')
if not data: if not data:
raise Exception('⛔ JSON does not have a "data" property at its root.') raise Exception('⛔ JSON does not have a "data" property at its root.')
@ -39,20 +41,19 @@ async def main():
print(f"📁 Created directory: {download_dir}") print(f"📁 Created directory: {download_dir}")
file_index = 1 file_index = 1
for key, subproperty in data.items(): async with asyncio.TaskGroup() as group:
if subproperty and subproperty.get('dhd'): for key, subproperty in data.items():
image_url = subproperty['dhd'] if subproperty and subproperty.get('dhd'):
print(f"🔍 Found image URL!") image_url = subproperty['dhd']
parsed_url = urlparse(image_url) print(f"🔍 Found image URL!")
ext = os.path.splitext(parsed_url.path)[-1] or '.jpg' parsed_url = urlparse(image_url)
filename = f"{file_index}{ext}" ext = os.path.splitext(parsed_url.path)[-1] or '.jpg'
file_path = os.path.join(download_dir, filename) filename = f"{file_index}{ext}"
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)}")