|
import json |
|
import os |
|
import sys |
|
import requests |
|
import threading |
|
name = "John" |
|
d = json.load(open(os.path.join(sys.path[0], "images.json"))) |
|
image_path = os.path.join(sys.path[0], "images") |
|
|
|
|
|
def download_image(img): |
|
# find the largest image |
|
max_width = 0 |
|
max_url = "" |
|
video_file = False |
|
for key, value in img["derivatives"].items(): |
|
if value["width"] > max_width: |
|
max_width = value["width"] |
|
max_url = value["url"] |
|
if ".mp4" in value["url"]: |
|
video_file = True |
|
|
|
# save max image to file |
|
|
|
print(max_url) |
|
# retry until success |
|
while True: |
|
try: |
|
img_data = requests.get(max_url).content |
|
break |
|
except: |
|
print("retrying") |
|
continue |
|
extension = ".JPG" |
|
if video_file: |
|
extension = ".mp4" |
|
with open(os.path.join(image_path + "2", img['contributorFirstName'] + "-" + img['photoGuid']) + extension, 'wb') as handler: |
|
handler.write(img_data) |
|
|
|
|
|
# filter d to only include images that are by name |
|
d["photos"] = [img for img in d["photos"] |
|
if img['contributorFullName'] == name] |
|
|
|
# add a new key to each image in d that will store the path to the image file |
|
for img in d["photos"]: |
|
if 'mediaAssetType' in img and img['mediaAssetType'] == "video": |
|
img["image_path"] = os.path.join( |
|
image_path, img['contributorFirstName'] + "-" + img['photoGuid'] + ".mp4") |
|
else: |
|
img["image_path"] = os.path.join( |
|
image_path, img['contributorFirstName'] + "-" + img['photoGuid'] + ".JPG") |
|
|
|
# check if the image file already exists, and if not, download it |
|
threads = [] |
|
for img in d["photos"]: |
|
if not os.path.exists(img["image_path"]): |
|
print("Downloading image:", img["image_path"]) |
|
thread = threading.Thread(target=download_image, args=(img,)) |
|
thread.start() |
|
|
|
threads.append(thread) |
|
|
|
# wait for all threads to finish |
|
for thread in threads: |
|
thread.join() |
|
print("All images downloaded") |