由于HTTP呼叫不是很正式,我找到了Instagram帖子的媒体ID,图片URL和用户名。
但我需要Instagram上每个帖子的URL,我不知道如何找到它。
是否有像instagram.com/something/{mediaID}
这样的网址重定向到instagram.com/p/{mediaSlug}
,或者是通过媒体ID找到slug的其他方法(当然不使用官方API!)?
例如,我有:
唯一编号:1238578393243739028_1408429375
媒体ID:1238578393243739028
用户ID:1408429375
我愿意:
谢谢你的帮助 !
这可能会有所帮助:
1)自己生成URL的算法http://carrot.is/coding/instagram-ids
2)此外,Instagram有私有API端点来生成media_id:https://i.instagram.com/api/v1/media/1212073297261212121_121212123111/permalink/的URL,但它受cookie sessionid保护
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;
}
我找到了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;}
使用大整数包(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;
}
基于很棒的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
。
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;
}