forked from gippy/instagram-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add login option and exports cookies
- Loading branch information
Showing
9 changed files
with
181 additions
and
2 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module.exports.pleaseOpen = ` | ||
_____ _ | ||
| __ \\| | | ||
| |__) | | ___ __ _ ___ ___ ___ _ __ ___ _ __ | ||
| ___/| |/ _ \\/ _\` / __|/ _ \\ / _ \\| '_ \\ / _ \\ '_ \\ | ||
| | | | __/ (_| \\__ \\ __/ | (_) | |_) | __/ | | | | ||
|_| |_|\\___|\\__,_|___/\\___| \\___/| .__/ \\___|_| |_| | ||
| | | ||
|_| | ||
` | ||
|
||
module.exports.liveView = ` | ||
_ _ _ | ||
| (_) (_) | ||
| |___ _____ __ ___ _____ __ | ||
| | \\ \\ / / _ \\ \\ \\ / / |/ _ \\ \\ /\\ / / | ||
| | |\\ V / __/ \\ V /| | __/\\ V V / | ||
|_|_| \\_/ \\___| \\_/ |_|\\___| \\_/\\_/ | ||
` | ||
|
||
module.exports.localhost = ` | ||
_ _ _ _ ____ ___ ___ ___ | ||
| | | | | | | _|___ \\ / _ \\ / _ \\ / _ \\ | ||
| | ___ ___ __ _| | |__ ___ ___| |_(_) __) | | | | | | | | | | | ||
| |/ _ \\ / __/ _\` | | '_ \\ / _ \\/ __| __| |__ <| | | | | | | | | | | ||
| | (_) | (_| (_| | | | | | (_) \\__ \\ |_ _ ___) | |_| | |_| | |_| | | ||
|_|\\___/ \\___\\__,_|_|_| |_|\\___/|___/\\__(_)____/ \\___/ \\___/ \\___/ | ||
` |
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
const Apify = require('apify'); | ||
const { pleaseOpen, liveView, localhost } = require('./asci-texts.js'); | ||
const { authorize, close } = require('./submit-page.js'); | ||
const http = require('http'); | ||
|
||
/** | ||
* Attempts log user into instagram with provided username and password | ||
* @param {String} username Username to use during login (can also be an email or telephone) | ||
* @param {String} password Password to use during login | ||
* @param {Object} page Puppeteer Page object | ||
* @return Does not return anything | ||
*/ | ||
const login = async (username, password, page) => { | ||
await Apify.utils.log.info(`Attempting to log in`); | ||
|
||
try { | ||
await page.goto('https://www.instagram.com/accounts/login/?source=auth_switcher'); | ||
await page.waitForSelector('input[name="username"]'); | ||
await page.waitForSelector('input[name="password"]'); | ||
await page.waitForSelector('button[type="submit"]'); | ||
await Apify.utils.sleep(1000); | ||
|
||
await page.type('input[name="username"]', username, { delay: 150 }); | ||
await page.type('input[name="password"]', password, { delay: 180 }); | ||
await Apify.utils.sleep(1000); | ||
|
||
await page.click('button[type="submit"]'); | ||
|
||
await page.waitForNavigation(); | ||
await Apify.utils.sleep(1000); | ||
|
||
await page.waitForSelector('form button'); | ||
await page.click('form button'); | ||
|
||
// Wait fo code sent to email | ||
const port = Apify.isAtHome() ? process.env.APIFY_CONTAINER_PORT : 3000; | ||
const information = Apify.isAtHome() ? liveView : localhost; | ||
|
||
console.log(pleaseOpen); | ||
console.log(information); | ||
let code; | ||
|
||
const server = http.createServer((req, res) => { | ||
if (req.url.includes('/authorize')) { | ||
let data = ''; | ||
req.on('data', (body) => { | ||
if (body) data += body; | ||
}); | ||
req.on('end', () => { | ||
code = decodeURIComponent(data.replace('code=', '')); | ||
res.end(close()); | ||
}); | ||
} else { | ||
res.end(authorize()); | ||
} | ||
}); | ||
|
||
server.listen(port, () => console.log('server is listening on port', port)); | ||
|
||
const start = Date.now(); | ||
while (!code) { | ||
const now = Date.now(); | ||
if (now - start > 5 * 60 * 1000) { | ||
throw new Error('You did not provide the code in time!'); | ||
} | ||
console.log(`waiting for code...You have ${300 - Math.floor((now - start) / 1000)} seconds left`); | ||
await new Promise((resolve) => setTimeout(resolve, 10000)); | ||
} | ||
server.close(() => console.log('closing server')); | ||
|
||
await page.waitForSelector('input[name="security_code"]'); | ||
await page.waitForSelector('form button'); | ||
await page.type('input[name="security_code"]', code, { delay: 150 }); | ||
await Apify.utils.sleep(1000); | ||
await page.click('form button'); | ||
|
||
await page.waitForNavigation(); | ||
|
||
await Apify.utils.log.info(`Successfully logged in`); | ||
await Apify.utils.sleep(3000); | ||
} catch (error) { | ||
await Apify.utils.log.info('Failed to log in'); | ||
await Apify.utils.log.error(error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
module.exports = { | ||
login, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const pCss = `color: #9fa5a9; line-height: 20px!important; text-align: center; font-family: Graphik,sans-serif;` | ||
const inputCss = `padding-left: 40px; cursor: pointer; font-size: 13px;font-weight: 700;color: #fff;background-color: #5cb85c;border-color: #4cae4c;text-align: center;vertical-align: middle;touch-action: manipulation;padding: 10px 20px;border-radius: 3px;line-height: 1.42857; border: 1px solid transparent;` | ||
|
||
module.exports.authorize = () => | ||
`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Apify Instagram authorization</title> | ||
</head> | ||
<body> | ||
<p style="${pCss} margin-top: 200px;"> Please fill in code you received to email connected to provided username.</p> | ||
<form method="POST" action=/authorize style="text-align: center; margin-top: 50px"> | ||
<input placeholder= "your code" name="code" id="code" style="font-size: 14px;font-family: monospace,serif;color: #11181c; border-radius: 3px; border: 1px solid #ccc; padding: 10px 15px; width: 400px; height: 20px; margin: auto"/> | ||
<input type="submit" style="${inputCss}"/> | ||
</form> | ||
</body> | ||
</html>` | ||
|
||
module.exports.close = () => | ||
`<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Apify Instagram authorization</title> | ||
</head> | ||
<body> | ||
<p style="${pCss} margin-top: 200px;"> You are now authorized, your actor should finish in seconds. </p> | ||
</body> | ||
</html>` |