Taz
Taz

Reputation: 617

Where do I find the Instagram media ID of a image

I'm am looking for the MediaID of an Instagram image which has been uploaded. It should look like

1234567894561231236_33215652

I have found out the last set of integers are the usersID

For example: this is the link for the image directly, however I see no mediaID in the correct format?

http://distilleryimage11.ak.instagram.com/d33aafc8b55d11e2a66b22000a9f09de_7.jpg

while this is the link

http://instagram.com/p/Y7GF-5vftL/

I don't wish to use the API as all I need the MediaID from a selected image.

Upvotes: 46

Views: 191207

Answers (16)

Faiz
Faiz

Reputation: 7000

  1. Instagram media id to Shortcode

  2. Instagram Shortcode to media id

var bigint = require( 'big-integer' )

var lower = 'abcdefghijklmnopqrstuvwxyz';
var upper = lower.toUpperCase();
var numbers = '0123456789'
var ig_alphabet =  upper + lower + numbers + '-_'
var bigint_alphabet = numbers + lower

function toShortcode( longid ){

    var o = bigint( longid ).toString( 64 )
    return o.replace(/<(\d+)>|(\w)/g, (m,m1,m2) =>{
    return ig_alphabet.charAt( ( m1 )
      ? parseInt( m1 ) 
      : bigint_alphabet.indexOf( m2 ) )
    });
}

function fromShortcode( shortcode ){
    var o = shortcode.replace( /\S/g, m =>{
    var c = ig_alphabet.indexOf( m )
    var b = bigint_alphabet.charAt( c ) 
    return ( b != "" ) ? b : `<${c}>`
    })  
    return bigint( o, 64 ).toString( 10 )
}

toShortcode(  '908540701891980503' ) // s.b. 'ybyPRoQWzX'
fromShortcode( 'ybyPRoQWzX' ) // s.b. '908540701891980503'

Upvotes: 1

Nava Bogatee
Nava Bogatee

Reputation: 1765

Instagram deprecated their legacy APIs in support for Basic Display API during the late 2019

In Basic Display API you are supposed to use the following API endpoint to get the media id. You will need to supply a valid access token.

https://graph.instagram.com/me/media?fields=id,caption&access_token={access-token}

You can read here on how to configure test account and generate access token on Facebook developer portal.

Here is another article which also describes about how to get access token.

Upvotes: 0

Arashnm80
Arashnm80

Reputation: 5

instaloader method

You can do it with instaloader python module too.

First install it with a command like this:

pip3 install instaloader

then you can get media id from a shortcode like this:

import instaloader
from instaloader import *

L = instaloader.Instaloader()
post_shortcode = "Y7GF-5vftL" # or whatever code it is
media_id = Post.shortcode_to_mediaid(post_shortcode)
print(media_id)

for more information you can refer to here

Upvotes: 0

Elad Ratson
Elad Ratson

Reputation: 724

Here's a much better way (No Instagram API):

  1. Get the permalink URL of the Instagram image you need the media ID for.
    http://instagram.com/p/Y7GF-5vftL/ 👀

  2. add ?__a=1 at the end of the permalink
    http://instagram.com/p/Y7GF-5vftL/?__a=1 👀

Run the link in a browser and Instagram will provide all the metadata of the image in JSON, from which you can easily recover the image ID and other valuable data.

The JSON response will look like this:

{
  "graphql": {
    "shortcode_media": {
      "__typename": "GraphImage",
      "id": "448979387270691659",
      "shortcode": "Y7GF-5vftL",
      "dimensions": {
        "height": 612,
        "width": 612
      },
      "gating_info": null,
      "fact_check_overall_rating": null,
      "fact_check_information": null,
      "sensitivity_friction_info": null,
      "sharing_friction_info": {
        "should_have_sharing_friction": false,
        "bloks_app_url": null
      },
      "media_overlay_info": null,
      "media_preview": null,
      "display_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
      "display_resources": [
        {
          "src": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
          "config_width": 640,
          "config_height": 640
        },
        {
          "src": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
          "config_width": 750,
          "config_height": 750
        },
        {
          "src": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-15/e15/11324452_400723196800905_116356150_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=109&_nc_ohc=YQtotj4Ygh0AX9Vqa8s&edm=AABBvjUBAAAA&ccb=7-4&oh=9f8658a873a3e94a462db148bde85b5a&oe=61A49EE6&_nc_sid=83d603",
          "config_width": 1080,
          "config_height": 1080
        }
      ],
      "accessibility_caption": "Photo by Steven Caelius Thirlwall on May 05, 2013.",
      "is_video": false,
      "tracking_token": "eyJ2ZXJzaW9uIjo1LCJwYXlsb2FkIjp7ImlzX2FuYWx5dGljc190cmFja2VkIjpmYWxzZSwidXVpZCI6IjFjOTliZDQ5MTU0YzQ2ODY4OGY5MmM4ODgwYWQ4NGQzNDQ4OTc5Mzg3MjcwNjkxNjU5Iiwic2VydmVyX3Rva2VuIjoiMTYzNzY4NDc1NjI4MHw0NDg5NzkzODcyNzA2OTE2NTl8MzExMTA2NDAyfDE2ZGMyNjk3M2M1YWY5YWEzOTNhZTY5YzEzYzU4YjM5NWI0YWQ3MjY1OGQxOTg4YWQ2OWUxYmI3ZjkyNzU5ZDQifSwic2lnbmF0dXJlIjoiIn0=",
      "upcoming_event": null,
      "edge_media_to_tagged_user": {
        "edges": []
      },
      "edge_media_to_caption": {
        "edges": [
          {
            "node": {
              "text": "New ankle biter! #Beagle #pup #shoes #blueisthecolor #cute #iwantone"
            }
          }
        ]
      },
      "can_see_insights_as_brand": false,
      "caption_is_edited": false,
      "has_ranked_comments": false,
      "like_and_view_counts_disabled": false,
      "edge_media_to_parent_comment": {
        "count": 902,
        "page_info": {
          "has_next_page": true,
          "end_cursor": "QVFBZFlTbmNsVWh1T2dIS1I3Mm8yRl9DdGVQMHV6VXg5cjQxZzlYa0gxQ1NFZGc5a1FfWHhIMTlURU84dlBCcG5QX1I2VXhvVXNpbGRGWlktNG5FTjdRTQ=="
        },
        "edges": [
          {
            "node": {
              "id": "17916614443887715",
              "text": "@h___ep10",
              "created_at": 1632633242,
              "did_report_as_spam": false,
              "owner": {
                "id": "7472159900",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/33736445_386998071786552_2814599361646821376_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=100&_nc_ohc=kaD1UHiVnU8AX9oWv0I&edm=AABBvjUBAAAA&ccb=7-4&oh=cabcfaac8d48b63b41f0e9a32bb0282f&oe=61A38CDE&_nc_sid=83d603",
                "username": "x._raha.moradi_.x"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17894221358363984",
              "text": "@zahraroshanikia",
              "created_at": 1632633244,
              "did_report_as_spam": false,
              "owner": {
                "id": "46440556914",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/159256912_431734447917250_5641996282890612011_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=110&_nc_ohc=y2JgjQ2mc6EAX8U9OnZ&edm=AABBvjUBAAAA&ccb=7-4&oh=95911a34aea9f1fe0f6ae941154de86b&oe=61A4F7B5&_nc_sid=83d603",
                "username": "saman.wx68"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17931125308699029",
              "text": "@azarimani55",
              "created_at": 1632633244,
              "did_report_as_spam": false,
              "owner": {
                "id": "33210479560",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/94635563_230560861379228_5419754827787796480_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=108&_nc_ohc=7JyeFrDCzcMAX_uDrhk&edm=AABBvjUBAAAA&ccb=7-4&oh=5599a78e5508ca3827b5c580c4e5daf0&oe=61A3CB8E&_nc_sid=83d603",
                "username": "alirezaekhteraee"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "18164016367163647",
              "text": "@mbahrambagi436",
              "created_at": 1632633245,
              "did_report_as_spam": false,
              "owner": {
                "id": "45915987079",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/148716764_428606351708713_8819828798031017255_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=101&_nc_ohc=ZKBGqc8f6xAAX_roxPf&edm=AABBvjUBAAAA&ccb=7-4&oh=0b8ed79236bf5e97dde3a4621a1cc647&oe=61A3B2BD&_nc_sid=83d603",
                "username": "xx77llxxrdd"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17921916424799580",
              "text": "@soheila.rezvan",
              "created_at": 1632633246,
              "did_report_as_spam": false,
              "owner": {
                "id": "47289012029",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "ha_midreza2843"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17907564257125873",
              "text": "@paria_niazi80",
              "created_at": 1632633247,
              "did_report_as_spam": false,
              "owner": {
                "id": "46492412778",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/260070060_924734438469169_5591668570031114480_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=101&_nc_ohc=fl-bN9uNz9sAX_v_XtR&edm=AABBvjUBAAAA&ccb=7-4&oh=a875da2937ac853d3111499bc32fa452&oe=61A43C46&_nc_sid=83d603",
                "username": "ricky_gutierrezz4"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17922561976794017",
              "text": "@memmm.mom",
              "created_at": 1632633247,
              "did_report_as_spam": false,
              "owner": {
                "id": "47086299475",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "izadiizadi2"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "18130172461216294",
              "text": "@221bcc",
              "created_at": 1632633247,
              "did_report_as_spam": false,
              "owner": {
                "id": "47371753534",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "karimov_94422"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17932389193640009",
              "text": "@tehran___la",
              "created_at": 1632633248,
              "did_report_as_spam": false,
              "owner": {
                "id": "46622156073",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "rynwrynw627"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17935588228640366",
              "text": "@sepideh.m.i.r",
              "created_at": 1632633248,
              "did_report_as_spam": false,
              "owner": {
                "id": "5905800882",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/160110991_451499282792610_787693123774809640_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=106&_nc_ohc=CBgOU0M1MPYAX8m7vvT&edm=AABBvjUBAAAA&ccb=7-4&oh=18a215ed2c3a0cd0e58a905e22cd0c8f&oe=61A489B4&_nc_sid=83d603",
                "username": "__mohammad__677"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17854564814634983",
              "text": "@nilooofar5828",
              "created_at": 1632633249,
              "did_report_as_spam": false,
              "owner": {
                "id": "46091925856",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/44884218_345707102882519_2446069589734326272_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=1&_nc_ohc=uQzgj27XaGsAX_EK6es&edm=AA0lj5EBAAAA&ccb=7-4&oh=11aea858038a83aea909c6a5934ac670&oe=61A4378F&_nc_sid=3add00&ig_cache_key=YW5vbnltb3VzX3Byb2ZpbGVfcGlj.2-ccb7-4",
                "username": "hsheh6154"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          },
          {
            "node": {
              "id": "17907793715125698",
              "text": "@oran31201816",
              "created_at": 1632633249,
              "did_report_as_spam": false,
              "owner": {
                "id": "44672434922",
                "is_verified": false,
                "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/149881733_183016319827876_8572211010018355650_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=104&_nc_ohc=JgjvztBQGrIAX8nLUiM&tn=hRpIRP1GgMkkZ4n7&edm=AABBvjUBAAAA&ccb=7-4&oh=d9e408cf78f195a83f8bb7beec8c22c1&oe=61A360EC&_nc_sid=83d603",
                "username": "barcelona10_trol"
              },
              "viewer_has_liked": false,
              "edge_liked_by": {
                "count": 0
              },
              "is_restricted_pending": false,
              "edge_threaded_comments": {
                "count": 0,
                "page_info": {
                  "has_next_page": false,
                  "end_cursor": null
                },
                "edges": []
              }
            }
          }
        ]
      },
      "edge_media_to_hoisted_comment": {
        "edges": []
      },
      "edge_media_preview_comment": {
        "count": 902,
        "edges": []
      },
      "comments_disabled": false,
      "commenting_disabled_for_viewer": false,
      "taken_at_timestamp": 1367742535,
      "edge_media_preview_like": {
        "count": 13823,
        "edges": [
          {
            "node": {
              "id": "750983393",
              "is_verified": false,
              "profile_pic_url": "https://scontent-cdt1-1.cdninstagram.com/v/t51.2885-19/s150x150/245113529_402184758053910_6991076024439577169_n.jpg?_nc_ht=scontent-cdt1-1.cdninstagram.com&_nc_cat=106&_nc_ohc=TPs3jHagdx4AX_JdrO5&tn=hRpIRP1GgMkkZ4n7&edm=AABBvjUBAAAA&ccb=7-4&oh=95f6532e2d737a91e3848fa0525319e4&oe=61A4C0D1&_nc_sid=83d603",
              "username": "josepkoray"
            }
          }
        ]
      },
      "edge_media_to_sponsor_user": {
        "edges": []
      },
      "is_affiliate": false,
      "is_paid_partnership": false,
      "location": null,
      "viewer_has_liked": false,
      "viewer_has_saved": false,
      "viewer_has_saved_to_collection": false,
      "viewer_in_photo_of_you": false,
      "viewer_can_reshare": true,
      "owner": {
        "id": "45818965",
        "is_verified": false,
        "profile_pic_url": "https://scontent-cdg2-1.cdninstagram.com/v/t51.2885-19/s150x150/38792937_2121773964703196_2247098649857228800_n.jpg?_nc_ht=scontent-cdg2-1.cdninstagram.com&_nc_cat=108&_nc_ohc=LyE1N2PfQ3IAX9R25jq&edm=AABBvjUBAAAA&ccb=7-4&oh=29912db673500a2adc9df6ec7241d8c7&oe=61A471D2&_nc_sid=83d603",
        "username": "taz4535",
        "blocked_by_viewer": false,
        "restricted_by_viewer": false,
        "followed_by_viewer": false,
        "full_name": "Steven Caelius Thirlwall",
        "has_blocked_viewer": false,
        "is_embeds_disabled": false,
        "is_private": false,
        "is_unpublished": false,
        "requested_by_viewer": false,
        "pass_tiering_recommendation": false,
        "edge_owner_to_timeline_media": {
          "count": 236
        },
        "edge_followed_by": {
          "count": 700
        }
      },
      "is_ad": false,
      "edge_web_media_to_related_media": {
        "edges": []
      },
      "coauthor_producers": [],
      "edge_related_profiles": {
        "edges": []
      }
    }
  }
}

You can use the same ?__a=1 ending even on Instagram user permalinks, for example https://www.instagram.com/taz4535/?__a=1 👀 and you'll get incredibly valuable user data in the reply JSON.

The best is that this is all done without Instagram API authentication!

Upvotes: 16

Loufs
Loufs

Reputation: 1656

So the most up-voted "Better Way" is a little deprecated so here is my edit and other solutions:

Javascript + jQuery

$.ajax({
    type: 'GET',
    url: 'http://api.instagram.com/oembed?callback=&url='+Url, //You must define 'Url' for yourself
    cache: false,
    dataType: 'json',
    jsonp: false,
    success: function (data) {
       var MediaID = data.media_id;
   }
});

PHP

$your_url = "" //Input your url
$api = file_get_contents("http://api.instagram.com/oembed?callback=&url=" . your_url);      
$media_id = json_decode($api,true)['media_id'];

So, this is just an updated version of @George's code and is currently working. However, I made other solutions, and some even avoid an ajax request:


Shortcode Ajax Solution

Certain Instagram urls use a shortened url syntax. This allows the client to just use the shortcode in place of the media id if requested properly.

An example shortcode url looks like this: https://www.instagram.com/p/Y7GF-5vftL/

The Y7GF-5vftL is your shortcode for the picture.

Using Regexp:

var url = "https://www.instagram.com/p/Y7GF-5vftL/"; //Define this yourself
var Key = /p\/(.*?)\/$/.exec(url)[1];

In the same scope, Key will contain your shortcode. Now to request, let's say, a low res picture using this shortcode, you would do something like the following:

    $.ajax({
        type: "GET",
        dataType: "json",
        url: "https://api.instagram.com/v1/media/shortcode/" + Key + "?access_token=" + access_token, //Define your 'access_token'
        success: function (RawData) {
            var LowResURL = RawData.data.images.low_resolution.url;
        }
    });

There is lots of other useful information, including the media id, in the returned RawData structure. Log it or look up the api documentation to see.


Shortcode Conversion Solution

You can actually convert your shortcode to the id fairly easily! Here's a simple way to do it in javascript:

function shortcodeToInstaID(Shortcode) {
      var char;
      var id = 0;
      var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
      for (var i = 0; i < Shortcode.length; i++) {
          char = Shortcode[i];
          id = (id * 64) + alphabet.indexOf(char);
      }
      return id;
  }

Note: If you want a more robust node.js solution, or want to see how you would convert it back, check out @Slang's module on npm.


Full Page Solution

So what if you have the URL to a full Instagram page like: https://www.instagram.com/p/BAYYJBwi0Tssh605CJP2bmSuRpm_Jt7V_S8q9A0/

Well, you can actually read the HTML to find a meta property that contains the Media ID. There are also a couple other algorithms you can perform on the URL itself to get it, but I believe that requires too much effort so we will keep it simple. Either query the meta tag al:ios:url or iterate through the html. Since reading metatags is posted all over, I'll show you how to iterate.

NOTE: This is a little unstable and is vulnerable to being patched. This method does NOT work on a page that uses a preview box. So if you give it the current HTML when you click on a picture in someone's profile, this WILL break and return a bad Media ID.

function getMediaId(HTML_String) {
  var MediaID = "";
  var e = HTML_String.indexOf("al:ios:url") + 42; //HTML_String is a variable that contains all of the HTML text as a string for the current document. There are many different ways to retrieve this so look one up. 
  for (var i = e; i <= e + 100; i++) { //100 should never come close to being reached
      if (request.source.charAt(i) == "\"")
         break;
      MediaID += request.source.charAt(i);
  }
  return MediaID;
}

And there you go, a bunch of different ways to use Instagram's api to get a Media ID. Hope one fixes your struggles.

Upvotes: 24

SamB
SamB

Reputation: 3245

edit

The iOS Instagram app has now registered for regular http links to open in the Instagram app and this deeplink methodology is no longer necessary.

old

Swift 4 short-code parsing solution

private static func instagramDeepLinkFromHTTPLink(_ link: String) -> String? {
    guard let shortcode = link.components(separatedBy: "/").last else { return nil }

    // algorithm from https://stackoverflow.com/a/37246231/337934
    let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
    var mediaId: Int = 0
    for (_, char) in shortcode.enumerated() {
        guard let index = alphabet.index(of: char) else { continue }
        mediaId = (mediaId * 64) + index.encodedOffset
    }

    return "instagram://media?id=\(mediaId)"
}

Upvotes: 2

Shubham Gupta
Shubham Gupta

Reputation: 81

Same thing you can implement in Python-

import requests,json

def get_media_id(media_url):
   url = 'https://api.instagram.com/oembed/?callback=&url=' + media_url
   response = requests.get(url).json()
   print(response['media_id'])

get_media_id('MEDIA_URL')

Upvotes: 1

totas
totas

Reputation: 10760

You can use the shortcode media API from instagram. If you use php you can use the following code to get the shortcode from the image's URL:

$matches = [];

preg_match('/instagram\.com\/p\/([^\/]*)/i', $url, $matches);

if (count($matches) > 1) {
    $shortcode = $matches[1];
}

Then send a request to the API using your access token (Replace ACCESS-TOKEN with your token)

$apiUrl = sprintf("https://api.instagram.com/v1/media/shortcode/%s?access_token=ACCESS-TOKEN", $shortcode);

Upvotes: 0

Abdul Majeed
Abdul Majeed

Reputation: 2781

Here is python solution to do this without api call.

def media_id_to_code(media_id):
    alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
    short_code = ''
    while media_id > 0:
        remainder = media_id % 64
        media_id = (media_id-remainder)/64
        short_code = alphabet[remainder] + short_code
    return short_code


def code_to_media_id(short_code):
    alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
    media_id = 0;
    for letter in short_code:
        media_id = (media_id*64) + alphabet.index(letter)

    return media_id

Upvotes: 9

George
George

Reputation: 6084

Here's a better way:

http://api.instagram.com/oembed?url=http://instagram.com/p/Y7GF-5vftL/

Render as json object and you can easily extract media id from it ---

For instance, in PHP

$api = file_get_contents("http://api.instagram.com/oembed?url=http://instagram.com/p/Y7‌​GF-5vftL/");      
$apiObj = json_decode($api,true);      
$media_id = $apiObj['media_id'];

For instance, in JS

$.ajax({     
    type: 'GET',     
    url: 'http://api.instagram.com/oembed?callback=&url=http://instagram.com/p/Y7GF-5vftL‌​/',     
    cache: false,     
    dataType: 'jsonp',     
    success: function(data) {           
        try{              
            var media_id = data[0].media_id;          
        }catch(err){}   
    } 
});

Upvotes: 102

Tal Zion
Tal Zion

Reputation: 6526

Right click on a photo and open in a new tab/window. Right click on inspect element. Search for:

instagram://media?id=

This will give you:

instagram://media?id=############# /// the ID

The full id construct from

photoID_userID

To get the user id, search for:

instapp:owner_user_id Will be in content=

Upvotes: -1

gerbz
gerbz

Reputation: 1020

Here's an even better way:

No API calls! And I threw in converting a media_id to a shortcode as an added bonus.

Based on slang's amazing work for figuring out the conversion. Nathan's work converting base10 to base64 in php. And rgbflawed's work converting it back the other way (with a modified alphabet). #teameffort

function mediaid_to_shortcode($mediaid){

    if(strpos($mediaid, '_') !== false){
        $pieces = explode('_', $mediaid);
        $mediaid = $pieces[0];
        $userid = $pieces[1];
    }

    $alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
    $shortcode = '';
    while($mediaid > 0){
        $remainder = $mediaid % 64;
        $mediaid = ($mediaid-$remainder) / 64;
        $shortcode = $alphabet{$remainder} . $shortcode;
    };

    return $shortcode;

}

function shortcode_to_mediaid($shortcode){

    $alphabet='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';

    $mediaid = 0;

    foreach(str_split($shortcode) as $letter) {
        $mediaid = ($mediaid*64) + strpos($alphabet, $letter);
    }

    return $mediaid;

}

Upvotes: 23

user1992271
user1992271

Reputation: 259

Your media ID is: 448979387270691659_45818965 This is how to get it.

  1. Go to instgram.com/username.
  2. Click the photo you want the id of.
  3. (Chrome instructions) right click on the photo (should be a popup image)
  4. Inspect element
  5. Search through the selected text, you should see something like this photo448979387270691659_45818965

There should be your photo ID.

For some reason, this only seems to work with the popup, and not the actual image URL.

Upvotes: 3

slang
slang

Reputation: 640

You can actually derive the MediaId from the last segment of the link algorithmically using a method I wrote about here: http://carrot.is/coding/instagram-ids. It works by mapping the URL segment by character codes & converting the id into a base 64 number.

For example, given the link you mentioned (http://instagram.com/p/Y7GF-5vftL), we get the last segment (Y7GF-5vftL) then we map it into character codes using the base64 url-safe alphabet (24:59:6:5:62:57:47:31:45:11_64). Next, we convert this base64 number into base10 (448979387270691659).

If you append your userId after an _ you get the full id in the form you specified, but since the MediaId is unique without the userId you can actually omit the userId from most requests.

Finally, I made a Node.js module called instagram-id-to-url-segment to automate this conversion:

convert = require('instagram-id-to-url-segment');
instagramIdToUrlSegment = convert.instagramIdToUrlSegment;
urlSegmentToInstagramId = convert.urlSegmentToInstagramId;

instagramIdToUrlSegment('448979387270691659'); // Y7GF-5vftL
urlSegmentToInstagramId('Y7GF-5vftL'); // 448979387270691659

Upvotes: 14

cluzt
cluzt

Reputation: 89

Try the solution from this question: How can I get an direct Instagram link from a twitter entity?

You can get just the image by appending /media/ to the URL. Using your

You can even specify a size,

One of t (thumbnail), m (medium), l (large). Defaults to m.

So for a thumbnail: http://instagr.am/p/QC8hWKL_4K/media/?size=t

Upvotes: 8

realkstrawn93
realkstrawn93

Reputation: 796

In pure JS (provided your browser can handle XHRs, which every major browser [including IE > 6] can):

function igurlretrieve(url) {

  var urldsrc = "http://api.instagram.com/oembed?url=" + url;

  //fetch data from URL data source
  var x = new XMLHttpRequest();
  x.open('GET', urldsrc, true);
  x.send();

  //load resulting JSON data as JS object
  var urldata = JSON.parse(x.responseText);

  //reconstruct data as "instagram://" URL that can be opened in iOS app
  var reconsturl = "instagram://media?id=" + urldata.media_id;
  return reconsturl;

}

Provided this is your goal -- simply opening the page in the Instagram iOS app, which is exactly what this is about -- this should do, especially if you don't want to have to endure licensing fees.

Upvotes: 3

Related Questions