从媒体ID获取instagram帖子网址 [英] Getting instagram post url from media id

查看:124
本文介绍了从媒体ID获取instagram帖子网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我手上有 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.

例如,如果您手上有一个Facebook帖子ID(xxxx_yyyy),则可以从中创建以下网址( http ://facebook.com/ xxxx /posts/ yyyy ),然后直接访问原始帖子.

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.

有没有办法在Instagram上做到这一点?有了media_id(和user_id),是否可以创建一个帖子网址?

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?

推荐答案

我必须实现客户端javascript来解决此问题,Seano的回答非常宝贵,我很高兴他们提到使用BigInteger库,但是我想提供一个事实证明,使用BigInteger库进行完整的实现是非常必要的.

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.

我从 https://www.npmjs.com/package/big-下载了BigInteger库整数.

这是最适合我的功能.

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 + '/';
}

我只想指出,在分配重新计算的media_id时,toString()的用法非常重要,该值仍然是一个字符串以确保使用完整的数字(在我的情况下,media_id为19个字符长) . BigInteger文档还说明了这一点……

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...

请注意,大于9007199254740992且小于-9007199254740992的Javascript数字不能精确表示,因此不会产生确切的结果.如果您要处理的数字超出此范围,则最好传入字符串.

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.

干杯!

这篇关于从媒体ID获取instagram帖子网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆