sdfdsf
This commit is contained in:
14
index.js
14
index.js
@@ -63,22 +63,12 @@ process.on('SIGTERM', exitHandler.bind(null, { cleanup: true }));
|
||||
* request handler
|
||||
*/
|
||||
app.post('/', async (req, res, next) => {
|
||||
const requestPage = () => browser.get(req.body.uri, req.body.waitElement).then(html => res.send(html)).catch(e => {
|
||||
if(e instanceof NoSuchSessionError) {
|
||||
browser.reset()
|
||||
browser.get(req.body).then(html => res.send(html)).catch(e => {
|
||||
if(e instanceof NoSuchSessionError) {
|
||||
logger.error({ reason: e.message, url: req.url }, 'Browser session expired')
|
||||
}else {
|
||||
logger.error({ reason: e.message, url: req.url }, 'Browser error occured')
|
||||
}
|
||||
res.status(500).send({ code: 500, message: e.message })
|
||||
// browser.close().then(() => e instanceof puppeteerErrors.TimeoutError ? setTimeout(() => requestPage(), 3000) : res.status(500).send(e.message))
|
||||
})
|
||||
requestPage()
|
||||
})
|
||||
|
||||
app.get('/restart', (req, res, next) => {
|
||||
// browser.close().then(() => {
|
||||
// res.json({message: 'Browser closed and lanuch with new request'})
|
||||
// })
|
||||
process.exit()
|
||||
})
|
||||
|
||||
@@ -6,57 +6,47 @@ const logger = require("logops")
|
||||
, { Options } = require('selenium-webdriver/chrome');
|
||||
|
||||
class SeleniumBrowser {
|
||||
constructor() {
|
||||
this.driver = null
|
||||
constructor() {
|
||||
this.config = { useHub: true, browser: 'chrome' }
|
||||
this.capabilities = Capabilities.chrome()
|
||||
this.sleep = m => new Promise(r => setTimeout(r, m))
|
||||
this.isRecaptchaReseponseEmpty = false
|
||||
this.isRecaptchaReseponseEmpty = false
|
||||
}
|
||||
|
||||
async setDriver() {
|
||||
if (this.config.useHub) {
|
||||
let opts = new Options;
|
||||
opts.addArguments(this.getChromeArgs())
|
||||
this.driver = await new Builder()
|
||||
.usingServer('http://spiduler-chrome:4444/wd/hub')
|
||||
.withCapabilities(this.capabilities)
|
||||
.forBrowser(this.config.browser)
|
||||
.setChromeOptions(opts)
|
||||
.build();
|
||||
} else {
|
||||
this.driver = await this.setLocalDriver()
|
||||
}
|
||||
}
|
||||
|
||||
setLocalDriver() {
|
||||
let opts = new Options
|
||||
getDriver() {
|
||||
let opts = new Options;
|
||||
opts.addArguments(this.getChromeArgs())
|
||||
return new Builder().forBrowser(this.config.browser).setChromeOptions(opts).build();
|
||||
return new Builder()
|
||||
.usingServer('http://spiduler-chrome:4444/wd/hub')
|
||||
.withCapabilities(this.capabilities)
|
||||
.forBrowser(this.config.browser)
|
||||
.setChromeOptions(opts)
|
||||
.build();
|
||||
}
|
||||
|
||||
request(uri, waitElement) {
|
||||
request(params) {
|
||||
let uri = params.uri
|
||||
let waitElement = params.waitElement
|
||||
if (this.isRecaptchaReseponseEmpty) {
|
||||
return ''
|
||||
} else {
|
||||
logger.info('URL %s', uri)
|
||||
if (uri.indexOf('://') > 3) {
|
||||
return this.driver.get(uri)
|
||||
// .then(() => {
|
||||
// if (waitElement) {
|
||||
// return this.driver.wait(until.elementIsVisible(this.driver.findElement(By.css(waitElement))), 5 * 1000)
|
||||
// }
|
||||
// return true
|
||||
// })
|
||||
.then(() => this.driver.findElement(By.css('body')))
|
||||
.then(body => body.getAttribute('innerHTML'))
|
||||
.then(html => this.getHtml(html))
|
||||
// .catch(error => {
|
||||
// logger.error({ message: error.message }, 'No image element')
|
||||
// return this.driver.findElement(By.css('body'))
|
||||
// .then(body => body.getAttribute('innerHTML'))
|
||||
// .then(html => this.getHtml(html))
|
||||
// })
|
||||
return this.getDriver().then(driver => {
|
||||
return driver.get(uri)
|
||||
.then(() => {
|
||||
if (waitElement) {
|
||||
return driver.wait(until.elementIsVisible(driver.findElement(By.css(waitElement))), 5 * 1000)
|
||||
}
|
||||
return true
|
||||
})
|
||||
.then(() => driver.findElement(By.css('body')))
|
||||
.then(body => body.getAttribute('innerHTML'))
|
||||
.then(html => this.getHtml(html))
|
||||
.finally(() => {
|
||||
driver.quit()
|
||||
})
|
||||
})
|
||||
} else {
|
||||
logger.info('URL %s is not valid', uri)
|
||||
return ''
|
||||
@@ -95,17 +85,11 @@ class SeleniumBrowser {
|
||||
return res
|
||||
}).then(res => {
|
||||
logger.debug(res, '2captcha solved')
|
||||
// this.driver.findElement(By.css('#g-recaptcha-response')).setAttribute('innerHTML', res.request)
|
||||
this.driver.executeScript('captchaCallback("' + res.request + '")')
|
||||
this.driver.wait(10000).then(d => {
|
||||
this.driver.findElement(By.css('#g-recaptcha-response')).then(function (webElement) {
|
||||
logger.error(res, 'Failed to overcome recaptcha')
|
||||
}, function (err) {
|
||||
// if (err.state && err.state === 'no such element') {
|
||||
// console.log('Element not found');
|
||||
// } else {
|
||||
// webdriver.promise.rejected(err);
|
||||
// }
|
||||
logger.info('Success to overcome recaptcha')
|
||||
})
|
||||
})
|
||||
@@ -131,7 +115,9 @@ class SeleniumBrowser {
|
||||
}
|
||||
|
||||
async close() {
|
||||
|
||||
this.pool.drain().then(function () {
|
||||
this.pool.clear();
|
||||
});
|
||||
}
|
||||
|
||||
getChromeArgs() {
|
||||
@@ -152,5 +138,4 @@ class SeleniumBrowser {
|
||||
}
|
||||
|
||||
const browser = new SeleniumBrowser
|
||||
browser.setDriver()
|
||||
module.exports = browser
|
||||
@@ -8,17 +8,14 @@ class HeadlessService {
|
||||
|
||||
}
|
||||
|
||||
get(uri, waitElement) {
|
||||
return browser.request(uri, waitElement)
|
||||
get(params) {
|
||||
return browser.request(params)
|
||||
}
|
||||
|
||||
close() {
|
||||
return browser.close()
|
||||
}
|
||||
|
||||
reset() {
|
||||
browser.setDriver()
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new HeadlessService
|
||||
Reference in New Issue
Block a user