-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
107 lines (86 loc) · 2.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import 'dotenv/config'
import axios from 'axios'
import { parse } from 'node-html-parser'
import { sheetsClient } from './sheetsClient.js'
import { selectors, getRecordInfo } from './scrapeHelpers.js'
const BASE_URL = 'https://www.vinylbazar.net'
const getDataForPath = async (path, pageId, data) => {
let response = await axios.get(`${BASE_URL}${path}?page=${pageId}&man=9`)
if (!response || !response.data) {
console.log('Invalid path: ', path)
return data
}
const productRoot = parse(response.data)
data.push(...productRoot.querySelectorAll(selectors.product))
// Call getDataForPath recursively when there is another page for the path
if (productRoot.querySelector(selectors.nextPage)) {
await getDataForPath(path, pageId + 1, data)
}
return data
}
const callBish = async () => {
// Get paths
const paths = []
// TODO: Testing Sheets API
if (!paths.length) {
const sheets = await sheetsClient()
// Sheet range
const range = `Sheet1!R1C1:R2C3`
// Get data from Sheet
let response = await sheets.spreadsheets.values.get({
spreadsheetId: process.env.SHEET_ID,
range,
})
// Prettify returned data, for testing
const rows = response.data.values.map((x) => {
return { title: x[0], price: x[1], link: x[2] }
})
console.log(rows)
// Update Sheet
response = await sheets.spreadsheets.values.update({
spreadsheetId: process.env.SHEET_ID,
includeValuesInResponse: true,
valueInputOption: 'RAW',
range: 'Sheet1!R4C1:R5C3',
requestBody: {
values: [
[1, 2, 3],
[4, 5, 7],
],
},
})
// Updated values
const newRows = response.data.updatedData.values
console.log(newRows)
return
}
let response = await axios.get(BASE_URL)
if (!response?.data) {
throw new Error('No data received.')
}
const root = parse(response.data)
for (let item of root.querySelectorAll(selectors.menuItem)) {
if (item.rawAttributes?.href) {
paths.push(item.rawAttributes.href)
}
}
// Log paths
paths.forEach((element) => {
console.log(element)
})
// Get products
const products = []
for (const path of paths) {
products.push({ path, data: await getDataForPath(path, 0, []) })
}
for (const section of products) {
for (const record of section.data) {
const recordInfo = getRecordInfo(record)
console.log(recordInfo.title)
console.log(recordInfo.url)
console.log(recordInfo.price)
console.log(recordInfo.img)
}
}
}
callBish()