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

查看:39
本文介绍了从媒体 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)在我手中,是否可以创建单个帖子 url?

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 库整数.

这是对我来说效果很好的函数.

Here is the function which works well for me.

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天全站免登陆