-
Notifications
You must be signed in to change notification settings - Fork 0
/
countries.js
147 lines (115 loc) · 4.27 KB
/
countries.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var fs = require('fs');
var request = require('request');
var Q = require('q');
var placesArray = [];
console.log('hello world');
fs.readFile('data/worldMapOriginal.json','utf8', function (err, data) {
if (err) throw err;
var jsonObj=JSON.parse(data);
var promises = [];
for (i = 0; i < jsonObj.features.length; i++) {
var countryName=jsonObj.features[i].properties.name;
var query=countryName + " fifa corruption";
var promise = makeRequest(jsonObj.features[i], query);
promises.push(promise);
}
var coordinate = {};
fs.readFile('data/coordinate.json','utf8', function (err, data) {
if (err) throw err;
coordinate=JSON.parse(data);
});
Q.all(promises)
.then(function(countries) {
console.log("All requsts done");
countries.sort(function(a, b) {
return parseFloat(b.properties.mentions) - parseFloat(a.properties.mentions);
});
var top10Countries = countries.slice(0,10);
for (var i = 0; i < top10Countries.length; i++) {
var countryName=top10Countries[i].properties.name;
var articles=top10Countries[i].properties.articles;
console.log(countryName);
console.log('LOOP NUMBER: ' + i);
function getLatLon (country) {
console.log('finding coord for ' + country);
var result = {};
for (j = 0; j < coordinate.length; j++) {
if (coordinate[j].SHORT_NAME == country) {
//console.log ('lat for ' + coordinate[j].SHORT_NAME + ' is ' + coordinate[j].LAT + coordinate[j].LONG);
result.lat = coordinate[j].LAT;
result.lon = coordinate[j].LONG;
}
}
return result;
}
var latlon = getLatLon(countryName);
console.log ('coordinate for ' + countryName + ' is ' + latlon);
var country = countryName;
var lat = latlon.lat;
var lon = latlon.lon;
var descriptionHtml = 'Fifa Corruption News Headlines: <ul class="pagination1">';
for (var k = 0; k < articles.length; k++){
descriptionHtml += '<li><a href="'+articles[k].url+'">'+articles[k].title+'</a><p>Source: '+articles[k].source+'</p><p>Publish time: '+articles[k].time+'</p></li>'
}
descriptionHtml += '</ul>';
var placesObj = {
"type": "Feature",
"geometry": {
"type": 'Point',
"coordinates": [lon, lat]
},
"properties": {
"title": country,
"description": descriptionHtml
}
}
placesArray.push(placesObj);
}
fs.writeFile("data/placesGenerated.js", "var geoJsonData = " + JSON.stringify(placesArray), function(err) {
if (err) {
return console.log(err);
}
console.log("The file2 was saved!");
});
var newWorldMap = {};
newWorldMap.type = 'FeatureCollection';
newWorldMap.features = top10Countries;
var jsonString=JSON.stringify(newWorldMap);
fs.writeFile("data/countryMentions.json",jsonString, function(err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
}).fail(function (err){
console.log(err);
return Q.reject(err);
});
});
function makeRequest(country, query) {
var deferred = Q.defer();
var lineNumber = i;
var juicer= "http:https://data.test.bbc.co.uk/bbcrd-juicer/articles?q="+query+"&recent_first=yes&size=40&apikey=9OHbOpZpVh9tQZBDjwTlTmsCF2Ce0yGQ";
request(juicer, function (error, response, body) {
country.properties.mentions = 0;
if (!error && response.statusCode == 200) {
///console.log(query, juicerObj.total);
var juicerObj=JSON.parse(body);
country.properties.mentions = juicerObj.total;
country.properties.articles = [];
for (var j = 0; j < juicerObj.hits.length; j++){
country.properties.articles.push({
title : juicerObj.hits[j].title,
url: juicerObj.hits[j].url,
time: juicerObj.hits[j].published,
source: juicerObj.hits[j].source["source-name"]
});
}
juicerObj.hits;
console.log (juicerObj.hits.length)
console.log(query);
}
deferred.resolve(country);
});
return deferred.promise;
}