-
Notifications
You must be signed in to change notification settings - Fork 0
/
chron.js
81 lines (62 loc) · 1.76 KB
/
chron.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
request = require('request');
mongo = require('./mongodb.js');
mongo.FnDelete("jobs",{},function(){
mongo.FnDelete("jobtag",{},function(){
console.log("collection jobs & jobtags cleaned");
});
});
var tagMap = [];
(function FnGetJobs(current) {
if(typeof(current) == "undefined") {
current = 1;
}
request('https://api.angel.co/1/jobs?page='+current, function (error, response, body) {
if (response.statusCode == 200) {
var docs = JSON.parse(body);
var jobs = docs.jobs;
var i,j;
for(i=0;i<jobs.length;i++) {
for(j=0;j<jobs[i].tags.length;j++) {
var tag = jobs[i].tags[j];
if(typeof(tagMap[tag.id]) == "undefined") {
tagMap[tag.id] = {};
tagMap[tag.id].id = tag.id;
tagMap[tag.id].tag_type = tag.tag_type;
tagMap[tag.id].name = tag.name;
tagMap[tag.id].display_name = tag.display_name;
tagMap[tag.id].angellist_url = tag.angellist_url;
tagMap[tag.id].jobs = [];
}
if(tagMap[tag.id].jobs.indexOf(jobs[i].id) == -1) {
tagMap[tag.id].jobs.push(jobs[i].id);
}
}
}
mongo.FnInsert("jobs",jobs,function(err,obj){
if(err){
console.log(err);
}else{
if(docs.page == docs.last_page) {
console.log("last page reached. job finished.");
return FnJobTagInsert();
} else {
current++;
console.log(current,docs.page,docs.last_page,(docs.page == docs.last_page));
FnGetJobs(current);
}
}
});
} else {
console.log(error, body);
}
});
})();
function FnJobTagInsert() {
var jobtag = [];
for(id in tagMap) {
jobtag.push(tagMap[id]);
}
mongo.FnInsert("jobtag",jobtag,function(err,obj){
console.log("job tags inserted.");
});
}