instagram-private-api icon indicating copy to clipboard operation
instagram-private-api copied to clipboard

How to take user posts feeds beginning from specific page, or from the end?

Open rihannarickeminem opened this issue 3 years ago • 3 comments

 const userFeed = ig.feed.user(loggedInUser.pk);
  const myPostsFirstPage = await userFeed.items();

seems that now it is possible only to take users feed only from latest posts, but how to get posts in other order, beginning from the first post posted? Or from specific page? Is there a way to get users posts count?

rihannarickeminem avatar Oct 05 '21 13:10 rihannarickeminem

Instagram uses pagination. As a result, to go from beginning to end, you must pull all of the records then traverse them backwards. i.e. ->grab all user posts ->work backwards from the end of your array

keharv avatar Oct 07 '21 05:10 keharv

Example for getting a user's post count

//requires for instagram
/* tslint:disable:no-console */
var {IgApiClient} =  require('instagram-private-api');
var { get } = require('request-promise'); // request is already declared as a dependency of the library

const fs = require('fs');
bot = async () => {
    //instagram credentials
    const igUsername = "";
    const igPassword = "";
    //instagram session handling
    function saveSession(data) {
        fs.writeFileSync('./igSession.dat', JSON.stringify(data));
        return data;
    }
    
    function checkSession() {
        return fs.existsSync('./igSession.dat');
    }

    function loadSession() {
        return JSON.parse(fs.readFileSync('./igSession.dat').toString());
    }
    const ig = new IgApiClient();
    ig.state.generateDevice(igUsername);
    ig.request.end$.subscribe(async () => {
        const serialized = await ig.state.serialize();
        delete serialized.constants; // this deletes the version info, so you'll always use the version provided by the library
        saveSession(serialized);
    });
    if (checkSession()) {
        // import state accepts both a string as well as an object
        // the string should be a JSON object
        await ig.state.deserialize(loadSession());
    }
    const auth = await ig.account.login(igUsername, igPassword);

    const userID = await ig.user.getIdByUsername('google');
    const userInfo = await ig.user.info(userID);
    console.log(userInfo.media_count);
}
bot();

keharv avatar Oct 07 '21 21:10 keharv

I tried to do pagination but pagination doesn't work either.

const targetUser = await ig.user.searchExact(igHandle);
const userFeed = await ig.feed.user(targetUser.pk);

const myPostsFirstPage = await userFeed.items();
const myPostsSecondPage = await userFeed.items();

const feedTray =  {
            firstPage: myPostsFirstPage,
            secondPage: myPostsSecondPage
        }

Both the myPostFirstPage and myPostSecondPage show the same value.

mayurc137 avatar Mar 05 '22 05:03 mayurc137