Skip to content

Commit af66da2

Browse files
committed
server track downloader
1 parent c0d4281 commit af66da2

File tree

3 files changed

+138
-2
lines changed

3 files changed

+138
-2
lines changed

bootstrap.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1-
var audioDataAnalyzer = require('audio_data_analyzer');
1+
var AudioDataAnalyzer = require('./library/audio_data_analyzer').analyzer;
22

3-
var values = audioDataAnalyzer.getValues();
3+
var TrackDownloader = require('./library/track_downloader').downloader;
4+
5+
var audioDataAnalyzer = new AudioDataAnalyzer();
6+
7+
var trackDownloader = new TrackDownloader();
8+
9+
trackDownloader.writeTrackToDisc(415208);
10+
11+
12+
13+
var values = audioDataAnalyzer.getValues();
14+
15+
console.log(values);

library/audio_data_analyzer.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
*
3+
* @returns {analyzerConstructor}
4+
*/
5+
var analyzer = function analyzerConstructor() {
6+
7+
};
8+
9+
/**
10+
*
11+
* @returns {undefined}
12+
*/
13+
analyzer.prototype.getValues = function getValuesFunction() {
14+
15+
16+
17+
};
18+
19+
module.exports.analyzer = analyzer;

library/track_downloader.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
*
3+
* @returns {downloaderConstructor}
4+
*/
5+
var downloader = function downloaderConstructor() {
6+
7+
};
8+
9+
/**
10+
*
11+
* fetches a track from jamendo.com and writes it into the temporary folder
12+
* on disc
13+
*
14+
* @param {type} trackId
15+
* @param {type} temporaryTracksDirecotry
16+
* @param {type} format
17+
* @returns {undefined}
18+
*/
19+
downloader.prototype.writeTrackToDisc = function(trackId, temporaryTracksDirecotry, format) {
20+
21+
if (temporaryTracksDirecotry === undefined) {
22+
23+
temporaryTracksDirecotry = '';
24+
25+
}
26+
27+
if (format === undefined) {
28+
29+
format = 'ogg';
30+
31+
}
32+
33+
var formatCode;
34+
35+
switch(format) {
36+
case 'mp3':
37+
formatCode = 'mp31';
38+
break;
39+
case 'ogg':
40+
formatCode = 'ogg1';
41+
break;
42+
default:
43+
throw 'unsupported track format';
44+
}
45+
46+
var trackPath = temporaryTracksDirecotry + '/' + trackId + '.' + format;
47+
48+
//
49+
var options = {
50+
hostname: 'storage-new.newjamendo.com',
51+
port: 80,
52+
path: '/download/track/' + trackId + '/' + formatCode,
53+
method: 'GET'
54+
};
55+
56+
var writeStream = fs.createWriteStream(trackPath);
57+
58+
writeStream.on('open', function() {
59+
60+
var httpRequest = http.request(options, function(httpResponse) {
61+
62+
console.log('writeTrackToDisc httpRequest STATUS: ' + httpResponse.statusCode);
63+
console.log('writeTrackToDisc httpRequest HEADERS: ' + JSON.stringify(httpResponse.headers));
64+
65+
httpResponse.on('data', function(chunk) {
66+
67+
writeStream.write(chunk);
68+
69+
});
70+
71+
httpResponse.on('end', function() {
72+
73+
console.log('file ' + trackFileName + ' got downloaded into ' + trackPath);
74+
75+
writeStream.end();
76+
77+
moveTrack();
78+
79+
});
80+
81+
});
82+
83+
httpRequest.on('error', function(error) {
84+
85+
console.log('writeTrackToDisc, http request error: ' + error.message);
86+
87+
writeStream.end();
88+
89+
});
90+
91+
httpRequest.end();
92+
93+
});
94+
95+
writeStream.on('error', function(error) {
96+
97+
console.log('writeTrackToDisc writeStream, error: ' + error);
98+
99+
writeStream.end();
100+
101+
});
102+
103+
};
104+
105+
module.exports.downloader = downloader;

0 commit comments

Comments
 (0)