Find Instagram media url by ID

懵懂的女人 提交于 2020-06-24 07:37:17

问题


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 !


回答1:


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




回答2:


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;
}



回答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;}



回答4:


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;
}



回答5:


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;
       }



回答6:


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.




回答7:


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;
        }


来源:https://stackoverflow.com/questions/37609420/find-instagram-media-url-by-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!