xiongzhu 2 years ago
parent
commit
ee97b5c235
3 changed files with 42 additions and 40 deletions
  1. 2 2
      package.json
  2. 31 36
      routes/api/index.js
  3. 9 2
      yarn.lock

+ 2 - 2
package.json

@@ -22,8 +22,8 @@
     "fastify": "^4.0.0",
     "fastify-cli": "^5.7.1",
     "fastify-plugin": "^4.0.0",
-    "fastq": "^1.15.0",
-    "playwright": "^1.32.1"
+    "playwright": "^1.32.1",
+    "queue": "^6.0.2"
   },
   "devDependencies": {
     "tap": "^16.1.0"

+ 31 - 36
routes/api/index.js

@@ -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
 

+ 9 - 2
yarn.lock

@@ -1041,7 +1041,7 @@ fastify@^4.0.0:
     semver "^7.3.7"
     tiny-lru "^10.0.0"
 
-fastq@^1.15.0, fastq@^1.6.1:
+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==
@@ -1275,7 +1275,7 @@ inflight@^1.0.4:
     once "^1.3.0"
     wrappy "1"
 
-inherits@2, inherits@2.0.4, inherits@^2.0.3:
+inherits@2, inherits@2.0.4, inherits@^2.0.3, inherits@~2.0.3:
   version "2.0.4"
   resolved "https://registry.npmmirror.com/inherits/-/inherits-2.0.4.tgz#0fa2c64f932917c3433a0ded55363aae37416b7c"
   integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
@@ -1936,6 +1936,13 @@ punycode@^2.0.0, punycode@^2.1.0:
   resolved "https://registry.npmmirror.com/punycode/-/punycode-2.3.0.tgz#f67fa67c94da8f4d0cfff981aee4118064199b8f"
   integrity sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==
 
+queue@^6.0.2:
+  version "6.0.2"
+  resolved "https://registry.npmmirror.com/queue/-/queue-6.0.2.tgz#b91525283e2315c7553d2efa18d83e76432fed65"
+  integrity sha512-iHZWu+q3IdFZFX36ro/lKBkSvfkztY5Y7HMiPlOUjhupPcG2JMfst2KKEpu5XndviX/3UhFbRngUPNKtgvtZiA==
+  dependencies:
+    inherits "~2.0.3"
+
 quick-format-unescaped@^4.0.3:
   version "4.0.4"
   resolved "https://registry.npmmirror.com/quick-format-unescaped/-/quick-format-unescaped-4.0.4.tgz#93ef6dd8d3453cbc7970dd614fad4c5954d6b5a7"