-
Notifications
You must be signed in to change notification settings - Fork 11
/
app.js
127 lines (99 loc) · 3.91 KB
/
app.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
122
123
124
125
126
127
var async = require('async'),
fs = require('fs'),
path = require('path');
var configPath = __dirname + '/config.json';
try {
var config = require(configPath);
}
catch (e){
var exist = fs.existsSync(configPath);
if(exist !== true){
fs.writeFileSync(configPath, '{}');
var config = require(configPath);
} else{
throw e;
}
}
var downloadPath = config.dirname;
var Spotify = require(__dirname + '/lib/spotify');
var argv = require('minimist')(process.argv.slice(2));
function switchCb(err) {
if (err)
console.log(err);
process.exit();
}
var cmd = (argv['h']) ? 'help' : argv._[0];
switch (cmd){
case 'help':
// Fuck dat this is dirty. https://www.youtube.com/watch?v=bzLvZMNHFvU
console.log('Commands:')
console.log('config - Sets your configuration')
console.log(' Options:')
console.log(' -u | --username | --user' + ' Set your spotify login username')
console.log(' -p | --password | --pass' + ' Set your spotify login password')
console.log(' -d | --dirname | --dir ' + ' Set download path')
console.log('track - Downloads a single track in preselected dirname')
console.log('spotijay track [spotify:trackUrl]\n')
console.log('playlist - Downloads a tracks inside playlist in preselected dirname')
console.log('spotijay playlist [spotify:playlistUrl]\n')
console.log('clean - Performs clean of non-converted raw audio files or album covers')
case 'config':
var user = argv['u'] || argv['username'] || argv['user'];
var pass = argv['p'] || argv['password'] || argv['pass'];
var dir = argv['d'] || argv['dir'] || argv['dirname'];
var copyDestiny = argv['c'] || argv['cop'] || argv['copy'];
var itunes = argv['itunes'];
var newConf = config;
newConf.username = user || newConf.username;
newConf.password = pass || newConf.password;
if(dir && typeof dir == 'string'){
if (dir == '.'){dir = process.cwd()} else if (dir.substring(0, 0) !== '/'){dir = process.cwd() + '/' + dir}
dir = path.resolve(dir);
console.log('Download path set to: ' + dir);
newConf.dirname = dir;
}
if(copyDestiny && typeof copyDestiny == 'string'){
if (copyDestiny == '.'){copyDestiny = process.cwd()} else if (copyDestiny.substring(0, 0) !== '/'){copyDestiny = process.cwd() + '/' + copyDestiny}
copyDestiny = path.resolve(copyDestiny);
console.log('Copy path: ' + copyDestiny);
newConf.copy = copyDestiny;
}
if(itunes == 'true'){
newConf.itunes = true;
} else if (itunes == 'false'){
newConf.itunes = false;
}
fs.writeFileSync(configPath, JSON.stringify(newConf));
process.exit();
break;
case 'playlist':
if(!argv._[1]){
console.log('You must provide a playlist url');
process.exit();
}
Spotify(config.username, config.password, function (err, spotify){
spotify.downloadPlaylistByUrl(argv._[1], downloadPath, switchCb);
});
break;
case 'track':
if(!argv._[1]){
console.log('You must provide a track url');
process.exit();
}
Spotify(config.username, config.password, function (err, spotify){
spotify.getTrackByUrl(argv._[1], downloadPath, switchCb);
});
break;
case 'search':
break;
case 'clean':
require('child_process').exec('cd '+ config.dirname +' && rm -rf *.png *.wav *.raw', function (err, stdout) {
console.log(err || 'Cleaned');
if(err) process.exit(1)
process.exit(0)
});
break;
default:
console.log('Use `$ spotijay help` to list different commands');
process.exit();
}