Skip to content

Instantly share code, notes, and snippets.

@barnaclejive
Created February 9, 2016 03:35
Show Gist options
  • Save barnaclejive/bac711c85e5e9a94a8b1 to your computer and use it in GitHub Desktop.
Save barnaclejive/bac711c85e5e9a94a8b1 to your computer and use it in GitHub Desktop.
import os
import sys
import getopt
import urllib
import json
from pprint import pprint
if __name__=='__main__':
try: opts, args = getopt.getopt(sys.argv[1:], 's:d:', [])
except getopt.GetoptError, e: usage()
# setting defaults
source = ""
dest = ""
# print arguments
print "opts\t" + str(opts)
print "args\t" + str(args)
# set params to settings
for optKey, optValue in opts:
if optKey == '-s': source = optValue
if optKey == '-d': dest = optValue
# print settings
print "source\t" + source
print "dest\t" + dest
# load file json
with open(source) as source_file:
data = json.load(source_file)
results = data['results']
c = 1
total = len(results)
for obj in results:
# progress
print str(c) + " / " + str(total)
c += 1
# has image
objKeys = obj.keys()
imageKey = 'image' if ('image' in objKeys) else ('image_main' if ('image_main' in objKeys) else None)
if imageKey:
name = obj[imageKey]['name']
url = obj[imageKey]['url']
# already downloaded
downloadPath = dest + "/" + name
if not os.path.exists(downloadPath):
print downloadPath
urllib.urlretrieve(url, downloadPath)
else:
print "ALREADY DOWNLOADED"
# no image
else:
print "NO IMAGE"
@paulfreeman
Copy link

Correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment