-
Notifications
You must be signed in to change notification settings - Fork 2
/
createLocalTestDb.js
84 lines (76 loc) · 2.35 KB
/
createLocalTestDb.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
var http = require('http');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/company-test');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
var companySchema = mongoose.Schema({
companyID: Number,
name: String,
website: String,
squareLogo: String,
overallRating: String,
cultureAndValuesRating: String,
seniorLeadershipRating: String,
compensationAndBenefitsRating: String,
careerOpportunitiesRating: String,
workLifeBalanceRating: String,
recommendToFriendRating: String,
sectorName: String,
industryName: String });
var Company = mongoose.model('Company', companySchema);
var apiURL = function(params) {
return 'http://api.glassdoor.com/api/api.htm?v=1&format=json&t.p=163516&t.k=fxjVIKjFddO&action=employers&country='
+ params['country'] +
'&q='
+ params['query'] +
'&pn='
+ params['page'] +
'&userip=192.168.43.42&useragent=Mozilla/%2F4.0';
};
var total = [];
for (var i = 1; i < 50; i ++){
http.get(apiURL({'country': 'uk', 'query': 'software', 'page': i}), function(res) {
var body = '';
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
total.push(JSON.parse(body).response.employers);
});
}).on('error', function(e) {
console.log('Got error: ' + e.message);
});
}
setTimeout(function() {
var data = total.reduce( function(a, b) {
return a.concat(b);
},
[]
);
data.forEach(function(object){
var company = new Company({
companyID: object['id'],
name: object.name,
website: object.website,
squareLogo: object.squareLogo,
overallRating: object.overallRating,
cultureAndValuesRating: object.cultureAndValuesRating,
seniorLeadershipRating: object.seniorLeadershipRating,
compensationAndBenefitsRating: object.compensationAndBenefitsRating,
careerOpportunitiesRating: object.careerOpportunitiesRating,
workLifeBalanceRating: object.workLifeBalanceRating,
recommendToFriendRating: object.recommendToFriendRating,
sectorName: object.sectorName,
industryName: object.industryName });
company.save(function(err) {
if (err) {
console.log(err);
} else {
console.log(company.name);
}
});
});
}, 2000)
setTimeout(function() {
mongoose.disconnect();
}, 5000);