Get data about an Instagram photo

Another example of doing something you've already done -- finding the information about a photo -- but doing it via API
Contents
Related files

Get media by shortcode

This is what we want to do:

https://api.instagram.com/v1/media/shortcode/THESHORTCODE?access_token=ACCESS-TOKEN

This endpoint returns the same response as GET /media/media-id. A media object’s shortcode can be found in its shortcode URL. An example shortcode is http://instagram.com/p/D/ Its corresponding shortcode is D`.

Find a photo

Find a photo:

    https://instagram.com/p/3eFma3IaPc/

Get the short id

    3eFma3IaPc

Reconstruct the URL

  • the domain: https://api.instagram.com
  • the path: /v1/media/shortcode/3eFma3IaPc
  • the query string: ?access_token=ACCESS-TOKEN

Copy the URL below, replace ACCESS-TOKEN with your own access token, and then paste it into your browser:

https://api.instagram.com/v1/media/shortcode/3eFma3IaPc?access_token=ACCESS-TOKEN

You should see something that looks like this:

image

Here’s what it looks like properly formatted:

{
  "meta": {
    "code": 200
  },
  "data": {
    "attribution": null,
    "tags": [
      "houston",
      "space",
      "eva",
      "spacewalk",
      "missioncontrol",
      "suitup",
      "nasa",
      "spacewalk50",
      "otd",
      "gemini"
    ],
    "type": "image",
    "location": null,
    "comments": {
      "count": 968,
      "data": [
        {
          "created_time": "1434770071",
          "text": "Lorem blah [Comment redacted for example purpose]s",
          "from": {
            "username": "example-user-1",
            "profile_picture": "https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/11380766_1594151867519935_1864575156_a.jpg",
            "id": "9999999988",
            "full_name": "Example Guy"
          },
          "id": "999999911987110320"
        },
        {
          "created_time": "1434791830",
          "text": "Amazing...[blah blah this is just an example]",
          "from": {
            "username": "jane_doe919119",
            "profile_picture": "https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/11380766_1594151867519935_1864575156_a.jpg",
            "id": "9999999989",
            "full_name": "Jane Doh"
          },
          "id": "9999999988416463514"
        }
      ]
    },
    "filter": "Normal",
    "created_time": "1433341191",
    "link": "https://instagram.com/p/3eFma3IaPc/",
    "likes": {
      "count": 143465,
      "data": [
        {
          "username": "Example2Dude123",
          "profile_picture": "https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/11380766_1594151867519935_1864575156_a.jpg",
          "id": "9999988777",
          "full_name": "Example Dude"
        },
        {
          "username": "DuchessYork9191",
          "profile_picture": "https://igcdn-photos-h-a.akamaihd.net/hphotos-ak-xfa1/t51.2885-19/11380766_1594151867519935_1864575156_a.jpg",
          "id": "9999988772",
          "full_name": "Duchess of York"
        }
      ]
    },
    "images": {
      "low_resolution": {
        "url": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s320x320/e15/11312029_374041792802247_1619900151_n.jpg",
        "width": 320,
        "height": 320
      },
      "thumbnail": {
        "url": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e15/11312029_374041792802247_1619900151_n.jpg",
        "width": 150,
        "height": 150
      },
      "standard_resolution": {
        "url": "https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/e15/11312029_374041792802247_1619900151_n.jpg",
        "width": 640,
        "height": 640
      }
    },
    "users_in_photo": [],
    "caption": {
      "created_time": "1433341191",
      "text": "50 years ago today, astronaut Ed White floated out of the Gemini IV spacecraft to become the first American to walk in space during the first Mission Controlled from Houston's manned spacecraft center. \nIn this image, White floats in the microgravity of space outside the Gemini IV spacecraft. Behind him is the brilliant blue Earth and its white cloud cover. White is wearing a specially-designed space suit. The visor of the helmet is gold plated to protect him against the unfiltered rays of the sun. In his left hand is a Hand-Held Self-Maneuvering Unit with which he controls his movements in space.\n\nCredits: NASA/Jim McDivitt\n\n#nasa #space #gemini #otd #spacewalk #eva #spacewalk50 #suitup #missioncontrol #houston",
      "from": {
        "username": "nasa",
        "profile_picture": "https://instagramimages-a.akamaihd.net/profiles/profile_528817151_75sq_1377792659.jpg",
        "id": "528817151",
        "full_name": "NASA"
      },
      "id": "999260799759393588"
    },
    "user_has_liked": false,
    "id": "999260797737739228_528817151",
    "user": {
      "username": "nasa",
      "profile_picture": "https://instagramimages-a.akamaihd.net/profiles/profile_528817151_75sq_1377792659.jpg",
      "id": "528817151",
      "full_name": "NASA"
    }
  }
}

[github] [raw]

Do it in code

import requests
url = "https://api.instagram.com/v1/media/shortcode/3eFma3IaPc?access_token=ACCESS-TOKEN"
resp = requests.get(url).json()
data = resp['data']
print(data['caption']['text'])

Break it up

ACCESS_TOKEN = "YOUR-ACCESS-TOKEN"
INSTAGRAM_DOMAIN = 'https://api.instagram.com'
MEDIA_PATH = '/v1/media/shortcode/%s'

shortcode = '3eFma3IaPc'
path = MEDIA_PATH % shortcode
url = INSTAGRAM_DOMAIN + path
atts = {"access_token": ACCESS_TOKEN}
resp = requests.get(url, params = atts).json()
data = resp['data']
print(data['caption']['text'])

Make it a function

import os
import requests
ACCESS_FILENAME = os.path.expanduser("~/Downloads/myinstagramcode.txt")
ACCESS_TOKEN = open(ACCESS_FILENAME).read().strip()
MEDIA_PATH = '/v1/media/shortcode/%s'
INSTAGRAM_DOMAIN = 'https://api.instagram.com'

def get_shortcode_photo(shortcode):
    path = MEDIA_PATH % shortcode
    url = INSTAGRAM_DOMAIN + path
    atts = {"access_token": ACCESS_TOKEN}
    resp = requests.get(url, params = atts).json()
    
    return resp['data']

Look for more inconveniences

https://instagram.com/p/3y8KeoIaN3/?taken-by=nasa

3y8KeoIaN3

How long did it take you to identify, highlight, and copy that shortcode? Was it a few seconds? Half a second? It doesn’t matter; even a tenth of a second is too slow for an action that petty.

url = "https://instagram.com/p/3y8KeoIaN3/?taken-by=nasa"
extract_shortcode(url)
# 3y8KeoIaN3

Naive solution

def extract_shortcode(weburl):
    x = weburl.split('instagram.com/p/')[1]
    y = x.split('/')[0]
    return y

extract_shortcode(weburl)

Compatibility concerns

However, what if a user expects to still be able to use the shortcode?

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-74-df769bc53035> in <module>()
----> 1 extract_shortcode('3y8KeoIaN3')

<ipython-input-64-623589064a65> in extract_shortcode(weburl)
      1 def extract_shortcode(weburl):
----> 2     x = weburl.split('instagram.com/p/')[1]
      3     y = x.split('/')[0]
      4     return y

IndexError: list index out of range
def extract_shortcode(weburl):
    u = weburl.split('instagram.com/p/')
    if len(u) > 1:
        x = u[1]
        return x.split('/')[0]
    else: # just return whatever you got
        return weburl

extract_shortcode('https://instagram.com/p/3y8KeoIaN3/?taken-by=nasa')
# 3y8KeoIaN3
extract_shortcode('https://instagram.com/p/3y8KeoIaN3/')
# 3y8KeoIaN3
extract_shortcode('3y8KeoIaN3')
# 3y8KeoIaN3

The full code can be found here: [github] [raw]