|
|
@@ -1,7 +1,7 @@
|
|
|
'use strict'
|
|
|
const { chromium, firefox, webkit } = require('playwright')
|
|
|
const axios = require('axios').default
|
|
|
-const fastq = require('fastq')
|
|
|
+const Queue = require('queue')
|
|
|
|
|
|
module.exports = async function (fastify, opts) {
|
|
|
fastify.post('/search', async function (request, reply) {
|
|
|
@@ -48,48 +48,43 @@ module.exports = async function (fastify, opts) {
|
|
|
//find a tag using page.locator
|
|
|
const items = await page.locator('.p13n-desktop-grid .a-column').all()
|
|
|
console.log(items.length)
|
|
|
- const res = []
|
|
|
+ let res = []
|
|
|
|
|
|
- const worker = async item => {
|
|
|
- let atags = await item.locator('a').all()
|
|
|
- if (atags && atags[1]) {
|
|
|
- let text = await atags[1].innerText()
|
|
|
- let href = await atags[1].getAttribute('href')
|
|
|
- let match = /\/dp\/(.*?)\//.exec(href)
|
|
|
- let asin
|
|
|
- if (match && match[1]) {
|
|
|
- asin = match[1]
|
|
|
- }
|
|
|
-
|
|
|
- let rows = await item.locator('.a-row').all()
|
|
|
- let price = null
|
|
|
- if (rows.length > 1) {
|
|
|
- price = await rows[1].innerText()
|
|
|
- }
|
|
|
+ const queue = new Queue({ results: [] })
|
|
|
+ items.forEach(item => {
|
|
|
+ queue.push(async () => {
|
|
|
+ let atags = await item.locator('a').all()
|
|
|
+ if (atags && atags[1]) {
|
|
|
+ let text = await atags[1].innerText()
|
|
|
+ let href = await atags[1].getAttribute('href')
|
|
|
+ let match = /\/dp\/(.*?)\//.exec(href)
|
|
|
+ let asin
|
|
|
+ if (match && match[1]) {
|
|
|
+ asin = match[1]
|
|
|
+ }
|
|
|
|
|
|
- let sif = await axios.get('https://www.sif.com/api/search/asinFlowOverview', {
|
|
|
- params: {
|
|
|
- country: 'US',
|
|
|
- asin,
|
|
|
- _t: new Date().getTime()
|
|
|
+ let rows = await item.locator('.a-row').all()
|
|
|
+ let price = null
|
|
|
+ if (rows.length > 1) {
|
|
|
+ price = await rows[1].innerText()
|
|
|
}
|
|
|
- })
|
|
|
- return { text, href, price, asin, data: sif.data.data }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
|
|
|
- const queue = fastq(worker, 5)
|
|
|
- items.forEach(item => {
|
|
|
- queue.push(item, (err, r) => {
|
|
|
- if (err) {
|
|
|
- console.log(err)
|
|
|
- } else {
|
|
|
- res.push(r)
|
|
|
+ let sif = await axios.get('https://www.sif.com/api/search/asinFlowOverview', {
|
|
|
+ params: {
|
|
|
+ country: 'US',
|
|
|
+ asin,
|
|
|
+ _t: new Date().getTime()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ return { text, href, price, asin, data: sif.data.data }
|
|
|
}
|
|
|
})
|
|
|
})
|
|
|
- await queue.drained()
|
|
|
+ res = await new Promise((resolve, reject) => {
|
|
|
+ queue.start(() => {
|
|
|
+ resolve(queue.results)
|
|
|
+ })
|
|
|
+ })
|
|
|
await browser.close()
|
|
|
return res
|
|
|
|