-
Notifications
You must be signed in to change notification settings - Fork 9
/
puppeteer.js
128 lines (115 loc) · 4.09 KB
/
puppeteer.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const puppeteer = require('puppeteer');
var app = require('express')();
const insEmail = require('./config.js')[app.get('env')].insEmail;
const insPass = require('./config.js')[app.get('env')].insPass;
const insCookies = require('./config.js')[app.get('env')].insCookies;
const usernameSelector = 'input[name="username"]';
const passwordSelector = 'input[name="password"]';
const loginBtn = 'button[type="submit"]';
const storiesCountClassSelector = '#react-root > section > div > div > section > div > div:nth-child(1)';
const nextStorySelector = '.coreSpriteRightChevron';
const userAgent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3239.108 Safari/537.36';
let browserWSEndpoint = null;
async function getStories(url) {
try {
let storiesUrl = '';
let baseUrl = 'https://www.instagram.com/';
let targetHomeUrl = '';
let imgUrls = [];
let username = '';
if(url.indexOf('/login/') === -1) {
username = url.slice(url.lastIndexOf('.com/')+5);
url = `https://www.instagram.com/accounts/login/?next=%2F${username}%2F`;
storiesUrl = `https://www.instagram.com/stories/${username}`;
targetHomeUrl = `https://www.instagram.com/${username}/`;
}
if (!browserWSEndpoint) {
const browser = await puppeteer.launch({
//headless: false,
args: [
// '--proxy-server="direct://"',
// '--proxy-bypass-list=*',
'--no-sandbox',
'--disable-setuid-sandbox'
]
});
browserWSEndpoint = await browser.wsEndpoint();
}
const browser = await puppeteer.connect({ browserWSEndpoint });
// const browser = await puppeteer.launch({
// //headless: true,
// headless: false,
// args: [
// // '--proxy-server="direct://"',
// // '--proxy-bypass-list=*',
// '--no-sandbox',
// '--disable-setuid-sandbox'
// ]
// });
const cookie = {
name: "sessionid",
value: insCookies,
path: "/",
domain: ".instagram.com",
};
const page = await browser.newPage();
await page.setCookie(cookie);
await page.setUserAgent(userAgent);
await page.goto(url, { waitUntil: 'networkidle0' });
if (await page.$(usernameSelector)){
// login
await page.click(usernameSelector);
await page.keyboard.type(insEmail);
await page.click(passwordSelector);
await page.keyboard.type(insPass);
await page.click(loginBtn).catch(e => e);
// // get image urls
await page.waitForNavigation();
currentPage = await page.url();
if (currentPage.search(/\/challenge\//) !== -1) {
await page.close();
return new Promise(function (resolve, reject) {
imgUrls.push(`請重新驗證帳號喔QQ`);
resolve(imgUrls);
});
}
}
await page.goto(storiesUrl, { waitUntil: 'load' });
if (await page.url() === targetHomeUrl) {
await page.close();
return new Promise(function (resolve, reject) {
imgUrls.push(`${username} 是私人帳號喔QQ`);
resolve(imgUrls);
});
}
await page.waitForSelector('img[decoding="sync"]');
let countClass = await page.$eval(storiesCountClassSelector, e => e.getAttribute('class'));
let count = await page.$$eval(`.${countClass}`, e => e.length);
for (let index = 0; index < count; index++) {
let img = '';
let video = '';
img = await page.$eval('img[decoding="sync"]', e => e.getAttribute('src')).catch(e => e);
video = await page.$eval('video[preload="auto"] > source', e => e.getAttribute('src')).catch(e => e);
if (typeof video === 'string') {
imgUrls.push(video);
} else {
imgUrls.push(img);
}
await page.click(nextStorySelector);
if(await page.url() === baseUrl) {
break;
}
await page.waitForSelector('img[decoding="sync"]');
}
//await browser.close();
await page.close();
return new Promise(function (resolve, reject) {
resolve(imgUrls);
});
} catch (error) {
console.log(error);
}
}
module.exports = {
getStories: getStories
};