magroski
magroski

Reputation: 323

Getting instagram post url from media id

I have the post media_id in my hands and I'd like to know if there is a way to create a valid url from it.

For instance, if you have a Facebook post id (xxxx_yyyy) on your hands, you can create the following url from it (http://facebook.com/xxxx/posts/yyyy) and directly access the original post.

Is there a way to do this on Instagram? Having media_id (and user_id) in my hands, is it possible to create a single post url?

Upvotes: 4

Views: 13934

Answers (7)

ubershmekel
ubershmekel

Reputation: 12798

I made Nick's answer use the BigInt that ships with Node. I also avoid returning the full url, I just return the shortcode.

function getInstagramShortcodeFromId(media_id) {
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
  let shortenedId = '';

  while (media_id > 0) {
    const remainder = BigInt(media_id) % BigInt(64);
    media_id = ((BigInt(media_id) - BigInt(remainder)) / BigInt(64)).toString();
    shortenedId = alphabet.charAt(Number(remainder)) + shortenedId;
  }

  return shortenedId;
}

Upvotes: 1

Cyclonecode
Cyclonecode

Reputation: 30001

Yes this would be possible using the following endpoint one of the media endpoints:

Here is a small php script that will get the link based on media_id and user_id:

$media_id = 'media_id';
$user_id = 'user_id';
$client_id = 'instagram_client_id';
$client_secret = 'instagram_client_secret';
$url = "https://api.instagram.com/v1/media/{$media_id}_{$user_id}?client_id=$client_id&client_secret=$client_secret";
$response = file_get_contents($url);
$response = json_decode($response);
if (is_object($response)) {
  $link_to_media = $response->data->link;
}

In the above example you should replace the media_id, user_id, client_id, client_secret with their appropriate values. You should also be able to use an access_token instead of client_id and client_secret as in this example.

References

Media Endpoints

Upvotes: 1

tww0003
tww0003

Reputation: 839

I just stumbled across this question, all the solutions are great, but none of them were in a language I use often, so running the code would have been a pain in the ass for me.

Seeing that Python is everywhere, I ported over @seano's answer:

def getInstagramUrlFromMediaId(media_id):
    alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
    shortened_id = ''

    while media_id > 0:
        remainder = media_id % 64
        # dual conversion sign gets the right ID for new posts
        media_id = (media_id - remainder) // 64;
        # remainder should be casted as an integer to avoid a type error. 
        shortened_id = alphabet[int(remainder)] + shortened_id

    return 'https://instagram.com/p/' + shortened_id + '/'

Upvotes: 5

James
James

Reputation: 1460

The instagram API returns a "shortcode" property. Something simple like this could work for you.

post.Link = "https://www.instagram.com/p/" + media.shortcode;

Upvotes: 1

Nick Hanshaw
Nick Hanshaw

Reputation: 396

I had to implement client side javascript to solve this and Seano's answer was invaluable and I was glad they mentioned the use of the BigInteger library however I wanted to provide a complete implementation using the BigInteger library which as it turns out is quite necessary.

I downloaded the BigInteger lib from https://www.npmjs.com/package/big-integer.

Here is the function which works well for me.

function getInstagramUrlFromMediaId(media_id) {
    var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
    var shortenedId = '';
    media_id = media_id.substring(0, media_id.indexOf('_'));

    while (media_id > 0) {
        var remainder = bigInt(media_id).mod(64);
        media_id = bigInt(media_id).minus(remainder).divide(64).toString();
        shortenedId = alphabet.charAt(remainder) + shortenedId;
    }

    return 'https://www.instagram.com/p/' + shortenedId + '/';
}

I just want to point out that the usage of toString() when assigning the re-calculated media_id is very important, the value remains a string to ensure the entire number is used (in my case the media_id was 19 characters long). The BigInteger documentation also states this...

Note that Javascript numbers larger than 9007199254740992 and smaller than -9007199254740992 are not precisely represented numbers and will not produce exact results. If you are dealing with numbers outside that range, it is better to pass in strings.

Cheers!

Upvotes: 7

Sean H
Sean H

Reputation: 1066

Here's how to derive the URL segment from the media ID using JavaScript:

function getInstagramUrlFromMediaId(media_id) {
    media_id = parseInt(media_id.substring(0, media_id.indexOf('_')));
    var alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_';
    var shortenedId = '';
    while (media_id > 0) {
        var remainder = modulo(media_id % 64);
        media_id = (media_id - remainder) / 64;
        shortenedId = alphabet.charAt(remainder) + shortenedId;
    }
    return 'https://www.instagram.com/p/' + shortenedId + '/';
}

Depending on how old the Instagram post is, you may need to use a library like BigInteger to handle the larger IDs.

You can find a breakdown of the encoding between media id and url segment here: http://carrot.is/coding/instagram-ids

Upvotes: 1

Aaron Rumery
Aaron Rumery

Reputation: 13

You can also figure it out algorithmically. It can be done in any language, but here is a way via MySQL function:

DROP FUNCTION IF EXISTS url_fragment

DELIMITER |

CREATE FUNCTION url_fragment(PID BIGINT) 
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN

DECLARE ALPHA CHAR(64);
DECLARE SHORTCODE VARCHAR(255) DEFAULT "";
DECLARE MEDIAID BIGINT;
DECLARE REMAINDER INT;

SET ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
SET MEDIAID = SUBSTRING_INDEX(PID, "_", 1);


WHILE MEDIAID > 0 DO
    SET REMAINDER = MOD(MEDIAID, 64);
    SET MEDIAID = (MEDIAID - REMAINDER) / 64;
    SET SHORTCODE = CONCAT(SUBSTRING(ALPHA, (REMAINDER + 1), 1), SHORTCODE);
END WHILE;

RETURN SHORTCODE;

END
|
DELIMITER ;

Upvotes: 1

Related Questions