Skip to content

Instantly share code, notes, and snippets.

@sclark39
Last active July 18, 2023 11:59
Show Gist options
  • Save sclark39/9daf13eea9c0b381667b61e3d2e7bc11 to your computer and use it in GitHub Desktop.
Save sclark39/9daf13eea9c0b381667b61e3d2e7bc11 to your computer and use it in GitHub Desktop.
Convert from Instagram Shortcode to Id and Back using big-integer
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'
@sclark39
Copy link
Author

It would be a lot better to just make a PR to big-integer to allow for custom alphabets for B64

@itdaniher
Copy link

itdaniher commented Sep 24, 2018

In Python if anyone is interested!

>>> id_to_shortcode = lambda instagram_id: base64.b64encode(instagram_id.to_bytes(9, 'big'), b'-_').decode().replace('A', ' ').lstrip().replace(' ', 'A')
>>> def shortcode_to_id(shortcode):
...     code = ('A' * (12-len(shortcode)))+shortcode
...     return int.from_bytes(base64.b64decode(code.encode(), b'-_'), 'big')

@ppKrauss
Copy link

Ideal to use module.exports as explained by https://stackoverflow.com/a/5801971/287948

@blixit
Copy link

blixit commented May 8, 2021

Hi @sclark39, and thanks for this great script.

Unfortunately, it's not working for me:

  • fromShortcode("CG53Utagki0") returns 2430216789653866676
  • toShortcode("2430216789653866676") returns CG53Utagki0

But the id "2430216789653866676" is not usable with the basic graph api.

  • If I call https://graph.instagram.com/2430216789653866676?...
  • I get the following error: "Unsupported get request. Object with ID '2430216789653866676' does not exist, cannot be loaded due to missing permissions, or does not support this operation"

But if I try to get the media id from .../me?fields=media&... I get a completly different id: 17893233037673372 which works fine with https://graph.instagram.com/17893233037673372?...

Do you have a clue to fix this issue ? Is this script expected to work with the instagram Basic Graph API ?

@sclark39
Copy link
Author

sclark39 commented May 10, 2021

Unfortunately, it's not working for me:

  • fromShortcode("CG53Utagki0") returns 2430216789653866676
  • toShortcode("2430216789653866676") returns CG53Utagki0

Do you have a clue to fix this issue ? Is this script expected to work with the instagram Basic Graph API ?

Hey @blixit, do you know what CG53Utagki0 should convert to? Do you expect 17893233037673372 out of it?

I haven't been working with the Instagram API recently, but there is some more information about Media IDs and converting between the formats here which may be helpful:

@blixit
Copy link

blixit commented May 14, 2021

hello @sclark39, sorry for the delay. I was expecting CG53Utagki0 to be converted to 17893233037673372 but actually I was wrong.

I got the confirmation that your functions are doing the job by adding ?__a=1 to https://instagram.com/p/CG53Utagki0. Doing so, I can see both CG53Utagki0 and 2430216789653866676 standing in the same object.

My conclusion is that the media you get with .../me?fields=... is built in another way.

@jamzy118
Copy link

@blixit @sclark39 Yep, that doesn't work with Basic Display API :(

For example I have a post: https://www.instagram.com/p/BWsP42nAHbD?__a=1

(__a=1 shows you raw JSON of this post)

JSON above states that the post has ID 1561693048415483587 which is properly converted to BWsP42nAHbD by this script.

But if I download the same post through Basic Display API, this exact post comes as:

{
  "id": "17883949432067035",
  "caption": "#water #lake #poland"
}

and obviously shortcode generated from this is wrong. Tried to find solution for that whole day, but it seems that Instagram API is just unusable crap, no wonder so many brutal Instagram scrapers arrived at npm within last two years :(

@pierrocknroll
Copy link

pierrocknroll commented Aug 10, 2021

and obviously shortcode generated from this is wrong. Tried to find solution for that whole day, but it seems that Instagram API is just unusable crap, no wonder so many brutal Instagram scrapers arrived at npm within last two years :(

Hey @jamzy118 did you find a solution to your problem ?
I have the exact same.

I post a media through the Graph API and it answers me an ID, but the shortcode generated from this ID is wrong.
Received ID : 17889455560051444
Incorrect shortcode from this ID : _dxrQnDeT
Correct shortcode : CSZUyULIgWQ
ID from this shortcode : 2637230475367810448

@joshwolff1
Copy link

Same problem as pierrocknroll ^

@filipencus
Copy link

Same here. Spent 2 days trying to fix this basic conversion which FB could do it for us. It makes total sense.

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