按ID查找Instagram媒体网址

时间:2016-06-03 08:21:10

标签: instagram instagram-api

由于HTTP呼叫不是很正式,我找到了媒体ID,图片网址和Instagram帖子的用户名。

但是我需要Instagram上每个帖子的网址,而且我不知道如何找到它。

是否有像instagram.com/something/{mediaID}这样的网址重定向到instagram.com/p/{mediaSlug},或者有其他方法可以按媒体ID查找slug(当然不使用官方API!)

例如,我有:

  

唯一号码:1238578393243739028_1408429375

     

媒体编号:1238578393243739028

     

用户ID:1408429375

我愿意:

  

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

感谢您的帮助!

7 个答案:

答案 0 :(得分:5)

这可能会有所帮助:

1)自己生成URL的算法 http://carrot.is/coding/instagram-ids

2)此外,Instagram还有私有API端点,可通过media_id生成网址: https://i.instagram.com/api/v1/media/1212073297261212121_121212123111/permalink/ 但它受cookie sessionid保护

答案 1 :(得分:4)

Java解决方案:

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

答案 2 :(得分:2)

我找到了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;}

答案 3 :(得分:2)

使用大整数包(https://www.npmjs.com/package/big-integer

在JavaScript中共享实现
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;
}

答案 4 :(得分:1)

基于精彩的http://carrot.is/coding/instagram-ids文章,以下是将数字转换为字符串ID的示例ruby实现:

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

您只需致电translate('1238578393243739028_1408429375')并返回BEwUHyDxGOU

答案 5 :(得分:1)

Swift 4.2解决方案:

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#解决方案:

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 :(得分:0)

C#解决方案(已测试)

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