|
|
@@ -1,6 +1,7 @@
|
|
|
'use strict'
|
|
|
const { chromium, firefox, webkit } = require('playwright')
|
|
|
const axios = require('axios').default
|
|
|
+const fastq = require('fastq')
|
|
|
|
|
|
module.exports = async function (fastify, opts) {
|
|
|
fastify.post('/search', async function (request, reply) {
|
|
|
@@ -48,6 +49,50 @@ module.exports = async function (fastify, opts) {
|
|
|
const items = await page.locator('.p13n-desktop-grid .a-column').all()
|
|
|
console.log(items.length)
|
|
|
const 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()
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ const queue = fastq(worker, 5)
|
|
|
+ items.forEach(item => {
|
|
|
+ queue.push(item, (err, r) => {
|
|
|
+ if (err) {
|
|
|
+ console.log(err)
|
|
|
+ } else {
|
|
|
+ res.push(r)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ })
|
|
|
+ await queue.drained()
|
|
|
+ await browser.close()
|
|
|
+ return res
|
|
|
+
|
|
|
await Promise.all(
|
|
|
items.map(item => {
|
|
|
return new Promise(async (resolve, reject) => {
|