Forked from simpleapples/fetch_google_earth_wallpapers.py
Created
July 27, 2019 01:32
-
-
Save beyoung/4910050eb5d7e233f2df499e8bd6251b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import aiohttp | |
import json | |
import base64 | |
import asyncio | |
import uvloop | |
START_ID = 1000 | |
END_ID = 1020 | |
async def fetch_all_images(): | |
async with aiohttp.ClientSession(connector=aiohttp.TCPConnector(verify_ssl=False)) as session: | |
sem = asyncio.Semaphore(10) | |
ids = [id for id in range(START_ID, END_ID)] | |
for current_id in ids: | |
async with sem: | |
await fetch_image_by_id(session, current_id) | |
async def fetch_image_by_id(session, item_id): | |
url = f'https://www.gstatic.com/prettyearth/assets/data/v2/{item_id}.json' | |
async with session.get(url) as response: | |
try: | |
json_obj = json.loads(await response.text()) | |
except json.decoder.JSONDecodeError as e: | |
print(f'Download failed - {item_id}.jpg') | |
return | |
image_str = json_obj['dataUri'].replace('data:image/jpeg;base64,', '') | |
image_data = base64.b64decode(image_str) | |
save_folder = dir_path = os.path.dirname( | |
os.path.realpath(__file__)) + '/google_earth_1/' | |
with open(f'{save_folder}{item_id}.jpg', 'wb') as f: | |
f.write(image_data) | |
print(f'Download complete - {item_id}.jpg') | |
def main(): | |
import time | |
start = time.time() | |
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy()) | |
event_loop = asyncio.get_event_loop() | |
future = asyncio.ensure_future(fetch_all_images()) | |
results = event_loop.run_until_complete(future) | |
end = time.time() | |
print(end - start) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment