Skip to content

Instantly share code, notes, and snippets.

@gregkepler
Last active December 25, 2015 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregkepler/6941486 to your computer and use it in GitHub Desktop.
Save gregkepler/6941486 to your computer and use it in GitHub Desktop.
Saves all photos and videos in a json file of instagram photos.
// 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
}
}
...
}
}
]
}
}
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