-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-store-data.js
82 lines (68 loc) · 1.92 KB
/
get-store-data.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
import fs from 'fs'
import fetch from 'node-fetch'
import { ikeaData } from './basic-store-data.js'
for (let [i, country] of Object.entries(ikeaData)) {
const percentage = (((parseInt(i) + 1) / ikeaData.length) * 100).toFixed(2)
if ((country.apiType ?? 0) != 0 || country.cantCheckUrls) {
console.log(
`${percentage}% complete \tskipping all checks for ${
country.abbrv ?? country.name
} (can't check urls)`
)
continue
}
ikeaData[i].itemUrls = {}
if (country.itemIds.original) {
const itemPageHtml = await (
await fetch(
`https://www.ikea.com/${country.urlCode}/p/blahaj-${country.itemIds.original}/`
)
).text()
ikeaData[i].itemUrls.original = itemPageHtml.match(
/<meta (?:[^<>]*?)?property="og:url" content="(.*?)"\/?>/
)[1]
}
if (country.itemIds.baby) {
const itemPageHtml = await (
await fetch(
`https://www.ikea.com/${country.urlCode}/p/blahaj-${country.itemIds.baby}/`
)
).text()
ikeaData[i].itemUrls.baby = itemPageHtml.match(
/<meta (?:[^<>]*?)?property="og:url" content="(.*?)"\/?>/
)[1]
}
if (country.cantCheckAutomatically) {
console.log(
`${percentage}% complete \tskipping stores for ${
country.abbrv ?? country.name
} (can't check automatically)`
)
continue
}
let storesResp = await fetch(
`https://www.ikea.com/${country.urlCode}/meta-data/navigation/stores-detailed.json`
)
let stores = await storesResp.json()
ikeaData[i].stores = stores.map(e => {
return {
value: e.id,
name: e.displayName,
address: e.address.street,
displayAddress: e.address.displayAddress
}
})
console.log(
`${percentage}% complete \tfinished retrieving stores for ${
country.abbrv ?? country.name
}`
)
}
console.log(`Sorting entries`)
ikeaData.sort((a, b) => a.name.localeCompare(b.name))
console.log(`Complete, saving to file`)
fs.promises.writeFile(
'src/lib/store-data.js',
'export default ' + JSON.stringify(ikeaData)
)
console.log(`Success`)