-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrunch-server.js
427 lines (342 loc) · 21.1 KB
/
brunch-server.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
var request = require('request');
var parentCoSearch = require('./app/parentCompanySearch.js');
// Server start function
module.exports.startServer = function(cb) {
//configure the server
var express = require('express'),
app = express();
var async = require('async');
var cheerio = require('cheerio');
var util = require('util');
// REMOVE THIS PROBABLY, used in subsidyDATA and violationData
var parseString = require('xml2js').parseString;
app.use(express.static('app'));
// Semantics3 api credentials
var api_key = 'SEM3AFEFE0A3431261B870FB7DC3BDF7FED2';
var api_secret = 'N2JjNjlmNzEwNDliOTM4N2YxMTI3NWEwYTJhMWI5MjE';
var sem3 = require('semantics3-node')(api_key, api_secret);
//get data from browser to web-server
app.get('/products/:query', function(req, res) {
async.waterfall([
//first request to Semantics3/UPC Search
function(callback) {
// build request for api
sem3.products.products_field("upc", req.params.query);
sem3.products.products_field("field", ["name", "gtins"]);
// Run the request
sem3.products.get_products(
function(err, products) {
//console.log("1st request(products): "+products);
var products = JSON.parse(products);
//checks that upc returned something
if (products.results_count === 0) {
callback(new Error(products.message));
//passes Sem3 results down waterfall
} else if (products.results) {
callback(null, products.results);
}
});
},
//second request, to corpwatch.org, find min_cw_id
function(products, callback) {
//convert upcSearch into $brand and $manufacturer
var brand = products[0].brand;
var manufacturer = products[0].manufacturer;
//console.log("2nd request(brand): "+brand);
//console.log("2nd request(manufacturer): "+manufacturer);
request('http://api.corpwatch.org/companies.json?company_name=' + brand, function(err, results) {
//console.log(results.body);
var min_cw_id = parentCoSearch.min_cw_idSearch(results);
//console.log(parentCoSearch.min_cw_idSearch(results));
callback(err, min_cw_id, brand);
});
},
//third request, to corpwatch.org
function(min_cw_id, brand, callback) {
//use min_cw_id to find parent name
request('http://api.corpwatch.org/companies/' + min_cw_id, function(err, results) {
var parentCo = parentCoSearch.parentCo_Query(results);
//console.log("3rd request(parentCo): "+parentCo);
callback(err, parentCo, brand);
});
},
//fourth request, scrape stock symbol from Bloomberg
function(parentCo, brand, callback) {
request("https://www.bloomberg.com/markets/symbolsearch?query="+parentCo+"&commit=Find+Symbols.json", function(err, response, html) {
var symbol;
// First we'll check to make sure no errors occurred when making the request
if (!err) {
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
//Seems weird response, using for testing
//console.log("html: "+html);
var $ = cheerio.load(html);
// We'll use the unique header class as a starting point.
symbol = $('.odd').children().first().text();
//console.log("4th request(symbol): "+symbol);
}
callback(err, symbol, parentCo, brand);
});
},
//fifth request, scrape Bloomberg for company profiles, execs, and news
function(symbol, parentCo, brand, callback) {
request("https://www.bloomberg.com/quote/"+symbol, function(err, response, html) {
var co_profile = {};
var execs = {};
var news = {};
var press_release = {};
var bloomberg = {
execs:execs,
news:news,
press_release:press_release,
co_profile: co_profile};
// First we'll check to make sure no errors occurred when making the request
if (!err) {
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
var $ = cheerio.load(html);
//Get Company News and Press Releases
//Company News:
$(" .news__state.active > article").each(function(i){
//var time_published = $(this).first().text();
var time_published = $(this).children().attr('datetime');
var headline = $(this).children().last().text();
var article_url = $(this).children().last().children().attr('href');
//console.log(i+" "+headline);
bloomberg.news[i] = {time_published:time_published, headline:headline, article_url:article_url};
})
//console.log(bloomberg.news);
//Press Releases:
$("div[data-group='press-releases'] > article").each(function(i){
//var time_published = $(this).first().text();
var time_published = $(this).children().attr('datetime');
var headline = $(this).children().last().text();
var article_url = $(this).children().last().children().attr('href');
//console.log(i+" "+headline);
bloomberg.press_release[i] = {time_published:time_published, headline:headline, article_url:article_url};
})
//console.log(bloomberg.press_release);
//Get EXECUTIVES INFO: jobtitle, name, link to bloomberg profile
// boardMembers don't load, issue
$('.link').each(function(i){
var url = $(this).attr('href');
var name = $(this).attr('title');
var jobtitle = $(this).parent().parent().children().last().text();
bloomberg.execs[i] = {name:name, jobtitle:jobtitle, url:url};
})
//console.log(bloomberg.execs);
//Get Company profile: description, address, phone, website, url
var description = $('.profile__description').text();
//console.log("Fifth Request (description): "+description);
var address = $('.profile__detail.profile__detail__address').clone().children().remove().end().text();
var phone = $('.profile__detail.profile__detail__address').next().clone().children().remove().end().text();
bloomberg.co_profile = {description:description, address:address, phone:phone};
$('.profile__detail__website_link').each(function(){
var website = $(this).text();
var url = $(this).attr('href');
//console.log("Fifth request (URL): "+url);
//console.log("Fifth request (website): "+website);
bloomberg.co_profile.website = website;
bloomberg.co_profile.url = url;
})
//console.log("Fifth request (bloomberg): "+bloomberg);
}
callback(err, bloomberg, parentCo, brand);
});
},
//sixth request, get orgID from openSecrets api
function (bloomberg, parentCo, brand, callback) {
//openSecrets api_key
var key = "27ca23c2803eddb132adecd7238d4c94";
request('http://www.opensecrets.org/api/?method=getOrgs&org='+ parentCo+'&apikey='+key+'&output=xml', function(err, results) {
// First we'll check to make sure no errors occurred when making the request
if (!err) {
var orgID = results.body;
//console.log("6th request(orgID): "+JSON.stringify(orgID));
parseString(orgID, {explicitArray: false}, function(err, result){
if (! err){
//console.log(JSON.stringify(result));
orgID = result.response.organization.$.orgid;
//console.log("orgID: "+orgID);
}
})
//console.log("9th request(violationData): "+violationData);
callback(null, orgID, parentCo, brand, bloomberg);
} else {
callback(err, bloomberg, brand, parentCo);
}
});
},
// 7th request, get lobby info from openSecrets api
function(orgID, parentCo, bloomberg, brand, callback) {
//openSecrets api_key
var key = "27ca23c2803eddb132adecd7238d4c94";
request('http://www.opensecrets.org/api/?method=orgSummary&id='+orgID+'&apikey='+key+'&output=xml', function(err, results) {
// First we'll check to make sure no errors occurred when making the request
if (!err) {
var secretsSummary = results.body;
//console.log("6th request(orgID): "+JSON.stringify(orgID));
parseString(secretsSummary, {explicitArray: false}, function(err, result){
if (! err){
//console.log(JSON.stringify(result));
secretsSummary = result.response.organization.$;
//console.log("secretsSummary: "+JSON.stringify(secretsSummary));
}
})
callback(null, bloomberg, parentCo, brand, secretsSummary);
} else {
callback(err, bloomberg, parentCo, brand);
}
});
},
// 7.5th request, scrape url for violationtracker
function(bloomberg, parentCo, brand, secretsSummary, callback) {
//console.log(parentCo);
request("http://violationtracker.goodjobsfirst.org/prog.php?company="+parentCo, function(err, response, html) {
var VTurl;
// First we'll check to make sure no errors occurred when making the request
if (!err) {
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
//console.log("VTURL: "+html);
var $ = cheerio.load(html);
// We'll use the unique header class as a starting point.
VTurl = $('.views-field.views-even').children().last().attr('href');
//console.log("8.5th request(VTurl): "+VTurl);
}
if (VTurl === undefined){
VTurl = "try_again";
callback(err, VTurl, bloomberg, parentCo, brand, secretsSummary);
}
else{
callback(err, VTurl, bloomberg, parentCo, brand, secretsSummary);
}
});
},
//IF VTurl = undefined, try violationtracker url scraper again
function(VTurl, brand, parentCo, bloomberg, secretsSummary, callback) {
//console.log("VTurl 3: "+VTurl);
if (VTurl === "try_again"){
request("http://violationtracker.goodjobsfirst.org/prog.php?company="+brand, function(err, response, html) {
// First we'll check to make sure no errors occurred when making the request
if (!err) {
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
//console.log("VTURL: "+html);
var $ = cheerio.load(html);
// We'll use the unique header class as a starting point.
VTurl = $('.views-field.views-even').children().last().attr('href');
//console.log("try again request(VTurl): "+VTurl);
}
callback(err, VTurl, bloomberg, parentCo, brand, secretsSummary);
});
}else{
callback(null, VTurl, bloomberg, parentCo, brand, secretsSummary);
}
},
//8th request, get xml data from violation tracker from goodjobsfirst
function(VTurl, bloomberg, parentCo, brand, secretsSummary, callback) {
//console.log("8th request(symbol): "+VTurl);
//console.log("8th request(parentCo): "+parentCo);
request(VTurl+"&detail=xml_results", function(err, res) {
// First we'll check to make sure no errors occurred when making the request
if (!err) {
var violationData = res.body;
//console.log("violationData: "+violationData);
parseString(violationData, {explicitArray: false}, function(err, result){
if (! err){
//console.log(JSON.stringify(result));
violationData = result.ViolationTrackerSearchResults;
//console.log(violationData);
}
})
//console.log("9th request(violationData): "+violationData);
callback(null, violationData, bloomberg, parentCo, brand, secretsSummary);
} else {
callback(err, bloomberg, parentCo, brand, secretsSummary);
}
});
},
// 8.5th request, scrape url for subsidy tracker
function(violationData, bloomberg, parentCo, brand, secretsSummary, callback) {
request("http://subsidytracker.goodjobsfirst.org/prog.php?company="+parentCo, function(err, response, html) {
var STurl;
// First we'll check to make sure no errors occurred when making the request
if (!err) {
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
var $ = cheerio.load(html);
// We'll use the unique header class as a starting point.
STurl = $('.views-field.views-even').children().last().attr('href');
//console.log("9.5th request(STurl): "+STurl);
}
if (STurl === undefined){
STurl = "try_again";
callback(err, STurl, violationData, bloomberg, parentCo, brand, secretsSummary);
}
else{
callback(err, STurl, violationData, bloomberg, parentCo, brand, secretsSummary);
}
});
},
//If STurl = undefined, try subsidytracker url scraper again
function(STurl, violationData, bloomberg, parentCo, brand, secretsSummary, callback){
//console.log("if STurl undefined: "+violationData);
//console.log("if STurl undefined: "+ brand);
//console.log("if STurl undefined: "+parentCo);
if (STurl === "try_again"){
request("http://subsidytracker.goodjobsfirst.org/prog.php?company="+brand, function(err, response, html) {
// First we'll check to make sure no errors occurred when making the request
if (!err) {
// Next, we'll utilize the cheerio library on the returned html which will essentially give us jQuery functionality
//console.log("VTURL: "+html);
var $ = cheerio.load(html);
// We'll use the unique header class as a starting point.
STurl = $('.views-field.views-even').children().last().attr('href');
//console.log("try again request(VTurl): "+VTurl);
}
callback(err, STurl, violationData, bloomberg, parentCo, secretsSummary);
});
}else{
callback(null, STurl, violationData, bloomberg, parentCo, secretsSummary);
}
},
//9th request, get xml data from subsidy tracker from goodjobsfirst
function(STurl, violationData, bloomberg, parentCo, secretsSummary, callback) {
// console.log("10th request(symbol): "+symbol);
//console.log("10th request(parentCo): "+STurl);
request(STurl + "&detail=x", function(err, res) {
// First we'll check to make sure no errors occurred when making the request
if (!err) {
var subsidyData = res.body;
parseString(subsidyData, {explicitArray: false}, function(err, result){
if (!err){
subsidyData = result.SubsidyTrackerSearchResults;
//console.log(subsidyData);
}
});
//console.log("10th request(subsidyData): "+subsidyData);
callback(null, subsidyData, violationData, bloomberg, parentCo, secretsSummary);
} else {
callback(err, violationData, bloomberg, parentCo, secretsSummary);
}
});
}
],
function asyncComplete(err, subsidyData, violationData, bloomberg, parentCo, secretsSummary) { //change results to companies
if (err) { //there was an error with some earlier function
console.log("Error", err);
res.status(500).send(err.message);
} else {
console.log("Successfully completed operation.");
return res.status(200).send({
subsidyData: subsidyData,
violationData: violationData,
bloomberg: bloomberg,
parentCo: parentCo,
secretsSummary: secretsSummary
});
}
}
);
});
app.listen(3333, function() {
console.log('web server listening on port 3333');
});
cb();
};