Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Instagram media url by ID

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 !

like image 895
Pierre Avatar asked Jun 03 '16 08:06

Pierre


People also ask

What is media on Instagram?

Represents an Instagram album, photo, or video (uploaded video, live video, video created with the Instagram TV app, reel, or story).

Does Instagram have an open API?

The Instagram Basic Display API allows users of your app to get basic profile information, photos, and videos in their Instagram accounts. The API can be used to access any type of Instagram account but only provides read-access to basic data.


2 Answers

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;
}
like image 124
JhonnyTawk Avatar answered Sep 22 '22 13:09

JhonnyTawk


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

like image 24
Alex Torson Avatar answered Sep 21 '22 13:09

Alex Torson