-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathscraper.js
67 lines (50 loc) · 1.51 KB
/
scraper.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
var _ = require('lodash');
var when = require('when');
var spotify = require('spotify-crawler');
var request = require('promise-request');
var Country = require('./model/country');
var Update = require('./model/update');
spotify.fetch()
.then(getGDPData)
.then(saveCountries)
.then(updateTime)
.catch(console.log);
function getGDPData(countries){
// GDP per capita, PPP (current international $) - 2012
var url = "http://api.worldbank.org/countries/all/indicators/NY.GDP.PCAP.PP.CD?format=json&date=2012:2012&per_page=300";
return request(url, true)
.then(function(data){
var results = data[1];
_.each(results, function(result){
var country = _.find(countries, function(country){
return country.rel === result.country.id.toLowerCase();
});
if(country)
country.gdp = result.value;
});
return countries;
});
}
function saveCountries(countries){
console.log('Inserting {0} countries in DB'.format(countries.length));
var save = Country.create(countries)
.then(function(){
console.log('yay success!!');
});
return save;
}
function updateTime(){
var record = new Update();
record.save();
}
function saveCountry(country){
console.log('Inserting country {0}({1})'.format(country.title, country.currency));
var storedCountry = new Country(country);
return when.promise(function(resolve, reject){
storedCountry.save(function(err){
if(err)
throw reject(err);
resolve(country);
});
});
}