This commit is contained in:
spiduler
2020-11-05 17:02:35 +09:00
parent 294ca78b71
commit afd0756715
2 changed files with 42 additions and 21 deletions

View File

@@ -2,6 +2,7 @@ const logger = require("logops")
, { By } = require('selenium-webdriver') , { By } = require('selenium-webdriver')
, url = require('url') , url = require('url')
, fetch = require('node-fetch') , fetch = require('node-fetch')
const { request } = require("./SeleniumBrowser")
class CaptchaComp { class CaptchaComp {
@@ -41,21 +42,30 @@ class CaptchaComp {
o.search = new url.URLSearchParams(params).toString(); o.search = new url.URLSearchParams(params).toString();
return o return o
} }
let params = Object.assign(this.params.in, { googlekey: params.googlekey, pageurl: params.pageurl }) params = Object.assign(this.params.in, { googlekey: params.googlekey, pageurl: params.pageurl })
let o = new url.URL(this.api.in) let o = new url.URL(this.api.in)
o.search = new url.URLSearchParams(params).toString(); o.search = new url.URLSearchParams(params).toString();
return o return o
} }
resolveCaptcha(driver) { getParams(params) {
return Object.assign({
method: 'GET',
headers: { "Content-Type": "application/json" },
timeout: this.api.retryTimeout
}, params)
}
request(params) {
return fetch(this.getUrl(params), this.getParams()).then(res => res.json())
}
requestResolve(driver) {
return driver.findElement(By.css('#grecaptcha iframe')).getAttribute('src') return driver.findElement(By.css('#grecaptcha iframe')).getAttribute('src')
.then(src => url.parse(src, true).query.k) .then(src => url.parse(src, true).query.k)
.then(googlekey => { .then(googlekey => {
return driver.getCurrentUrl().then(pageurl => { return driver.getCurrentUrl().then(pageurl => {
return fetch(this.getUrl({ googlekey: googlekey, pageurl: pageurl }), { return this.request({ googlekey: googlekey, pageurl: pageurl }).then(res => {
method: 'GET',
headers: { "Content-Type": "application/json" }
}).then(res => res.json()).then(res => {
logger.debug(res, '2captcha IN-request response') logger.debug(res, '2captcha IN-request response')
if (!res.hasOwnProperty('request')) { if (!res.hasOwnProperty('request')) {
throw new Error('2captcha IN-request response has no data request id : ' + JSON.stringify(res)) throw new Error('2captcha IN-request response has no data request id : ' + JSON.stringify(res))
@@ -63,21 +73,28 @@ class CaptchaComp {
return res.request return res.request
}) })
}) })
}).then(id => {
logger.info({ id: this.api.id }, 'Request answer delay for 20 senconds')
return new Promise(r => setTimeout(() => r(id), 20000))
}) })
.then(async id => { }
logger.info('Request 2captcha solving result after 20 senconds')
await this.sleep(1000 * 20) async getAnswer(res) {
let res = await fetch(this.getUrl({ id: id })).then(res => res.json()) let retryLimit = 10
let maxRetry = 10 while (res.request == 'CAPCHA_NOT_READY') {
while (res.request == 'CAPCHA_NOT_READY') { await this.sleep(this.api.retryTimeout)
await this.sleep(this.api.retryTimeout) retryLimit = retryLimit - 1
maxRetry = maxRetry - 1 logger.info('Retry get recap answer in 5 senconds')
logger.info('2captcha still working so request solving result after 5 senconds') res = await this.request({ id: id })
res = await fetch(this.getUrl({ id: id })).then(res => res.json()) if (retryLimit == 0) break
if (maxRetry == 0) break }
} return res
return res }
})
resolveCaptcha(driver) {
return this.requestResolve(driver)
.then(id => this.request({ id: id }))
.then(res => this.getAnswer(res))
.then(res => { .then(res => {
return driver.executeScript('captchaCallback("' + res.request + '")').then(() => this.sleep(1000 * 5)) return driver.executeScript('captchaCallback("' + res.request + '")').then(() => this.sleep(1000 * 5))
}) })

View File

@@ -21,7 +21,7 @@ class SeleniumBrowser {
.then(body => body.getAttribute('innerHTML')) .then(body => body.getAttribute('innerHTML'))
let htmlPromise = captcha.isCaptchaPage(html) ? captcha.resolveCaptcha(driver) : this.getHtml(driver, html, params) let htmlPromise = captcha.isCaptchaPage(html) ? captcha.resolveCaptcha(driver) : this.getHtml(driver, html, params)
html = await htmlPromise html = await htmlPromise
this.pool.release(driver) if (this.pool.isBorrowedResource(driver)) this.pool.release(driver)
return html return html
}) })
} else { } else {
@@ -38,6 +38,10 @@ class SeleniumBrowser {
.then(html => { .then(html => {
return html return html
}) })
.catch(err => {
logger.error({ message: err.message }, 'Get html failed')
this.pool.release(driver)
})
} else { } else {
return html return html
} }