-
Notifications
You must be signed in to change notification settings - Fork 31
/
index.js
208 lines (193 loc) · 5.21 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict';
var osascript = require('osascript').eval;
var stringify = require('js-function-string');
var logSymbols = require('log-symbols');
var chalk = require('chalk');
var requireDir = require('require-dir');
var lib = requireDir('./lib');
var osascriptOpts = {
flags: ['-s', 's']
};
function startPlayback(callback) {
// Run JavaScript file through OSA
osascript(stringify(lib.play.method), function (err) {
var result;
if (err === null) {
result = callback(logSymbols.success + ' Playing ♪♬');
} else {
result = callback(logSymbols.error + ' ' + chalk.red(err));
}
return result;
});
}
function stopPlayback(callback) {
// Run JavaScript file through OSA
osascript(stringify(lib.stop.method), function (err) {
var result;
if (err === null) {
result = callback(logSymbols.success + ' Stopped playing ♪♬');
} else {
result = callback(logSymbols.error + ' ' + chalk.red(err));
}
return result;
});
}
function pausePlayback(callback) {
// Run JavaScript file through OSA
osascript(stringify(lib.pause.method), function (err) {
var result;
if (err === null) {
result = callback(logSymbols.success + ' Paused playing ♪♬');
} else {
result = callback(logSymbols.error + ' ' + chalk.red(err));
}
return result;
});
}
function nextTrack(callback) {
// Run JavaScript file through OSA
osascript(stringify(lib.next.method), function (err) {
var result;
if (err === null) {
result = callback(logSymbols.success + ' Skipped track.');
} else {
result = callback(logSymbols.error + ' ' + chalk.red(err));
}
return result;
});
}
function prevTrack(callback) {
// Run JavaScript file through OSA
osascript(stringify(lib.previous.method), function (err) {
var result;
if (err === null) {
result = callback(logSymbols.success + ' Returned to previous track.');
} else {
result = callback(logSymbols.error + ' ' + chalk.red(err));
}
return result;
});
}
function backTrack(callback) {
// Run JavaScript file through OSA
osascript(stringify(lib.back.method), function (err) {
var result;
if (err === null) {
result = callback(logSymbols.success + ' Returned to previous track or beginning of current track.');
} else {
result = callback(logSymbols.error + ' ' + chalk.red(err));
}
return result;
});
}
function startPlaylist(playlist, amount, callback) {
// Run JavaScript file through OSA
osascript(stringify(lib.play.method).replace(/{{playlist}}/, playlist), function (err) {
var result;
if (err === null) {
result = callback(logSymbols.success + ' Playing ' + amount + ' song' + (amount > 1 ? 's' : '') + ' ♪♬');
} else {
result = callback(logSymbols.error + ' ' + chalk.red(err));
}
return result;
});
}
function startSearch(searchTerm, opts, callback) {
var limitTo = 'all';
var messagePart = 'songs, albums and artists containing ”';
if (opts.songs) {
limitTo = 'songs';
messagePart = 'songs containing ”';
}
if (opts.artists) {
limitTo = 'artists';
messagePart = 'songs by ”';
}
if (opts.albums) {
limitTo = 'albums';
messagePart = 'album containing ”';
}
// Run JavaScript file through OSA
osascript(stringify(lib.search.method)
.replace(/{{searchTerm}}/, searchTerm)
.replace(/{{limitTo}}/, limitTo), osascriptOpts, function (err, data) {
data = JSON.parse(data);
var playlist = data[0];
var amount = parseInt(data[1], 10);
var result;
if (err === null && amount) {
if (!opts['dont-play']) {
startPlaylist(playlist, amount, function (response) {
result = callback(response);
});
}
result = callback(logSymbols.success + ' Found ' + messagePart +
chalk.inverse(searchTerm) + '“ and generated a temporary playlist');
} else if (err === null) {
result = callback(logSymbols.error + ' Oops. Found 0 ' + messagePart +
chalk.inverse(searchTerm) + '“.');
} else {
result = callback(logSymbols.error + ' ' + chalk.red(err));
}
return result;
});
}
function getData(callback) {
// Run JavaScript file through OSA
osascript(stringify(lib.getLibraryData.method), osascriptOpts, function (err, data) {
var result;
if (err === null) {
data = JSON.parse(data);
result = callback(data);
} else {
result = callback(logSymbols.error + ' ' + chalk.red(err));
}
return result;
});
}
module.exports = function (command, callback, args) {
switch (command) {
case 'play':
startPlayback(function (response) {
return callback(response);
});
break;
case 'stop':
stopPlayback(function (response) {
return callback(response);
});
break;
case 'pause':
pausePlayback(function (response) {
return callback(response);
});
break;
case 'next':
nextTrack(function (response) {
return callback(response);
});
break;
case 'previous':
prevTrack(function (response) {
return callback(response);
});
break;
case 'back':
backTrack(function (response) {
return callback(response);
});
break;
case 'search':
startSearch(args.searchterm, args.options, function (response) {
return callback(response);
});
break;
case 'getData':
getData(function (response) {
return callback(response);
});
break;
default:
throw new Error('You have to call itunesRemote with a command');
}
};