-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
51 lines (42 loc) · 1.23 KB
/
main.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
var subs = require('./subscriptions.json').subscriptions,
_ = require('underscore'),
fetchRSS = require('./fetchRSS.js').fetch,
Events = new require('events'),
reduce = require('./reduce.js').reduce,
download = require('download'),
EventEmitter = new Events.EventEmitter();
function checkModeAndStartDownload(data, sub) {
switch (sub.mode) {
case 'all':
_(data).each(function(post) {
downloadPost(post, sub.path);
});
break;
case 'latest' || 'last':
downloadPost(_(data).first(), sub.path);
break;
}
}
function onFinish() {
console.log('Download Finished ' + this.title + ', Ep:' + this.ep);
}
function downloadPost(post, path) {
var dl;
console.log('Downloading ' + post.title + ', Ep:' + post.url);
try {
dl = download(post.url, path);
dl.title = post.title;
dl.ep = post.url;
}
catch (e) {
console.log('Download Failed: ' + post.title);
console.log(e);
}
dl.once('close', onFinish);
}
//App Start
_(subs).each(function(sub) {
fetchRSS(sub, EventEmitter);
});
EventEmitter.on('rssFetched', reduce);
EventEmitter.on('rssReduced', checkModeAndStartDownload);