Created
August 16, 2022 13:39
-
-
Save M1n007/df3469b800b8835bfcaf150ae0af3cb8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {IgApiClient}= require('instagram-private-api'); | |
const {withFbns} = require('instagram_mqtt'); | |
const fs = require('fs'); | |
const IG_USERNAME = ''; | |
const IG_PASSWORD = ''; | |
(async () => { | |
const ig = withFbns(new IgApiClient()); | |
ig.state.generateDevice(IG_USERNAME); | |
// this will set the auth and the cookies for instagram | |
await readState(ig); | |
// this logs the client in | |
await loginToInstagram(ig); | |
// you received a notification | |
ig.fbns.on('push', async data => { | |
console.log(data) | |
}); | |
// the client received auth data | |
// the listener has to be added before connecting | |
ig.fbns.on('auth', async auth => { | |
// logs the auth | |
logEvent('auth')(auth); | |
//saves the auth | |
await saveState(ig); | |
}); | |
// 'error' is emitted whenever the client experiences a fatal error | |
ig.fbns.on('error', logEvent('error')); | |
// 'warning' is emitted whenever the client errors but the connection isn't affected | |
ig.fbns.on('warning', logEvent('warning')); | |
// this sends the connect packet to the server and starts the connection | |
// the promise will resolve once the client is fully connected (once /push/register/ is received) | |
await ig.fbns.connect(); | |
// you can pass in an object with socks proxy options to use this proxy | |
// await ig.fbns.connect({socksOptions: {host: '...', port: 12345, type: 4}}); | |
})(); | |
async function saveState(ig) { | |
return fs.writeFileSync('state.json', await ig.exportState(), { encoding: 'utf8' }); | |
} | |
async function readState(ig) { | |
if (!await fs.existsSync('state.json')) | |
return; | |
await ig.importState(await fs.readFileSync('state.json', {encoding: 'utf8'})); | |
} | |
async function loginToInstagram(ig) { | |
ig.request.end$.subscribe(() => saveState(ig)); | |
await ig.account.login(IG_USERNAME, IG_PASSWORD); | |
} | |
function logEvent(name) { | |
return (data) => console.log(name, data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment