dilame / instagram-private-api

NodeJS Instagram private API SDK. Written in TypeScript.
MIT License
5.99k stars 1.14k forks source link

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

Open rihannarickeminem opened 3 years ago

rihannarickeminem commented 3 years ago
 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?

keharv commented 3 years ago

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 commented 3 years ago

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();
mayurc137 commented 2 years ago

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.