-
-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(stores): list stores per country
- Loading branch information
Showing
5 changed files
with
106 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#!/usr/bin/env node | ||
'use strict'; | ||
|
||
let program = require('commander'); | ||
let cheerio = require('cheerio'); | ||
let pkg = require('./../package.json'); | ||
|
||
program | ||
.version(pkg.version) | ||
.arguments('[countryCodes...]') | ||
.description( | ||
'Will list the IKEA stores found in a country by scraping an example ' + | ||
'detail page from ikea.com or other domains and extracting the store ' + | ||
'buCodes and names' | ||
) | ||
.action(function(countryCodes) { | ||
// make countryCodes unique and lowercase | ||
countryCodes = countryCodes | ||
.map(function(countryCode) { | ||
return countryCode.toLowerCase(); | ||
}) | ||
.filter(function(cur, i, arr) { | ||
return arr.indexOf(cur, i + 1) === -1; | ||
}); | ||
|
||
countryCodes.forEach(function(countryCode) { | ||
// trying to use a fixed productId of a product which must be popular in | ||
// all countries | ||
let productId = '30275861'; | ||
let languageCode = countryCode; | ||
let url = `http://www.ikea.com/${countryCode}/${languageCode}/catalog/products/${productId}/`; | ||
|
||
let reporter = require('./lib/reporter/stores-table'); | ||
let request = require('request'); | ||
request.get(url, function(err, response) { | ||
let $ = cheerio.load(response.body); | ||
let storeDropdownElm = $('#ikeaStoreNumber1'); | ||
let stores = storeDropdownElm.find('option') | ||
.map(function(index, elm) { | ||
return { | ||
buCode: $(elm).attr('value'), | ||
name: $(elm).text().replace(/IKEA\s+/, ''), | ||
countryCode: countryCode, | ||
}; | ||
}) | ||
.toArray() | ||
// filter stores where buCode is 3-letter digit and store name is | ||
// not empty | ||
.filter(function(store) { | ||
return store.buCode && store.buCode.match(/^\d+$/) && store.name; | ||
}); | ||
|
||
console.log(reporter.show(stores)); | ||
}); // forEach | ||
}); // action | ||
}) | ||
.parse(process.argv); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
let Table = require('cli-table'); | ||
let countries = require('i18n-iso-countries'); | ||
|
||
module.exports = { | ||
show: function(stores) { | ||
let table = new Table({ | ||
head: [ | ||
'countryCode', | ||
'country', | ||
'buCode', | ||
'name', | ||
], | ||
}); | ||
|
||
// sort data by countryCode and buCode in ascending order | ||
stores.sort(function(a, b) { | ||
if (a.countryCode > b.countryCode) { | ||
return 1; | ||
} else if (a.countryCode < b.countryCode) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
stores.sort(function(a, b) { | ||
if (a.buCode > b.buCode) { | ||
return 1; | ||
} else if (a.buCode < b.buCode) { | ||
return -1; | ||
} | ||
return 0; | ||
}); | ||
|
||
stores.forEach(function(item) { | ||
table.push([ | ||
item.countryCode, | ||
countries.getName(item.countryCode, 'en'), | ||
item.buCode, | ||
item.name, | ||
]); | ||
}); | ||
|
||
return table.toString(); | ||
}, | ||
}; |