-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.ts
61 lines (48 loc) · 1.33 KB
/
index.ts
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
import * as path from 'path';
import * as puppeteer from 'puppeteer';
import * as api from './api';
import { GetPage, CloseBrowser, CreateBrowser } from 'src/common/interfaces';
const getUrl = (endpoint: string): string => `https://www.instagram.com${endpoint}`;
const createBrowser: CreateBrowser = async () => {
let browser = await puppeteer.launch({
headless: true,
args: ['--lang=en-US,en'],
});
const getPage: GetPage = async function getPage(endpoint, fn) {
let page: puppeteer.Page;
let result;
try {
const url = getUrl(endpoint);
console.log(url);
page = await browser.newPage();
await page.goto(url, { waitUntil: 'load' });
page.on('console', msg => {
const leng = msg.args().length;
for (let i = 0; i < leng; i += 1) {
console.log(`${i}: ${msg.args()[i]}`);
}
});
await page.addScriptTag({
path: path.join(__dirname, '../../../src/common/scraper/scraper.js'),
});
result = await fn(page);
await page.close();
} catch (e) {
if (page) {
await page.close();
}
throw e;
}
return result;
};
const close: CloseBrowser = async function close() {
await browser.close();
browser = null;
};
return {
getPage,
close,
...api,
};
};
export { createBrowser };