xiongzhu 2 éve
szülő
commit
423404791f
3 módosított fájl, 47 hozzáadás és 1 törlés
  1. 1 0
      package.json
  2. 45 0
      routes/api/index.js
  3. 1 1
      yarn.lock

+ 1 - 0
package.json

@@ -22,6 +22,7 @@
     "fastify": "^4.0.0",
     "fastify-cli": "^5.7.1",
     "fastify-plugin": "^4.0.0",
+    "fastq": "^1.15.0",
     "playwright": "^1.32.1"
   },
   "devDependencies": {

+ 45 - 0
routes/api/index.js

@@ -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) => {

+ 1 - 1
yarn.lock

@@ -1041,7 +1041,7 @@ fastify@^4.0.0:
     semver "^7.3.7"
     tiny-lru "^10.0.0"
 
-fastq@^1.6.1:
+fastq@^1.15.0, fastq@^1.6.1:
   version "1.15.0"
   resolved "https://registry.npmmirror.com/fastq/-/fastq-1.15.0.tgz#d04d07c6a2a68fe4599fea8d2e103a937fae6b3a"
   integrity sha512-wBrocU2LCXXa+lWBt8RoIRD89Fi8OdABODa/kEnyeyjS5aZO5/GNvI5sEINADqP/h8M29UHTHUb53sUu5Ihqdw==