Last active
December 25, 2015 07:48
-
-
Save gregkepler/6941486 to your computer and use it in GitHub Desktop.
Saves all photos and videos in a json file of instagram photos.
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
// json file should be formatted something like | |
{ | |
"data": { | |
"photos": [ | |
{ | |
... | |
"images": { | |
"low_resolution": { | |
"url": "http://distilleryimage3.s3.amazonaws.com/###_6.jpg", | |
"width": 306, | |
"height": 306 | |
}, | |
"thumbnail": { | |
"url": "http://distilleryimage3.s3.amazonaws.com/###_5.jpg", | |
"width": 150, | |
"height": 150 | |
}, | |
"standard_resolution": { | |
"url": "http://distilleryimage3.s3.amazonaws.com/###_7.jpg", | |
"width": 612, | |
"height": 612 | |
}, | |
... | |
}, | |
{ | |
... | |
"videos": { | |
"low_resolution": { | |
"url": "http://distilleryimage7.s3.amazonaws.com/###_102.mp4", | |
"width": 480, | |
"height": 480 | |
}, | |
"standard_resolution": { | |
"url": "http://distilleryimage7.s3.amazonaws.com/###_101.mp4", | |
"width": 640, | |
"height": 640 | |
} | |
}, | |
"type": "video", | |
"images": { | |
"low_resolution": { | |
"url": "http://distilleryimage7.s3.amazonaws.com/###_6.jpg", | |
"width": 306, | |
"height": 306 | |
}, | |
"thumbnail": { | |
"url": "http://distilleryimage7.s3.amazonaws.com/###_5.jpg", | |
"width": 150, | |
"height": 150 | |
}, | |
"standard_resolution": { | |
"url": "http://distilleryimage7.s3.amazonaws.com/###_7.jpg", | |
"width": 612, | |
"height": 612 | |
} | |
} | |
... | |
} | |
} | |
] | |
} | |
} |
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 urllib | |
import json | |
import os | |
def saveImage(path, dir): | |
directory = dir + "/" + os.path.basename(path) | |
# check if path exists yet. If not, then create it | |
if not os.path.exists(dir): | |
os.makedirs(dir) | |
f = open(directory,'wb') | |
f.write(urllib.urlopen(path).read()) | |
f.close() | |
# open and load photo json | |
json_data=open('photos.json') | |
data = json.load(json_data) | |
# loop through "photos" | |
for i in data['data']['photos']: | |
# save all resolutions of images | |
saveImage(i['images']['thumbnail']['url'], 'thumb'); | |
saveImage(i['images']['low_resolution']['url'], 'low'); | |
saveImage(i['images']['standard_resolution']['url'], 'standard'); | |
# save both resolutions of videos | |
try: | |
saveImage(i['videos']['standard_resolution']['url'], 'video_standard'); | |
saveImage(i['videos']['low_resolution']['url'], 'video_low'); | |
except: | |
print 'no video' | |
json_data.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment