-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
72 lines (51 loc) · 1.4 KB
/
worker.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
// Set up dependencies
var request = require('request');
var cheerio = require('cheerio');
var async = require('async');
var dnode = require('dnode');
// Set up environment variables
var PORT = process.env.PORT;
var number = process.env.NUMBER;
// Set up rpc server
var d = dnode.connect(PORT);
var concurrency = 8;
var worker = {};
var remote = {};
worker.id = number;
d.on('remote', function onRemote(_remote) {
remote = _remote;
remote.initWorker(worker, initWorker);
remote.getJobs(worker, getJobs);
});
function initWorker(server) {
console.log(server.response);
}
function getJobs(site) {
var url = site.url + site.section;
var $;
// Shorten url to avoid console spam
var shortUrl = url.trim().slice(0,42);
console.log(url)
request(url, function servePage(err, res, body) {
if (err) throw err;
$ = cheerio.load(body);
var jobsOnThisPage = $(site.selectors.job).map(getHrefFromLink);
var nextSection = $(site.selectors.nextPage).attr('href');
worker.jobsCollected = jobsOnThisPage;
// TODO:
// Fix transport to save jobs collected
//remote.saveJobs(worker, saveJobs);
// Call recursively until last page
if(nextSection) {
site.section = nextSection;
getJobs(site);
}
return;
});
function getHrefFromLink() {
return $(this).attr('href');
}
function saveJobs(server) {
console.log(server.response);
}
}