-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
75 lines (64 loc) · 2.15 KB
/
index.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
"use strict";
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
var path = require('path');
var scraper = require('./lib/scraper');
var argv = require('yargs')
.usage('Usage: $0 --id [num] --type [type_to_fetch] --plugin [dir]')
.option('id', {
alias: 'i',
demand: true,
describe: '1001tracklists id'
})
.option('type', {
alias: 't',
demand: true,
default: 'tracklist',
choices: ['tracklist', 'artist'],
describe: '1001tracklists type to fetch'
})
.option('plugin', {
alias: 'p',
demand: true,
describe: 'tracklist consumer',
choices: ['soundcloud']
})
.option('dir', {
alias: 'd',
demand: true,
default: '#tracklistId/',
describe: 'download destination'
})
.argv;
var fetchId = argv.id,
fetchType = argv.type,
plugin = argv.plugin,
pluginArgKey = plugin + '-',
pluginArgs = {},
pluginPath = './plugin/' + plugin,
downloadDestination = argv.dir,
shortUrl = 'http://1001.tl/' + fetchType + '/' + fetchId;
for (var argKey in argv) {
if (argv.hasOwnProperty(argKey) === false || argKey.indexOf(pluginArgKey) !== 0) {
continue;
}
pluginArgs[argKey] = argv[argKey];
}
downloadDestination = downloadDestination.replace(/#id/gi, fetchId);
if (path.isAbsolute(downloadDestination) === false) {
downloadDestination = path.normalize(__dirname + '/' + downloadDestination);
}
if (fs.existsSync(downloadDestination) === false) {
fs.mkdirSync(downloadDestination);
}
console.log('Requesting ' + shortUrl + ' ...');
scraper.scrape(shortUrl, function (err, tracks, tracklistName) {
if (err) {
console.error(err);
}
console.log('Got ' + tracks.length + ' tracks from:', tracklistName);
console.log('Loading "' + pluginPath + '" ..');
var plugin = require(pluginPath);
plugin.consume(tracks, downloadDestination, pluginArgs);
});