This repository has been archived by the owner on Feb 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.js
156 lines (139 loc) · 3.86 KB
/
api.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const Async = require('crocks/Async');
const Maybe = require('crocks/Maybe');
const pipeK = require('crocks/helpers/pipeK');
const puppeteer = require('puppeteer');
const curry = require('ramda/src/curry');
const compose = require('ramda/src/compose');
const Persist = require('./persist').Persist;
const defaultUserAgent =
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36';
const { Nothing } = Maybe;
const I = a => a;
const { restore, persist } = Persist;
// identityToAsync :: a → Async a
const identityToAsync = compose(
Async.of,
I
);
// foldMaybe :: Maybe Async a → Async a
const foldMaybe = m => m.option(identityToAsync);
function api() {
const createBrowser = () =>
Async((rej, res) =>
puppeteer
.launch({
headless: false,
args: ['--no-sandbox']
})
.then(browser => persist('browser', browser))
.then(res)
.catch(rej)
);
// getBrowser :: () → Async Browser
const getBrowser = () => (restore('browser') ? Async.Resolved(restore('browser')) : createBrowser);
// closeBrowser :: Async Browser → Async ()
const closeBrowser = () =>
getBrowser().chain(browser =>
Async((rej, res) =>
browser
.close()
.then(res)
.catch(rej)
)
);
// getNewPage :: Async Browser → Async Page
const getNewPage = browser =>
Async((rej, res) =>
browser
.newPage()
.then(res)
.catch(rej)
);
// goto :: String → Async Page → Async Page
const goto = url => page =>
Async((rej, res) =>
page
.goto(url, {
timeout: 25000,
waitUntil: ['domcontentloaded']
})
.then(res.bind(null, page))
.catch(message => rej(message))
);
// enableJs :: Async Page → Async Page
const enableJs = page => Async((_, res) => page.setJavaScriptEnabled(true).then(res(page)));
// setUserAgent :: Async Page → Async Page
const setUserAgent = agent => page => Async((_, res) => page.setUserAgent(agent).then(res(page)));
// getContent :: Async Page → Async String
const getContent = page =>
Async((rej, res) =>
typeof page === 'object'
? page
.content()
.then(res)
.catch(res)
: rej('No page object available')
);
// failOnCaptcha :: Async Page → Async Page | Async Error
const failOnCaptcha = page =>
Async((rej, res) =>
page
.waitForSelector('.g-recaptcha', {
timeout: 1500
})
.then(rej.bind(null, "Error: Sorry....re-captcha! You'll be luckier next time"))
.catch(res.bind(null, page))
);
// getText :: Async Page → Async String | Async Error
const getText = page =>
Async((rej, res) =>
typeof page !== 'string'
? page
.evaluate(() => document.body.innerText)
.then(res)
.catch(rej)
: rej(page)
);
// getPage :: ( Url → String ) → Async Page
getPage = curry((url, ua = defaultUserAgent) =>
pipeK(
createBrowser,
getNewPage,
enableJs,
setUserAgent(ua),
goto(url),
// failOnCaptcha
)
);
// getHtml :: ( String → Maybe Async → String ) → Async String
const getHtml = curry((url, failCondition = Nothing(), ua = defaultUserAgent) =>
pipeK(
createBrowser,
getNewPage,
enableJs,
setUserAgent(ua),
goto(url),
// failOnCaptcha,
foldMaybe(failCondition),
getContent
)
);
// getPublicIp :: () → Async String
const getPublicIp = pipeK(
createBrowser,
getNewPage,
enableJs,
setUserAgent(defaultUserAgent),
goto('https://api.ipify.org/'),
getText
);
return {
closeBrowser,
// failOnCaptcha,
getPage,
getHtml,
getPublicIp,
getNewPage
};
}
exports.init = api;