-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (62 loc) · 3.15 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
const vinmonopolet = require('vinmonopolet')
const fs = require('fs');
const filter = {category:'all',regex:'.'}
//const filter = {category:'Rødvin',regex:'rødvin'}
//const filter = {category:'Hvitvin',regex:'hvitvin'}
//const filter = {category:'Brennevin',regex:'whisky|druebrennevin|likør|akevitt|vodka|fruktbrennevin|rom|gin|bitter|genever'}
//const filter = {category:'Øl',regex:'øl|hveteøl|surøl|ale|porter|stout|klosterstil|spesial|lager|red\/amber|barley|mjød'}
//const filter = {category:'Musserende vin',regex:'musserende|champagne'}
//const filter = {category:'Sterkvin',regex:'portvin|sterkvin|sherry|vermut|madeira'}
//const filter = {category:'Perlende vin',regex:'perlende'}
//const filter = {category:'Sider',regex:'sider'}
//const filter = {category:'Aromatisert vin',regex:'aromatisert'}
//const filter = {category:'Fruktvin',regex:'fruktvin'}
//const filter = {category:'Alkoholfritt',regex:'alkoholfri'}
const deleteElements = ['url','barcode','images','method','storeable','foodPairing','eco','gluten','kosher','fairTrade','bioDynamic','mainProducer','distributor','distributorId','wholesaler','storeCategory','buyable','deliveryTime','nrOfUsage','availableForPickup','averageRating','stock','status','ageLimit','expiredDate','purchasable','newProduct']
const convertToArray = ['productType','mainCountry']
const setToId = 'code'
const filename = 'products-' + encodeURIComponent(filter.category) +'.str'
const writeStream = fs.createWriteStream(filename)
var i = 0
regex = new RegExp(filter.regex, 'gi')
vinmonopolet
.stream.getProducts()
.on('data', function(product) {
// Filter on productType
var productType = product.productType
if (productType.match(regex)) {
console.log('Saving product: ' + product.code + ' ' + product.productType)
// Remove unwanted elements in object
for (element in deleteElements) {
var deleteElement = deleteElements[element]
delete product[deleteElement]
}
// Convert string values to array values
for (stringKey in convertToArray) {
var stringToArray = convertToArray[stringKey]
product[stringToArray] = [product[stringToArray]]
}
// Add filter.category to product.productType array
if (filter.category != product.productType[0] && filter.category != 'all') {
product.productType.push(filter.category)
// Temporary filtering fix (doesn't recognize array items as separate filters, but splits on ' ' (space)
var productTypeArray = product.productType.join('§')
//console.log(productTypeArray)
product.productType = [productTypeArray]
//console.log('new productType: ' + product.productType)
}
// Define field to be id
product.id = product[setToId]
// Create product image url
product.image = 'https://bilder.vinmonopolet.no/cache/800x800-0/' + product.id + '-1.jpg'
console.log(JSON.stringify(product))
writeStream.write(JSON.stringify(product))
i++
}
if (!productType.match(regex)) {
// console.log('Skipping product: ' + product.code + ' ' + product.productType)
}
})
.on('end', function() {
console.log('Done! Saved products: ' + i)
})