forked from arkisaoglu/crunchbase.json
-
Notifications
You must be signed in to change notification settings - Fork 1
/
record_companies_full.js
54 lines (50 loc) · 1.44 KB
/
record_companies_full.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
var request = require('request');
var async = require('async');
var fs = require('fs');
var key = require('./key.js');
var url = 'http://api.crunchbase.com/v/1/company/triphunter.js?api_key='+key;
function get_company(name, cb){
var url = 'http://api.crunchbase.com/v/1/company/'+name+'.js?api_key='+key;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
try {
var company = JSON.parse(body);
return cb(null, company); // Print the google web page.
} catch(e){
return cb(e);
}
} else {
return cb(error);
}
});
}
function write_company(name, json){
return fs.writeFileSync(__dirname+'/companies/'+name+'.json', JSON.stringify(json));
}
function get_fn_array(){
var arr = [];
var companies = JSON.parse(fs.readFileSync('./companies.json', 'utf8'));
companies.forEach(function(company, i){
try{
var ls = fs.lstatSync(__dirname+'/companies/'+company.permalink+'.json');
return false;
} catch(e){
var fn = function(cb){
get_company(company.permalink, function(e, company_json){
console.log(company);
if (!e){
write_company(company.permalink, company_json);
return cb(null);
} else {
console.error(e);
return cb(null);
}
});
};
arr.push(fn);
}
});
return arr;
}
var funcs = get_fn_array();
async.series(funcs);