/
EatDailyJam.js
executable file
·123 lines (111 loc) · 3.48 KB
/
EatDailyJam.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
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
#!/usr/bin/env node
/**
* Downloads the daily jam from DailyJam.co.nz. ./EatDailyJam -d /path/to/save/to
*/
"use strict";
var argv = require('argv'),
htmlparser = require('htmlparser2'),
http = require('http'),
fs = require('fs'),
savePath,
dailyJamHTML,
title,
artist;
function start() {
var pjson = require('./package.json');
console.log('Eat Daily Jam v ' + pjson.version);
argv.version(pjson.version);
argv.option({
name: 'directory',
short: 'd',
type: 'path',
description: 'Pot to put today\'s Jam in',
example: "'EatDailyJam --directory=/your/music/folder' or 'EatDailyJam -d \"/Users/adrian/Music/iTunes/iTunes Media/Automatically Add to iTunes\"'"
});
var args = argv.run();
if (!args || !args.options || !args.options.directory) {
console.log('Please supply a directory using the -d option');
argv.help();
process.exit(1);
} else {
savePath = args.options.directory;
fetchHTML();
}
}
function fetchHTML() {
console.log(' Saving jam to "' + savePath + '" folder.');
var options = {
host: 'dailyjam.co.nz',
path: '/'
};
console.log(' Reading dailyjam.co.nz...');
var jamMasterJay = http.request(options, function (res) {
var data = '';
res.on('data', function (chunk) {
data += chunk;
});
res.on('end', function () {
dailyJamHTML = data;
parseHTML();
});
});
jamMasterJay.on('error', function (e) {
console.log("Pooh. This error went down : " + e.message);
process.exit(1);
});
jamMasterJay.end();
}
function parseHTML() {
console.log(' Parsing...');
var grabTitle = false,
grabArtist = false,
fileURL;
var parser = new htmlparser.Parser({
onopentag: function (name, attribs) {
grabTitle = (attribs.class === 'trackTitle');
grabArtist = (attribs.class === 'artist');
if (attribs.class === 'download_link') {
fileURL = attribs.href;
}
},
ontext: function (text) {
if (grabTitle) {
title = text;
}
if (grabArtist) {
artist = text;
}
},
onclosetag: function () {
grabArtist = grabTitle = false;
}
});
parser.write(dailyJamHTML);
parser.end();
console.log(' Today\'s jam is ' + title + ' by ' + artist + '...');
downloadTrack('http://dailyjam.co.nz/' + fileURL, savePath, onComplete);
}
function downloadTrack(url, dest, cb) {
console.log(' Downloading... (be patient, this takes a wee while)');
var filename = url.substring(url.lastIndexOf('/'));
dest += filename.substring(dest[dest.length - 1] !== '/' ? 0 : 1);
var file = fs.createWriteStream(dest);
file.on('error', function (err) {
console.log('Doh. This happened writing the file: ' + err);
process.exit(3);
});
http.get(url,function (response) {
response.pipe(file);
file.on('finish', function () {
file.close(cb);
});
}).on('error', function (err) {
fs.unlink(dest);
console.log('Rats! This happened while downloading: ' + err.message);
process.exit(2);
});
}
function onComplete() {
console.log('Done! Check out ' + title + ' by ' + artist + ' at ' + savePath + ' and kick out the jam!');
}
start();