Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.js #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 34 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Native
const fs = require('fs')
const crypto = require('crypto')
const readline = require('readline');

// Packages
const request = require('request-promise-native')
Expand All @@ -10,6 +11,18 @@ const useragentFromSeed = require('useragent-from-seed')

const baseUrl = 'https://www.instagram.com'

function askQuestion(query) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});

return new Promise(resolve => rl.question(query, ans => {
rl.close();
resolve(ans);
}))
}

class Instagram {
constructor(
{ username, password, cookieStore },
Expand Down Expand Up @@ -68,10 +81,30 @@ class Instagram {
}

// Login
const res = await this.request.post('/accounts/login/ajax/', {
try {
var res = await this.request.post('/accounts/login/ajax/', {
resolveWithFullResponse: true,
form: { username, enc_password: createEncPassword(password) }
})
}
catch (err){
if (err.error.two_factor_required)
{
console.log("Two factor auth is required!");
let ans = await askQuestion("Code:");
res = await this.request.post('/accounts/login/ajax/', {
resolveWithFullResponse: true,
form: {
username: username,
verificationCode: ans,
identifier: err.error.two_factor_info.two_factor_identifier
}
})
}
else{
throw err;
}
}

if (!res.headers['set-cookie']) {
throw new Error('No cookie')
Expand Down