18

Thanks to a HTTP call not very official, I find media ID, image URL, and user name of Instagram posts.

But I need the URL of each post on Instagram, and I don't know how to find it.

Is there an URL like instagram.com/something/{mediaID} which redirect to instagram.com/p/{mediaSlug}, or another method to find slug by media ID (without using the official API of course!) ?

For example, I've got :

Unique number : 1238578393243739028_1408429375

Media ID : 1238578393243739028

User ID : 1408429375

And I would :

https://www.instagram.com/p/BEwUHyDxGOU/

Thanks for your help !

8 Answers 8

14

Java Solution:

public static String getInstagramPostId(String mediaId) {
  String postId = "";
  try {
    long id = Long.parseLong(mediaId.substring(0, mediaId.indexOf('_')));
    String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";

    while (id > 0) {
      long remainder = (id % 64);
      id = (id - remainder) / 64;
      postId = alphabet.charAt((int)remainder) + postId;
    }
  } catch (Exception e) {
    e.printStackTrace();
  }
  
  return postId;
}
8

This can be helpful:

1) The algorithm to generate URL by yourself http://carrot.is/coding/instagram-ids

2) Also, Instagram has private API endpoint to generate URLs by media_id: https://i.instagram.com/api/v1/media/1212073297261212121_121212123111/permalink/ but it is protected with cookie sessionid

1
  • 3
    Although this may provide an answer to the question, links only answer are discouraged.
    – Aurasphere
    Commented Dec 1, 2016 at 14:05
5

Sharing the implementation in JavaScript, using big-integer package (https://www.npmjs.com/package/big-integer)

var bigInt = require('big-integer');

function getShortcodeFromTag(tag) {
  let id = bigInt(tag.split('_', 1)[0]);
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
  let remainder;
  let shortcode = '';

  while (id.greater(0)) {
    let division = id.divmod(64);
    id = division.quotient;
    shortcode = `${alphabet.charAt(division.remainder)}${shortcode}`;
  }

  return shortcode;
}
3

I found a solution for iOS objective-C:

  -(NSString *) getInstagramPostId:(NSString *)mediaId {
  NSString *postId = @"";
  @try {
    NSArray *myArray = [mediaId componentsSeparatedByString:@"_"];
    NSString *longValue = [NSString stringWithFormat:@"%@",myArray[0]];
    long itemId = [longValue longLongValue];
    NSString *alphabet = @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
    while (itemId > 0) {
      long remainder = (itemId % 64);
      itemId = (itemId - remainder) / 64;
      unsigned char charToUse = [alphabet characterAtIndex:(int)remainder];
      postId = [NSString stringWithFormat:@"%c%@",charToUse , postId];
    }
  } @catch(NSException *exception) {
    NSLog(@"%@",exception);
  }
  return postId;}
2

Based on great http://carrot.is/coding/instagram-ids article, here is the example ruby implementation on converting numeric to string ids:

def translate(post_id)
  dict = [?A..?Z, ?a..?z, 0..9].map(&:to_a).flatten
  dict += ['-', '_']

  post_id = post_id.split('_').first.to_i
  to_radix(post_id, 64).map { |d| dict[d] }.join
end

def to_radix(int, radix)
  int == 0 ? [] : (to_radix(int / radix, radix) + [int % radix])
end

Where you'd just call translate('1238578393243739028_1408429375') and get back BEwUHyDxGOU.

2

Swift 4.2 Solution:

func getInstagramPostId(_ mediaId: String?) -> String? {
  var postId = ""
  do {
    let myArray = mediaId?.components(separatedBy: "_")
    let longValue = "\(String(describing: myArray?[0]))"
    var itemId = Int(Int64(longValue) ?? 0)
    let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
    while itemId > 0 {
      let remainder: Int = itemId % 64
      itemId = (itemId - remainder) / 64
      
      let charToUse = alphabet[alphabet.index(alphabet.startIndex, offsetBy: Int(remainder))]
      postId = "\(charToUse)\(postId)"
    }
  }
  return postId
}

C# Solution:

public static string getInstagramPostId(string mediaId)
{
  string postId = "";
  try
  {
    long id = long.Parse(mediaId.Substring(0, mediaId.IndexOf('_')));
    string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
    while (id > 0)
    {
      long remainder = (id % 64);
      id = (id - remainder) / 64;
      int a = (int)remainder + int.Parse(postId);
      postId = "" + alphabet[a];
    }
  }
  catch (Exception e)
  {
    Console.Write(e.StackTrace);
  }
  return postId;
}
1

Just add one more field which is 'permalink'

List<String> mediaFields = [
  'id',
  'media_type',
  'media_url',
  'username',
  'permalink'
];
0
0

C# Solution (Tested)

public static string getInstagramPostId(string mediaId)
{
  string postId = "";
  try
  {
    long id = long.Parse(mediaId.Substring(0, mediaId.IndexOf('_')));
    string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
    while (id > 0)
    {
      long remainder = (id % 64);
      id = (id - remainder) / 64;
      
      postId = alphabet.ElementAt((int)remainder) + postId;
    }
  }
  catch (Exception e)
  {
    Console.Write(e.StackTrace);
  }
  return postId;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.