-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpodcatcher.js
More file actions
190 lines (152 loc) · 4.52 KB
/
Copy pathpodcatcher.js
File metadata and controls
190 lines (152 loc) · 4.52 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
var feedparser = require('ortoo-feedparser');
var request = require('request');
var fs = require('fs');
var level = require('level');
var db = level('db')
var mediaDir = '';
fs.exists('media/', function(exists) {
if (exists) {
mediaDir = 'media/';
}
});
/*
* Helper functions for use in identifying and downloading feeds/articles
*/
// Download media at specified URL and set a ticker to indicate activity.
function downloadMedia(mediaUrl, title) {
var media = request(mediaUrl).pipe(fs.createWriteStream(mediaDir+title));
console.log('Downloading ' + title);
// Set 15sec ticker to indicate download activity.
var waitTick = setInterval(function() {
console.log("...");
}, 15000);
media.on('finish', function() {
clearInterval(waitTick);
console.log(title + ' is done!');
});
}
// Get and return article from array, by date.
function getByDate(date, articles) {
var article;
for (var i=0; i< articles.length; i++) {
// Check if the date of article[i] is a match.
if (articles[i].pubDate.toISOString().slice(0,10) == date) {
article = articles[i];
}
}
return article;
}
// Determine media type and set filename accordingly.
function getMediaType(article, meta) {
var extension = article.enclosures[0].type;
var date = article.pubDate.toISOString().slice(0,10);
// Set extension to the filetype in .type, and ensure filename is lower case.
extension = '.' + extension.slice(extension.indexOf('/')+1, extension.length);
var title = meta.title + '-' + date + extension;
title = title.replace(/\s+/g, '').toLowerCase();
return title;
}
/*
* Module methods
*/
// Default function for module, returning metadata of specified feed.
function podcatcher(feedUrl, cb) {
feedparser.parseUrl(feedUrl, function(err, meta, articles) {
if (err) {
return cb(err);
} else {
return cb(null, meta, articles);
}
});
}
// Same functionality as podcatcher(), added for consistency with other methods.
podcatcher.getAll = podcatcher;
// Get the latest media in the specified podcast stream.
podcatcher.getNewest = function(feedUrl, cb) {
// Callback to locate the newest media and download it.
function getArticle(err, meta, articles) {
var article = articles[0];
if (meta) {
return cb(null, meta, article);
} else if (err) {
return cb(err);
}
}
feedparser.parseUrl(feedUrl, getArticle);
};
podcatcher.downloadNewest = function(feedUrl, cb) {
// Callback to download media.
function download(err, meta, article) {
var fileName = getMediaType(article, meta);
downloadMedia(article.enclosures[0].url, fileName);
if (err) {
return cb(err);
} else {
return cb(null, meta, article);
}
}
// Get the feed and issue callback.
this.getNewest(feedUrl, download);
};
// Get the specified article in the specified podcast stream and download it.
podcatcher.getByDate = function(feedUrl, date, cb) {
// Callback to locate the media at date and download it.
function getArticle(err, meta, articles) {
var article = getByDate(date, articles);
if (!cb) return articles;
if (meta) {
return cb(null, meta, article);
} else if (err) {
return cb(err);
}
}
// Parse feed and download in parser's callback.
feedparser.parseUrl(feedUrl, getArticle);
};
podcatcher.downloadByDate = function(feedUrl, date, cb) {
// Callback to download media.
function download(err, meta, article) {
var fileName = getMediaType(article, meta);
downloadMedia(article.enclosures[0].url, fileName);
if (!cb) return;
if (err) {
return cb(err);
} else {
return cb(null, meta, article);
}
}
// Find the feed and issue the callback.
this.getByDate(feedUrl, date, download);
}
/*
* Methods for dealing with LevelDB
*/
function getFeed(feed, cb) {
db.get(feed, function(err, res) {
if (!cb) return res;
if (err) return cb(err);
return cb(null, res);
})
}
function putFeed(feed, url, cb) {
db.put(feed, url, function(err) {
if (!cb) return;
if (err) return cb(err);
return cb(null, 'Feed saved successfully as ' + '\"' + feed + '\"');
});
}
// Return everything in db.
function getDBContents(cb) {
var res = [];
db.createReadStream().on('data', function(data) {
res.push(data);
}).on('error', function(err) {
return cb(err);
}).on('end', function() {
return cb(null, res);
})
}
podcatcher.putFeed = putFeed;
podcatcher.getDB = getDBContents;
podcatcher.getFeed = getFeed;
module.exports = podcatcher;