| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- 'use strict'
- const { chromium, firefox, webkit } = require('playwright')
- const axios = require('axios').default
- const Queue = require('queue')
- const { setTimeout } = require('timers/promises')
- module.exports = async function (fastify, opts) {
- fastify.post('/search', async function (request, reply) {
- let response = await axios.post('https://www.sif.com/api/user/login?_t=' + new Date().getTime(), {
- phone: '18390881087',
- password: 'Gaole1401'
- })
- let token = response.headers['authorization']
- axios.defaults.headers.common['authorization'] = token
- console.log(token)
- const browser = await chromium.launch({ headless: true }) // Or 'firefox' or 'webkit'.
- const page = await browser.newPage()
- await page.goto(request.body.url, { timeout: 60000 })
- // await page.evaluate(async () => {
- // await new Promise((resolve, reject) => {
- // let i = setInterval(() => {
- // console.log(
- // (window.innerHeight, document.documentElement.scrollTop, document.documentElement.scrollHeight)
- // )
- // if (window.innerHeight + document.documentElement.scrollTop > document.documentElement.scrollHeight) {
- // clearInterval(i)
- // resolve()
- // } else {
- // window.scrollBy(0, 200)
- // }
- // }, 100)
- // })
- // })
- let bottom = 1
- while (bottom > 0) {
- while (bottom > 0) {
- await page.evaluate(() => {
- window.scrollBy(0, 200)
- })
- await page.waitForTimeout(100)
- bottom = await page.evaluate(() => {
- return (
- document.documentElement.scrollHeight -
- (window.innerHeight + document.documentElement.scrollTop)
- )
- })
- console.log(bottom)
- }
- await page.waitForTimeout(1000)
- bottom = await page.evaluate(() => {
- return document.documentElement.scrollHeight - (window.innerHeight + document.documentElement.scrollTop)
- })
- }
- await page.waitForTimeout(2000)
- //find a tag using page.locator
- const items = await page.locator('.p13n-desktop-grid .a-column').all()
- console.log(items.length)
- let res = []
- const queue = new Queue({ results: [], concurrency: 5 })
- items.forEach(item => {
- queue.push(async () => {
- for (let i = 0; i < 3; i++) {
- try {
- 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()
- }
- console.log(asin + ' start')
- let sif = await axios.post(
- 'https://www.sif.com/api/search/asinFlowOverview',
- {
- asin,
- timePieceType: 'latelyDay',
- timePieceValue: '7',
- isListingSearch: false
- },
- {
- params: {
- country: 'US',
- _t: new Date().getTime(),
- _m: 'Sif_b589-1af2-44a3-b025-de62'
- },
- timeout: 5000
- }
- )
- console.log(asin + ' end')
- res.push({ text, href, price, asin, data: sif.data.data })
- console.log(res.length + '/' + items.length)
- break
- }
- } catch (error) {
- console.log(error)
- }
- await setTimeout(1000)
- }
- })
- })
- await new Promise((resolve, reject) => {
- queue.start(() => {
- resolve()
- })
- })
- await browser.close()
- return res
- await Promise.all(
- items.map(item => {
- return new Promise(async (resolve, reject) => {
- try {
- 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()
- }
- })
- res.push({ text, href, price, asin, data: sif.data.data })
- }
- resolve()
- } catch (e) {
- reject()
- }
- })
- })
- )
- await browser.close()
- return res
- })
- }
|