forked from C4illin/systembolaget-data
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getAllProducts.js
61 lines (56 loc) · 1.96 KB
/
getAllProducts.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
import fetch from 'node-fetch'
import fs from 'fs'
export const getAllProducts = () => {
let starturl = "https://api-extern.systembolaget.se/sb-api-ecommerce/v1/productsearch/search?size=30"
// let products = require('./test.json')
let products = []
let options = {
method: 'GET',
headers: {
"accept": "application/json",
"access-control-allow-origin": "*",
"ocp-apim-subscription-key": "cfc702aed3094c86b92d6d4ff7a54c84",
"Referer": "https://www.systembolaget.se/",
}
}
let urlParameters = [
'&assortmentText=S%C3%A4song',
'&assortmentText=Tillf%C3%A4lligt%20sortiment',
'&assortmentText=Webblanseringar',
'&assortmentText=Fast%20sortiment',
'&assortmentText=Lokalt%20%26%20Sm%C3%A5skaligt',
'&assortmentText=Presentartiklar',
'&assortmentText=Ordervaror&price.max=250',
'&assortmentText=Ordervaror&price.min=251'
];
(async () => {
for (const urlParam of urlParameters) {
let url = starturl + urlParam
console.log("Starting: " + url)
for (let i = 1; i < 500; i++) {
await fetch(url + "&page=" + i, options)
.then(res => res.json())
.then(json => {
if (i > json["metadata"]["nextPage"] && json["metadata"]["nextPage"] > 0) {
console.log("Aborted, something is wrong...")
console.log("Last page: " + json["metadata"]["nextPage"])
i = 10000
} else if (json["metadata"]["nextPage"] == -1) {
products = products.concat(json["products"])
console.log("Done after " + i + " pages")
i = 10000
} else {
products = products.concat(json["products"])
}
})
.catch(error => console.error('Error:', error));
}
}
fs.writeFile('products.json', JSON.stringify(products, null, 2), (err) => {
if (err) {
throw err
}
console.log("Wrote: " + products.length + " products")
})
})()
}