-
Notifications
You must be signed in to change notification settings - Fork 1
/
instagram.js
89 lines (81 loc) · 2.44 KB
/
instagram.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const puppeteer = require('puppeteer');
const request = require('request');
require('dotenv').config()
const url = 'https://www.instagram.com/accounts/login/?source=auth_switcher'
const profile = (handle)=>{
return `https://www.instagram.com/${handle}/`
}
let instagram = {
browser: null,
page: null,
initialize: async ()=>{
instagram.browser = await puppeteer.launch({headless: false, args: ['--no-sandbox', '--disable-setuid-sandbox']});
instagram.page = await instagram.browser.newPage();
},
login: async ()=>{
await instagram.page.goto(url, {waitUntil: 'networkidle2'});
await instagram.page.waitFor("input[name='username']");
// Login to the account
await instagram.page.type("input[name='username']", process.env.USERNAME, {delay: 100});
await instagram.page.type("input[name='password']", process.env.PASSWORD, {delay: 100});
await instagram.page.click("button[type='submit']");
await instagram.page.waitFor(3000);
},
scrapePublicProfile: async (handle, limit)=>{
profilePage = profile(handle);
// console.log(profilePage);
let result = [];
let count = 0;
let len_over_time = [];
await instagram.page.goto(profilePage, {waitUntil: 'networkidle2'});
await instagram.page.waitFor('a>div>div.KL4Bh', {waitFor: 2000})
let flag = true;
while(count<limit && flag){
const nodes = await instagram.page.$$("a>div>div.KL4Bh")
if(nodes.length==0){
return [];
}
for(let i=0;i<nodes.length;i++){
res = await instagram.page.evaluate(el => el.firstChild.src, nodes[i]);
if(!result.includes(res)){
result.push(res);
console.log(res);
count+=1;
}
result = result.slice(-100);
}
len_over_time.push(result.length);
if(len_over_time.length>50){
if(new Set(len_over_time.slice(-50)).size === 1){
return result;
// await instagram.login();
// flag = false;
}
}
await instagram.page.evaluate(_ => {
window.scrollBy(0, window.innerHeight);
});
await instagram.page.waitFor(3000);
}
return result;
},
// use this function only when the flask server is up and running on your local machine
download: async (urls, handle)=>{
await request({
uri: "http://localhost:5000/downloadImages",
method: "POST",
body: {
nprocs: 8,
urls: urls,
handle: handle
},
json: true,
}, function(error, response, body) {
console.log("Success");
});
},
close: async ()=>{
await instagram.browser.close();
},
}
module.exports = instagram;