index.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. 'use strict'
  2. const { chromium, firefox, webkit } = require('playwright')
  3. const axios = require('axios').default
  4. const Queue = require('queue')
  5. const { setTimeout } = require('timers/promises')
  6. module.exports = async function (fastify, opts) {
  7. fastify.post('/search', async function (request, reply) {
  8. let response = await axios.post('https://www.sif.com/api/user/login?_t=' + new Date().getTime(), {
  9. phone: '18390881087',
  10. password: 'Gaole1401'
  11. })
  12. let token = response.headers['authorization']
  13. axios.defaults.headers.common['authorization'] = token
  14. console.log(token)
  15. const browser = await chromium.launch({ headless: true }) // Or 'firefox' or 'webkit'.
  16. const page = await browser.newPage()
  17. await page.goto(request.body.url, { timeout: 60000 })
  18. // await page.evaluate(async () => {
  19. // await new Promise((resolve, reject) => {
  20. // let i = setInterval(() => {
  21. // console.log(
  22. // (window.innerHeight, document.documentElement.scrollTop, document.documentElement.scrollHeight)
  23. // )
  24. // if (window.innerHeight + document.documentElement.scrollTop > document.documentElement.scrollHeight) {
  25. // clearInterval(i)
  26. // resolve()
  27. // } else {
  28. // window.scrollBy(0, 200)
  29. // }
  30. // }, 100)
  31. // })
  32. // })
  33. let bottom = 1
  34. while (bottom > 0) {
  35. while (bottom > 0) {
  36. await page.evaluate(() => {
  37. window.scrollBy(0, 200)
  38. })
  39. await page.waitForTimeout(100)
  40. bottom = await page.evaluate(() => {
  41. return (
  42. document.documentElement.scrollHeight -
  43. (window.innerHeight + document.documentElement.scrollTop)
  44. )
  45. })
  46. console.log(bottom)
  47. }
  48. await page.waitForTimeout(1000)
  49. bottom = await page.evaluate(() => {
  50. return document.documentElement.scrollHeight - (window.innerHeight + document.documentElement.scrollTop)
  51. })
  52. }
  53. await page.waitForTimeout(2000)
  54. //find a tag using page.locator
  55. const items = await page.locator('.p13n-desktop-grid .a-column').all()
  56. console.log(items.length)
  57. let res = []
  58. const queue = new Queue({ results: [], concurrency: 5 })
  59. items.forEach(item => {
  60. queue.push(async () => {
  61. for (let i = 0; i < 3; i++) {
  62. try {
  63. let atags = await item.locator('a').all()
  64. if (atags && atags[1]) {
  65. let text = await atags[1].innerText()
  66. let href = await atags[1].getAttribute('href')
  67. let match = /\/dp\/(.*?)\//.exec(href)
  68. let asin
  69. if (match && match[1]) {
  70. asin = match[1]
  71. }
  72. let rows = await item.locator('.a-row').all()
  73. let price = null
  74. if (rows.length > 1) {
  75. price = await rows[1].innerText()
  76. }
  77. console.log(asin + ' start')
  78. let sif = await axios.post(
  79. 'https://www.sif.com/api/search/asinFlowOverview',
  80. {
  81. asin,
  82. timePieceType: 'latelyDay',
  83. timePieceValue: '7',
  84. isListingSearch: false
  85. },
  86. {
  87. params: {
  88. country: 'US',
  89. _t: new Date().getTime(),
  90. _m: 'Sif_b589-1af2-44a3-b025-de62'
  91. },
  92. timeout: 5000
  93. }
  94. )
  95. console.log(asin + ' end')
  96. res.push({ text, href, price, asin, data: sif.data.data })
  97. console.log(res.length + '/' + items.length)
  98. break
  99. }
  100. } catch (error) {
  101. console.log(error)
  102. }
  103. await setTimeout(1000)
  104. }
  105. })
  106. })
  107. await new Promise((resolve, reject) => {
  108. queue.start(() => {
  109. resolve()
  110. })
  111. })
  112. await browser.close()
  113. return res
  114. await Promise.all(
  115. items.map(item => {
  116. return new Promise(async (resolve, reject) => {
  117. try {
  118. let atags = await item.locator('a').all()
  119. if (atags && atags[1]) {
  120. let text = await atags[1].innerText()
  121. let href = await atags[1].getAttribute('href')
  122. let match = /\/dp\/(.*?)\//.exec(href)
  123. let asin
  124. if (match && match[1]) {
  125. asin = match[1]
  126. }
  127. let rows = await item.locator('.a-row').all()
  128. let price = null
  129. if (rows.length > 1) {
  130. price = await rows[1].innerText()
  131. }
  132. let sif = await axios.get('https://www.sif.com/api/search/asinFlowOverview', {
  133. params: {
  134. country: 'US',
  135. asin,
  136. _t: new Date().getTime()
  137. }
  138. })
  139. res.push({ text, href, price, asin, data: sif.data.data })
  140. }
  141. resolve()
  142. } catch (e) {
  143. reject()
  144. }
  145. })
  146. })
  147. )
  148. await browser.close()
  149. return res
  150. })
  151. }