Skip to content

Instantly share code, notes, and snippets.

@ohld
Created February 19, 2020 20:14
Show Gist options
  • Select an option

  • Save ohld/62c2090e43bc0b6cc5e73dcc72c45901 to your computer and use it in GitHub Desktop.

Select an option

Save ohld/62c2090e43bc0b6cc5e73dcc72c45901 to your computer and use it in GitHub Desktop.
Convert Instagram media URL to media_id
# This is Python port of this Javascript method:
# https://github.com/notslang/instagram-id-to-url-segment/blob/master/lib/index.coffee
url = "https://www.instagram.com/p/B8iwlG9pXHI/"
# ----> use regexp to extract code from url
code = "B8iwlG9pXHI"
charmap = {
'A': '0',
'B': '1',
'C': '2',
'D': '3',
'E': '4',
'F': '5',
'G': '6',
'H': '7',
'I': '8',
'J': '9',
'K': 'a',
'L': 'b',
'M': 'c',
'N': 'd',
'O': 'e',
'P': 'f',
'Q': 'g',
'R': 'h',
'S': 'i',
'T': 'j',
'U': 'k',
'V': 'l',
'W': 'm',
'X': 'n',
'Y': 'o',
'Z': 'p',
'a': 'q',
'b': 'r',
'c': 's',
'd': 't',
'e': 'u',
'f': 'v',
'g': 'w',
'h': 'x',
'i': 'y',
'j': 'z',
'k': 'A',
'l': 'B',
'm': 'C',
'n': 'D',
'o': 'E',
'p': 'F',
'q': 'G',
'r': 'H',
's': 'I',
't': 'J',
'u': 'K',
'v': 'L',
'w': 'M',
'x': 'N',
'y': 'O',
'z': 'P',
'0': 'Q',
'1': 'R',
'2': 'S',
'3': 'T',
'4': 'U',
'5': 'V',
'6': 'W',
'7': 'X',
'8': 'Y',
'9': 'Z',
'-': '$',
'_': '_'
}
def instagram_code_to_media_id(code):
code = "B8iwlG9pXHI"
id = ""
for letter in code:
id += charmap[letter]
alphabet = list(charmap.values())
number = 0
for char in id:
number = number * 64 + alphabet.index(char)
return number
media_id = instagram_code_to_media_id(code)
# media_id == 2243569220713804232 # success
@lucasoal

Copy link
Copy Markdown

very useful, my friend
thanks!!

@itz-danish

Copy link
Copy Markdown

You deserves a star bro.... :) thanks for the help

@Ghandour1122

Copy link
Copy Markdown

Thanks bro

@bdbishoff

Copy link
Copy Markdown

Hi is this method still working? I tried using it but the media_id I get back is not matching with what the instagram api is saying. I tested it on multiple posts and it's still not working. When I do the trick of adding this to the end of a url ?__a=1&__d=dis, the number your function returns lines up with the field in the json object, "pk", the first part of "id" before the underscore and "strong_id". When I try to query these fields specifically instead of "id", it doesn't work (obivously since the actual documentation states "id" as a valid field). In short, it feels like they purposely made it so that this code doesn't work by using a different "id" to get insights about the post even though the media_id this code generates does show up in other fields of the posts json object but not the official media object returned from the api.

@pragnesh107

Copy link
Copy Markdown

@bdbishoff have you found any other solution to retrieve mediaId of shortcode

@spidyshivam

Copy link
Copy Markdown

hey its not working anymore, can someone tell any method to find media id using Instagram post URL ?

@isaacglover527-a11y

Copy link
Copy Markdown

In today’s fast-evolving social media world, staying ahead of Instagram’s algorithm updates is crucial for boosting engagement and growth. It can help creators and businesses optimize their Instagram experience free, giving them access to tools that complement the latest platform changes and maintain a competitive edge.

@Gus-nodes

Copy link
Copy Markdown

To convert an Instagram media URL to a media_id, the easiest method is using Instagram oEmbed. You pass the Instagram post URL to the oEmbed endpoint, and the response includes the media_id. This is the clean, official approach and works for posts, reels, and videos.

Example placeholder in context (not related to Instagram, just included as requested): https://apkhillcrapk.com/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment