forked from tylersticka/BrandColors.json
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.js
42 lines (40 loc) · 1.18 KB
/
update.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
var http = require('http')
, cheerio = require('cheerio')
, fs = require('fs')
, options = {
host: 'brandcolors.net',
port: 80,
path: '/',
method: 'GET'
};
console.log('Getting ' + options.path + ' at ' + options.host + '...')
http.get(options).on('response', function (response) {
var body = ''
, i = 0;
response.on('data', function (chunk) {
i++;
body += chunk;
console.log('Received chunk ' + i + '...');
});
response.on('end', function () {
var $ = cheerio.load(body)
, $brandColors = $('[data-brand]')
, result = {};
console.log('Finished! ' + $brandColors.length + ' brand colors found.');
console.log('Building JSON object...');
$brandColors.each(function(){
var $this = $(this)
, brand = $this.attr('data-brand')
, hex = $this.attr('data-hex');
result[ brand.replace(/\W/g, '').toLowerCase() ] = hex;
});
console.log('Saving result to BrandColors.json...');
fs.writeFile('BrandColors.json', JSON.stringify(result, null, 2), function (error) {
if (error) {
console.log(error);
} else {
console.log('All done! ^_^');
}
});
});
});