forked from vmagro/playmusic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.js
880 lines (805 loc) · 28.7 KB
/
play.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
/* Node-JS Google Play Music API
*
* Written by Jamon Terrell <git@jamonterrell.com>
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Based partially on the work of the Google Play Music resolver for Tomahawk (https://github.com/tomahawk-player/tomahawk-resolvers/blob/master/gmusic/content/contents/code/gmusic.js)
* and the gmusicapi project by Simon Weber (https://github.com/simon-weber/Unofficial-Google-Music-API/blob/develop/gmusicapi/protocol/mobileclient.py).
*/
var https = require('https');
var querystring = require('querystring');
var url = require('url');
var CryptoJS = require("crypto-js");
var uuid = require('node-uuid');
var util = require('util');
var crypto = require('crypto');
var encryptLogin = require('./lib/encryptLogin');
var pmUtil = {};
pmUtil.parseKeyValues = function(body) {
var obj = {};
body.split("\n").forEach(function(line) {
var pos = line.indexOf("=");
if(pos > 0) obj[line.substr(0, pos)] = line.substr(pos+1);
});
return obj;
};
pmUtil.Base64 = {
_map: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
stringify: CryptoJS.enc.Base64.stringify,
parse: CryptoJS.enc.Base64.parse
};
pmUtil.salt = function(len) {
return Array.apply(0, Array(len)).map(function() {
return (function(charset){
return charset.charAt(Math.floor(Math.random() * charset.length));
}('abcdefghijklmnopqrstuvwxyz0123456789'));
}).join('');
};
var PlayMusic = function() {};
PlayMusic.prototype._baseURL = 'https://www.googleapis.com/sj/v1.11/';
PlayMusic.prototype._webURL = 'https://play.google.com/music/';
PlayMusic.prototype._mobileURL = 'https://android.clients.google.com/music/';
PlayMusic.prototype._accountURL = 'https://www.google.com/accounts/';
PlayMusic.prototype._authURL = 'https://android.clients.google.com/auth';
PlayMusic.prototype.request = function(options, callback) {
var opt = url.parse(options.url);
opt.headers = {};
opt.method = options.method || "GET";
if(typeof options.options === "object") {
Object.keys(options.options).forEach(function(k) {
opt[k] = options.options[k];
});
}
if(typeof this._token !== "undefined") opt.headers.Authorization = "GoogleLogin auth=" + this._token;
opt.headers["Content-type"] = options.contentType || "application/x-www-form-urlencoded";
var req = https.request(opt, function(res) {
res.setEncoding('utf8');
var body = "";
res.on('data', function(chunk) {
body += chunk;
});
res.on('end', function() {
var err;
if(res.statusCode >= 400) {
err = new Error(res.statusCode + " error from server");
err.statusCode = res.statusCode;
err.response = res;
}
var contentType = (typeof res.headers["content-type"] !== "string") ? null : res.headers["content-type"].split(";", 1)[0].toLowerCase();
var response = body;
try {
if(contentType === "application/json") {
response = JSON.parse(response);
}
} catch (e) {
if(typeof callback === "function") callback(new Error("unable to parse json response: " + e), null, res);
}
if(typeof callback === "function") callback(err, response, res);
});
res.on('error', function(error) {
var err = new Error("Error making https request");
err.error = error;
err.response = res;
if(typeof callback === "function") callback(err, body, res);
});
});
req.on('error',(err)=>{
if(typeof callback === "function") callback(err);
});
if(typeof options.data !== "undefined") req.write(options.data);
req.end();
};
PlayMusic.prototype.init = function(config, callback) {
var that = this;
this._email = config.email;
this._password = config.password;
this._masterToken = config.masterToken;
// load signing key
var s1 = CryptoJS.enc.Base64.parse('VzeC4H4h+T2f0VI180nVX8x+Mb5HiTtGnKgH52Otj8ZCGDz9jRWyHb6QXK0JskSiOgzQfwTY5xgLLSdUSreaLVMsVVWfxfa8Rw==');
var s2 = CryptoJS.enc.Base64.parse('ZAPnhUkYwQ6y5DdQxWThbvhJHN8msQ1rqJw0ggKdufQjelrKuiGGJI30aswkgCWTDyHkTGK9ynlqTkJ5L4CiGGUabGeo8M6JTQ==');
for(var idx = 0; idx < s1.words.length; idx++) {
s1.words[idx] ^= s2.words[idx];
}
this._key = s1;
this._oauth(function(err, data) {
if(err) return callback(new Error("Unable to create oauth token" + err));
that._token = data.Auth;
that.getSettings(function(err, response) {
if(err) return callback(new Error("Login Failed, unable to load settings:" + err));
that._settings = response.settings;
that._allAccess = response.settings.entitlementInfo.isSubscription;
var devices = response.settings.uploadDevice.filter(function(d) {
return d.deviceType === 2 || d.deviceType === 3;
});
if(devices.length > 0) {
var id = devices[0].id;
if (devices[0].deviceType === 2) {
id = id.slice(2);
}
that._deviceId = id;
if(typeof callback === "function") callback();
} else {
if(typeof callback === "function") callback();
}
});
});
};
PlayMusic.prototype._oauth = function (callback) {
var that = this;
var data = {
accountType: "HOSTED_OR_GOOGLE",
has_permission: 1,
service: "sj",
source: "android",
androidId: that._androidId,
app: "com.google.android.music",
device_country: "us",
operatorCountry: "us",
//client_sig: "61ed377e85d386a8dfee6b864bd85b0bfaa5af81",
lang: "en",
sdk_version: "17"
};
if(this._masterToken) {
data.Token = this._masterToken;
} else if(this._password) {
data.EncryptedPasswd = encryptLogin(this._email, this._password),
data.Email = this._email;
} else {
callback(new Error("You must provide either an email address and password, or a token"));
}
this.request({
method: "POST",
url: that._authURL,
contentType: "application/x-www-form-urlencoded",
data: querystring.stringify(data),
headers: {
//Authorization: "GoogleLogin auth=" + that._master_token
}
}, function(err, data) {
callback(err, err ? null : pmUtil.parseKeyValues(data));
});
};
PlayMusic.prototype.login = function (opt, callback) {
opt.email = opt.email.trim(); // Trim immediately
var that = this;
opt.androidId = opt.androidId || crypto.pseudoRandomBytes(8).toString("hex");
var data = {
accountType: "HOSTED_OR_GOOGLE",
Email: opt.email,
has_permission: "1",
add_account: "1",
EncryptedPasswd: encryptLogin(opt.email, opt.password),
service: "ac2dm",
source: "android",
androidId: opt.androidId,
device_country: "us",
operatorCountry: "us",
lang: "en",
sdk_version: "17"
};
this.request({
method: "POST",
url: this._authURL,
contentType: "application/x-www-form-urlencoded",
data: querystring.stringify(data)
}, function(err, data) {
var response = pmUtil.parseKeyValues(data);
callback(err, err ? null : {androidId: opt.androidId, masterToken: response.Token});
});
};
/**
* Returns settings / device ids authorized for account.
*
* @param callback function(err, settings) - success callback
*/
PlayMusic.prototype.getSettings = function(callback) {
var that = this;
this.request({
method: "POST",
url: this._webURL + "services/fetchsettings?" + querystring.stringify({u: 0}),
contentType: "application/json",
data: JSON.stringify({"sessionId": ""})
}, function(err, body) {
if(err) return callback(new Error("error loading settings: " + err), body);
// loadsettings returns text/plain even though it's json, so we have to manually parse it.
var response;
try {
response = JSON.parse(body);
} catch (e) {
callback(new Error("error parsing settings: " + e), body);
}
callback(null, response);
});
};
/**
* Returns list of all tracks
*
* @param callback function(err, trackList) - success callback
*/
PlayMusic.prototype.getLibrary = PlayMusic.prototype.getAllTracks = function(opts, callback) {
var that = this;
// If first parameter is a callback, shift it over to the second param
if (typeof opts === "function") {
callback = opts;
opts = {};
}
// Set options defaults
opts.limit = opts.limit || 1000;
// Request body data
var data = { "max-results": opts.limit };
// Add 'start-token' if a continuation token was provided
if (!!opts.nextPageToken) {
data["start-token"] = opts.nextPageToken;
}
this.request({
method: "POST",
url: this._baseURL + "trackfeed",
contentType: "application/json",
data: JSON.stringify(data)
}, function(err, body) {
if(err) return callback(new Error("error getting library: " + err), body);
callback(null, body);
});
};
/**
* Returns stream URL for a track.
*
* @param id string - track id, hyphenated is preferred, but "nid" will work for all access tracks (not uploaded ones)
* @param callback function(err, streamUrl) - success callback
*/
PlayMusic.prototype.getStreamUrl = function (id, callback) {
if(!this._deviceId) {
callback(new Error("Unable to find a usable device on your account, access from a mobile device and try again"));
return;
}
var that = this;
var salt = pmUtil.salt(13);
var sig = CryptoJS.HmacSHA1(id + salt, this._key).toString(pmUtil.Base64);
var qp = {
u: "0",
net: "wifi",
pt: "e",
targetkbps: "8310",
slt: salt,
sig: sig
};
if(id.charAt(0) === "T") {
qp.mjck = id;
} else {
qp.songid = id;
}
var qstring = querystring.stringify(qp);
this.request({
method: "GET",
url: this._mobileURL + 'mplay?' + qstring,
options: { headers: { "X-Device-ID": that._deviceId } }
}, function(err, data, res) {
if(res.statusCode === 302 && typeof res.headers.location === "string") {
callback(null, res.headers.location);
} else {
callback(new Error("Unable to get stream url" + err), res.headers.location);
}
});
};
/**
* Opens and returns a stream object
*
* @param id string - track id, hyphenated is preferred, but "nid" will work for all access tracks (not uploaded ones)
* @param callback function(stream) - success callback
*/
PlayMusic.prototype.getStream = function(id, callback) {
this.getStreamUrl(id, function(err, url) {
if(err) return callback(err);
https.get(url, function(stream) {
callback(null, stream);
})
})
}
/**
* Searches for All Access tracks.
*
* @param text string - search text
* @param maxResults int - max number of results to return
* @param callback function(err, searchResults) - success callback
*/
PlayMusic.prototype.search = function (text, maxResults, callback) {
var that = this;
var qp = {
q: text,
ct: '1,2,3,4,5,6,7,8,9',
"max-results": maxResults
};
var qstring = querystring.stringify(qp);
this.request({
method: "GET",
url: this._baseURL + 'query?' + qstring
}, function(err, data) {
callback(err ? new Error("error getting search results: " + err) : null, data);
});
};
/**
* Returns list of all playlists.
*
* @param callback function(err, playlists) - success callback
*/
PlayMusic.prototype.getPlayLists = function (callback) {
var that = this;
this.request({
method: "POST",
url: this._baseURL + 'playlistfeed'
}, function(err, body) {
callback(err ? new Error("error getting playlist results: " + err) : null, body);
});
};
/**
* Creates a new playlist
*
* @param playlistName string - the playlist name
* @param callback function(err, mutationStatus) - success callback
*/
PlayMusic.prototype.addPlayList = function (playlistName, callback) {
var that = this;
var mutations = [
{
"create": {
"creationTimestamp": -1,
"deleted": false,
"lastModifiedTimestamp": 0,
"name": playlistName,
"type": "USER_GENERATED"
}
}
];
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'playlistbatch?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"mutations": mutations})
}, function(err, body) {
callback(err ? new Error("error creating playlist " + err) : null, body);
});
};
/**
* Deletes a playlist
*
* @param playlistId string - the playlist id
* @param callback function(err, mutationStatus) - success callback
*/
PlayMusic.prototype.deletePlayList = function (playlistId, callback) {
var that = this;
var mutations = [
{
"delete": playlistId
}
];
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'playlistbatch?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"mutations": mutations})
}, function(err, body) {
callback(err ? new Error("error creating playlist " + err) : null, body);
});
};
/**
* Updates a playlist's metadata
*
* @param playlistId string - the playlist id
* @param updates object - data to update the playlist with
* @param callback function(err, mutationStatus) - success callback
*/
PlayMusic.prototype.updatePlayListMeta = function (playlistId, updates, callback) {
var that = this;
var mutations = [
{
"update": {
"id": playlistId,
"name": updates.name || null,
"description": updates.description || null,
"shareState": updates.shareState || null
}
}
];
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'playlistbatch?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"mutations": mutations})
}, function(err, body) {
callback(err ? new Error("error updating playlist metadata " + err) : null, body);
});
};
/**
* Adds a track to a playlist. By default the track is added to the end.
* Optionally pass entryBeforeClientId and addAfterClientId to specify a specific position.
*
* @param songId string - the song id. Or an array of song ids
* @param playlistId int - the playlist id
* @param callback function(err, mutationStatus) - success callback
* @param entryBeforeClientId - optional clientId of playlist entry where to add song after
* @param entryAfterClientId - optional clientId of playlist entry where to add song before
*/
PlayMusic.prototype.addTrackToPlayList = function (songIds, playlistId, callback, entryBeforeClientId, entryAfterClientId) {
var that = this;
var songIdsArray = Array.isArray(songIds) ? songIds : [songIds];
var mutations = [];
var prevId, currId, nextId;
prevId = entryBeforeClientId || null;
currId = uuid.v1();
nextId = uuid.v1();
songIdsArray.forEach(function(songId, index) {
var mutation = {
"clientId": currId,
"creationTimestamp": "-1",
"deleted": "false",
"lastModifiedTimestamp": "0",
"playlistId": playlistId,
"source": (songId.indexOf("T") === 0 ? "2" : "1"),
"trackId": songId
}
if (prevId) {
mutation.precedingEntryId = prevId;
}
if (index < songIdsArray.length - 1) {
mutation.followingEntryId = nextId;
} else if (entryAfterClientId){
mutation.followingEntryId = entryAfterClientId;
}
mutations.push(
{
"create": mutation
}
);
prevId = currId;
currId = nextId;
nextId = uuid.v1();
});
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'plentriesbatch?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"mutations": mutations})
}, function(err, body) {
callback(err ? new Error("error adding tracks to playlist: " + err) : null, body);
});
};
/**
* Move the specified entry inbetween two specified entries (identified by their clientIds)
*
* @param callback function(err, playlistEntries) - success callback
*/
PlayMusic.prototype.movePlayListEntry = function(entryToMove, entryBeforeClientId, entryAfterClientId, callback) {
var that = this;
entryToMove = JSON.parse(JSON.stringify(entryToMove)); // clone fo-real
var keysToKeep = ['clientId', 'creationTimestamp', 'deleted', 'id', 'lastModifiedTimestamp', 'playlistId', 'source', 'trackId'];
for(var entry in entryToMove) {
if (keysToKeep.indexOf(entry) == -1)
delete entryToMove[entry];
}
if (entryBeforeClientId){
entryToMove['precedingEntryId'] = entryBeforeClientId;
};
if (entryAfterClientId){
entryToMove['followingEntryId'] = entryAfterClientId;
}
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'plentriesbatch?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"mutations" : [{ "update": entryToMove}]})
}, function(err, body) {
callback(err ? new Error("error getting moving playlist entry: " + err) : null, body);
});
};
/**
* Increments track's playcount
*
* @param songId int - the song id. See http://bit.ly/1L4U6oK for id requirements.
* @param callback function(err, mutationStatus) - success callback
*/
PlayMusic.prototype.incrementTrackPlaycount = function (songId, callback) {
var that = this;
var stats = [
{
"id": songId,
"incremental_plays": "1",
"last_play_time_millis": Date.now().toString(),
"type": (songId.indexOf("T") === 0 ? "2" : "1"),
"track_events": []
}
];
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'trackstats?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"track_stats": stats})
}, function(err, body) {
callback(err ? new Error("error incrementing track playcount: " + err) : null, body);
});
};
/* Change metadata of a track a library
* Currently only support changing rating
* You need to change a song object with a different rating value:
* 5 = thumb up, 1 = thumb down, 0 = no thumb
* @param song object - the track dictionnary. You can get from getAllAccessTrack or from getLibrary
* @param callback function(err, success) - success callback
*/
PlayMusic.prototype.changeTrackMetadata = function (song, callback) {
var that = this;
var mutations = [ { "update": song } ];
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'trackbatch?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"mutations": mutations})
}, function(err, body) {
callback(err ? new Error("error changing rating track: " + err) : null, body);
});
}
/**
* Removes given entry ids from playlist entries
*
* @param entryId string - the entry id. Or an array of entry ids. You can get this from getPlayListEntries
* @param callback function(err, mutationStatus) - success callback
*/
PlayMusic.prototype.removePlayListEntry = function (entryIds, callback) {
var that = this;
var entryIdsArray = Array.isArray(entryIds) ? entryIds : [entryIds];
var mutations = [];
entryIdsArray.forEach(function(entryId) {
mutations.push({ "delete": entryId });
});
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'plentriesbatch?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"mutations": mutations})
}, function(err, body) {
callback(err ? new Error("error removing playlist entries: " + err) : null, body);
});
};
/**
* Returns tracks on all playlists.
*
* @param opts Object - parameters
* @param callback function(err, playlistEntries) - success callback
*/
PlayMusic.prototype.getPlayListEntries = function (opts, callback) {
var that = this;
// If first parameter is a callback, shift it over to the second param
if (typeof opts === "function") {
callback = opts;
opts = {};
}
// Set options defaults
opts.limit = opts.limit || 1000;
// Request body data
var data = {"max-results": opts.limit};
// Add 'start-token' if a continuation token was provided
if (!!opts.nextPageToken) {
data["start-token"] = opts.nextPageToken;
}
this.request({
method: "POST",
url: this._baseURL + 'plentryfeed',
contentType: "application/json",
data: JSON.stringify(data)
}, function (err, body) {
callback(err ? new Error("error getting playlist results: " + err) : null, body);
});
};
/**
* Returns tracks on shared playlist.
*
* @param opts Object - parameters
* @param callback function(err, playlistEntries) - success callback
*/
PlayMusic.prototype.getSharedPlayListEntries = function (opts, callback) {
var that = this;
// If first parameter is a callback, shift it over to the second param
if (typeof opts === "function") {
callback = opts;
opts = {};
}
// Set options defaults
opts.limit = opts.limit || 1000;
// Request body data
var data = {"max-results": opts.limit};
// Add 'start-token' if a continuation token was provided
if (!!opts.nextPageToken) {
data["start-token"] = opts.nextPageToken;
}
data['shareToken'] = opts.shareToken;
this.request({
method: "POST",
url: this._baseURL + 'plentries/shared',
contentType: "application/json",
data: JSON.stringify({
'entries': [data]
})
}, function (err, body) {
callback(err ? new Error("error getting playlist results: " + err) : null, body);
});
};
/**
* Returns info about an All Access album. Does not work for uploaded songs.
*
* @param albumId string All Access album "nid" -- WILL NOT ACCEPT album "id" (requires "T" id, not hyphenated id)
* @param includeTracks boolean -- include track list
* @param callback function(err, albumList) - success callback
*/
PlayMusic.prototype.getAlbum = function (albumId, includeTracks, callback) {
var that = this;
this.request({
method: "GET",
url: this._baseURL + "fetchalbum?" + querystring.stringify({nid: albumId, "include-tracks": includeTracks, alt: "json"})
}, function(err, body) {
callback(err ? new Error("error getting album tracks: " + err) : null, body);
});
};
/**
* Returns info about an All Access track. Does not work for uploaded songs.
*
* @param trackId string All Access track "nid" -- WILL NOT ACCEPT track "id" (requires "T" id, not hyphenated id)
* @param callback function(err, trackInfo) - success callback
*/
PlayMusic.prototype.getAllAccessTrack = function (trackId, callback) {
var that = this;
this.request({
method: "GET",
url: this._baseURL + "fetchtrack?" + querystring.stringify({nid: trackId, alt: "json"})
}, function(err, body) {
callback(err ? new Error("error getting all access track: " + err) : null, body);
});
};
/**
* Returns Artist Info, top tracks, albums, related artists
*
* @param artistId string - not sure which id this is
* @param includeAlbums boolean - should album list be included in result
* @param topTrackCount int - number of top tracks to return
* @param relatedArtistCount int - number of related artists to return
* @param callback function(err, artistInfo) - success callback
*/
PlayMusic.prototype.getArtist = function (artistId, includeAlbums, topTrackCount, relatedArtistCount, callback) {
var that = this;
this.request({
method: "GET",
url: this._baseURL + "fetchartist?" + querystring.stringify({
nid: artistId,
"include-albums": includeAlbums,
"num-top-tracks": topTrackCount,
"num-related-artists": relatedArtistCount,
alt: "json"
})
}, function(err, body) {
callback(err ? new Error("error getting artist info: " + err) : null, body);
});
};
/**
* Builds a seed object for use with createStation
*
* @param seedId string - a track, artist, album, or genre id
* @param type string - one of ["track", "artist", "album", "genre"]
* @return object - seed object for use with createStation
*/
PlayMusic.prototype._getSeed = function(seedId, type) {
var seed;
if(type === "track" && seedId.charAt(0) === "T") {
seed = {trackId: seedId, seedType: 2};
} else if(type === "track") {
seed = {trackId: seedId, seedType: 1};
} else if(type === "artist") {
seed = {artistId: seedId, seedType: 3};
} else if(type === "album") {
seed = {albumId: seedId, seedType: 4};
} else if(type === "genre") {
seed = {genreId: seedId, seedType: 5};
} else if(type === "station") {
seed = {curatedStationId: seedId, seedType: 9};
}
return seed;
};
/**
* Returns list of existing stations
*
* @param callback function(err, stationInfo) - success callback
*/
PlayMusic.prototype.getStations = function(callback) {
var that = this;
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'radio/station'
//data: JSON.stringify(obj)
}, function(err, body) {
callback(err ? new Error("error listing stations: " + err) : null, body);
});
};
/**
* Creates a new station
*
* @param name string - name of new station
* @param seedId string - a track, artist, album, or genre id
* @param type string - one of ["track", "artist", "album", "genre"]
* @param callback function(err, mutationStatus) - success callback
*/
PlayMusic.prototype.createStation = function(name, seedId, type, callback) {
var that = this;
var seed = this._getSeed(seedId, type);
if(!seed) return callback(new Error("Invalid Seed type"));
var mutations = [
{
"createOrGet": {
"clientId": uuid.v1(),
"deleted": false,
"imageType": 1,
"lastModifiedTimestamp": "-1", // + (new Date()).valueOf()*1000,
"name": name,
"recentTimeStamp": "" + (new Date()).valueOf()*1000,
"seed": seed,
"tracks": []
},
"includeFeed": false,
"numEntries": 0,
"params": { "contentFilter": 1 }
}
];
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'radio/editstation?' + querystring.stringify({alt: "json"}),
data: JSON.stringify({"mutations": mutations})
}, function(err, body) {
callback(err ? new Error("error creating station: " + err) : null, body);
});
};
/**
* Gets a list of tracks for a given station id
*
* @param stationId string - id of station
* @param tracks int - number of tracks to return
* @param callback function(err, stationInfo) - success callback
*/
PlayMusic.prototype.getStationTracks = function(stationId, tracks, callback) {
var that = this;
var obj = {
"contentFilter": 1,
"stations": [{
"radioId": stationId,
"numEntries": tracks,
"recentlyPlayed": []
}]
};
this.request({
method: "POST",
contentType: "application/json",
url: this._baseURL + 'radio/stationfeed?' + querystring.stringify({alt: "json", "include-tracks": "true"}),
data: JSON.stringify(obj)
}, function(err, body) {
callback(err ? new Error("error getting station tracks: " + err) : null, body);
});
};
PlayMusic.prototype.getFavorites = function(callback) {
this.request({
method: "POST",
contentType: "application/json",
url: this._webURL + 'services/getephemthumbsup'
}, function(err, body) {
if (err) {
return callback(err);
}
try {
body = JSON.parse(body);
} catch (err) {
return callback(err);
};
callback(null, body);
});
};
// So we don't break things using "getFavotites"
PlayMusic.prototype.getFavotites = PlayMusic.prototype.getFavorites;
module.exports = exports = PlayMusic;