Skip to content

Instantly share code, notes, and snippets.

@bsnux
Last active December 31, 2015 22:49
Show Gist options
  • Save bsnux/8056485 to your computer and use it in GitHub Desktop.
Save bsnux/8056485 to your computer and use it in GitHub Desktop.
Gets a public URL for a given Instagram photo ID
#!/usr/bin/python
#------------------------------------------------------------------------
# Gets public URL for a given Instagram photo ID.
# Example: http://instagram.com/p/h6qXCCmCJf/ => photo_id = h6qXCCmCJf
#------------------------------------------------------------------------
import requests
import sys
import json
if len(sys.argv) != 2:
print('Error! Please, use a photo ID')
exit()
photo_id = sys.argv[1]
url = 'http://api.instagram.com/oembed?url=http://instagram.com/p/{0}/'
response = requests.get(url.format(photo_id))
if response.status_code != 200:
print('Error! {0}'.format(response.status_code))
exit()
json_res = json.loads(response.content)
if 'url' not in json_res.keys():
print('Error! Bad photo ID or bad response')
exit()
print(json_res['url'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment