forked from PSpeiser/Portify.JS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathportify.js
954 lines (902 loc) · 31.1 KB
/
portify.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
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
//Portify.JS V1.04
//TODO
// Incorporate g-scrape.js to allow exporting FROM google play.
// Perhaps create a playlist sync feature. Check playlists on both systems and non destructive merge the two.
function addscript(url, cbname){ //Inject a javascript script into the dom
var script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.onload =function(){cbname()};
document.getElementsByTagName('head')[0].appendChild(script);
}
function addstyle(url){ //Inject a css stylesheet into the dom
var style = document.createElement('link');
style.rel = 'stylesheet';
style.href = url;
style.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(style);
}
function removejscssfile(filename, filetype){ //Remove a javascript or css element from the dom
var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none"; //determine element type to create nodelist from
var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" ; //determine corresponding attribute to test for
var allsuspects=document.getElementsByTagName(targetelement);
for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1){
allsuspects[i].parentNode.removeChild(allsuspects[i]); //remove element by calling parentNode.removeChild()
}
}
}
function doclick(el){ // Fake a click on an element the fancy way
var ev = document.createEvent("MouseEvent");
ev.initMouseEvent(
"mousedown",
true /* bubble */, true /* cancelable */,
window, null,
el.x+10, el.y+10, 0, 0, /* coordinates */
false, false, false, false, /* modifier keys */
0 /*left*/, null
);
el.dispatchEvent(ev);
var ev = document.createEvent("MouseEvent");
ev.initMouseEvent(
"mouseup",
true /* bubble */, true /* cancelable */,
window, null,
el.x+10, el.y+10, 0, 0, /* coordinates */
false, false, false, false, /* modifier keys */
0 /*left*/, null
);
el.dispatchEvent(ev);
}
function exportToCsv(filename, rows) {
var processRow = function (row) {
var finalVal = '';
for (var j = 0; j < row.length; j++) {
if (row[j] === undefined){
row[j] = '';
}
var innerValue = row[j] === null ? '' : row[j].toString();
if (row[j] instanceof Date) {
innerValue = row[j].toLocaleString();
};
var result = innerValue.replace(/"/g, '""');
if (result.search(/("|,|\n)/g) >= 0)
result = '"' + result + '"';
if (j > 0)
finalVal += ',';
finalVal += result;
}
return finalVal + '\n';
};
var csvFile = '';
for (var i = 0; i < rows.length; i++) {
csvFile += processRow(rows[i]);
}
var blob = new Blob([csvFile], { type: 'text/csv;charset=utf-8;' });
if (navigator.msSaveBlob) { // IE 10+
navigator.msSaveBlob(blob, filename);
} else {
var link = document.createElement("a");
if (link.download !== undefined) { // feature detection
// Browsers that support HTML5 download attribute
var url = URL.createObjectURL(blob);
link.setAttribute("href", url);
link.setAttribute("download", filename);
link.style.visibility = 'hidden';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}
}
function doloadstep(){ // Load in the required libraries in the proper order
switch (window.loadstep) {
case 0:
addstyle("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css");
addscript('https://code.jquery.com/jquery-1.11.0.min.js', doloadstep);
addscript(window.portifyURL + "/g-scrape.js", function(){});
break;
case 1:
addscript('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js', doloadstep);
jQuery.extend(jQuery.expr[":"], {
"containsNC": function(elem, i, match, array) {
return (elem.textContent || elem.innerText || "").toLowerCase().indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
break;
case 2:
addscript('https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js', dospotimport);
break;
}
window.loadstep = window.loadstep + 1;
}
function newPlaylist(name){ // Create new playlist by name.
jQuery('[data-id="create-playlist"]').click();
jQuery("paper-input.playlist-name").val(name);
setTimeout(newPlaylistSave, 1000);
}
function newPlaylistSave(){
jQuery("paper-button[data-action='save']").click();
alert("Test");
setTimeout(runPLPause, 1000);
}
function runPLPause(){ // Indicates that we can create the next playlist
window.plinfo['RunPL'] = true;
}
function addSongToPlaylist(search, playlist){ // Add a song to a playlist. Fires a search query and sets up pollfornewsong to poll for the search to complete
jQuery("button[name='add-duplicate-songs']").click();
clearwaves();
jQuery("sj-search-box")[0].fire('query', {query: search});
setTimeout(function() {
pollfornewsong(playlist);
}, 100);
window.pollcount = 0;
window.pollstart = new Date().getTime();
}
function pollfornewsong(playlist){ // Check if search has completed, call addSongCallback when it has
window.pollcount = window.pollcount + 1; // Add one to the poll count (used to timeout)
var repoll = true;
if (jQuery("#loadingIndicator:visible").length == 0){ // If our loading spinner not visible
addSongCallback(playlist); //Run the next step in the automation, clicking the song and adding it to the playlist
repoll = false; // Dont recheck anymore
}
if (window.pollcount > 59 || ((new Date().getTime() - pollstart)/1000) > 6){ //If we have waited too long
repoll = false; //Dont recheck
addSongCallback(playlist); //Try with whats on the page now
}
if (repoll == true){ // If we need to recheck
setTimeout(function() { //Recheck in a half sec
pollfornewsong(playlist);
}, 500);
}
}
function addSongCallback(playlist){ // Add the first song in a songlist-container on the page to a playlist
try{
jQuery("div.songlist-container").find("paper-icon-button[icon='more-vert']")[0].click(); //Click the more button on the song
jQuery("div.songlist-container").find("paper-icon-button[icon='more-vert']")[0].click(); //Run twice to fix buggyness
doclick(jQuery("div[class='goog-menuitem-content']:contains('Add to playlist')")[0]); //Click add to playlist on the submenu
doclick(jQuery("div.playlist-menu").find("div[role='menuitem']:contains('" + playlist + "')")[0]); //Click on the playlist that it needs to add to
} catch(err){
}
window.plinfo['RunSong'] = true; // Run the next song
}
function loadpls(){ // Grabs a playlist from plinfo['lists'] and loads the tracks for it. Runs startRunning() if all playlists have been loaded.
for(i in window.plinfo['lists']){ // Check our playlist list
pl = window.plinfo['lists'][i];
if (pl['loaded'] == false){ //If a playlist has not had its tracks loaded
switch (pl['mode']){ //Check its mode
case "link": //If its a link type playlist
loadPlByLink(pl); //Load the tracks for the playlist
break;
}
return; //Drop out of loadpls
}
} // Check for playlists with more tracks then 1000
rcb = [];
for(i in window.plinfo['lists']){
pl = window.plinfo['lists'][i];
if (pl['tracks'].length > 1000){
var i,j,temparray,chunk = 1000;
for (i=0,j=pl['tracks'].length; i<j; i+=chunk) {
temparray = pl['tracks'].slice(i,i+chunk);
newpl = {};
newpl['name'] = pl['name'] + "-" + ((i/chunk)+1).toString();
newpl['mode'] = pl['mode'];
newpl['prefix'] = pl['prefix'];
newpl['createdpl'] = pl['createdpl'];
newpl['loaded'] = pl['loaded'];
newpl['link'] = pl['link'];
newpl['tracks'] = temparray;
rcb.push(newpl);
}
} else{
rcb.push(pl);
}
}
window.plinfo['lists'] = rcb; // Replace the existing playlist list with the modified one that is split up if a playlist has more then 1000 songs.
startRunning(); //If all playlists are loaded, startRunning the automation
}
function loadPlByLink(i){ // Load a playlist by its url
getItems(i['link'], i['link'], plComplete, []);
}
function plComplete(dat, pllink){ // Called when a url loaded playlist has completed loading. Will set the tracks on the proper object in plinfo['lists'] and rerun loadpls()
for(i in window.plinfo['lists']){
pl = window.plinfo['lists'][i];
if (pl['link'] == pllink){
pl['loaded'] = true;
pl['tracks'] = dat;
}
}
loadpls();
}
function getSpotDirect(url, donecb, errorcb){
jQuery.ajax(url, {
dataType: 'json',
headers: {
'Authorization': 'Bearer ' + spotifyoauth
},
success: function(r) {
donecb(r);
},
error: function(r) {
errorcb(r);
}
});
}
function postSpotDirect(url, data, donecb, errorcb){
jQuery.ajax(url, {
method: "POST",
data: JSON.stringify(data),
dataType: 'json',
headers: {
'Authorization': 'Bearer ' + spotifyoauth
},
success: function(r) {
donecb(r);
},
error: function(r) {
errorcb(r);
}
});
}
function getItems(url, donevar, donecb, build) { // Load in items from a spotify api endpoint. Automatically follows next links, compiles data, and pushes donecb(data, donevar) in the end
jQuery.ajax(url, {
dataType: 'json',
headers: {
'Authorization': 'Bearer ' + spotifyoauth
},
success: function(r) {
itemCB(r, donevar, donecb, build);
},
error: function(r) {
bootbox.alert('Spotify Error (most likely an invalid or expired oauth token.)', function() {
blankscriptfiles();
});
}
});
}
function itemCB(resp, donevar, donecb, build){ // Callback for getItems. Gets data from spotify response, parses, and loads next or goes to donecb depending on response.
if (resp.tracks == undefined){ // If we dont have tracks
ritems = resp.items; //We want the .items property
rnext = resp.next;
} else { //If we have a tracks property
ritems = resp.tracks.items; //We want the .tracks.items property
rnext = resp.tracks.next;
}
if (rnext != undefined){ // If we have a next link
getItems(rnext, donevar, donecb, build.concat(ritems)); //Pull in more items
} else { //If we dont have a next link
donecb(build.concat(ritems), donevar); //We are done, call callback
}
}
function startRunning(){ // Kicks off the timer that manages the actuall import process and starts importing
window.plinfo['RunSong'] = false;
window.plinfo['RunPL'] = true;
window.plinfo['ticker']=setInterval(tickLauncher,200);
}
function endPortifyRun(){ // Shown to the user on completion. Simple stats and reset code.
var end = new Date().getTime();
var time = end - window.portifystart;
alert("Import Completed ");
alert('Runtime: ' + time/1000);
window.canImage = true;
window.portifyWorking = false;
clearInterval(window.plinfo['ticker']);
}
function tickLauncher(){ // This function is called regularly on a timer to kick off playlist and song operations. It handles the state of the import process and what is happening moment to moment
if (window.plinfo['RunSong'] == true){ // If we set to run song automation
window.plinfo['RunSong'] = false; // We have handled the request, set it to false to mark that
if (window.plinfo['lists'].length){ // If we have playlists that have not completed
dosong = window.plinfo['lists'][0]['tracks'].shift(); // Grab a song from the playlist we are working on
plname = window.plinfo['lists'][0]['name'];
if (dosong === undefined){ //If our playlist didnt have any songs left to do
window.plinfo['lists'].shift(); // Delete the playlist from the list
if (window.plinfo['lists'].length == 0){ // If the list is blank now
endPortifyRun(); // We are done
} else { // If the list is not blank
window.plinfo['RunSong'] = true; // Run the next song
}
} else { // If our playlist had songs
if (window.plinfo['lists']['mode'] != 'string'){ //Check the mode the playlist is set to. string mode is a special mode in development so if the mode is not string
tname = dosong['track']['name'];
if (dosong['track']['artists'] != undefined){ //If we have an artist for the track we are working on
if (dosong['track']['artists'].length > 0){
addSongToPlaylist(tname+ ' - ' +dosong['track']['artists'][0]['name'], plname); // Run the automation to add the song to the playlist. Has artist name included
} else {
addSongToPlaylist(tname, plname);
}
} else { // No artist on the track
addSongToPlaylist(tname, plname); //Run add song automation, no artist though.
}
}
}
}
}
if (window.plinfo['RunPL'] == true){// If we are set to run playlist automation
window.plinfo['RunPL'] = false; // We have handled the request, set it to false to mark that
for(i in window.plinfo['lists']){ //Check the playlists
var plist = window.plinfo['lists'][i];
if (plist['createdpl'] == false){ //If a playlist has not been created
plist['createdpl'] = true; //Say we are handling it
plist['name'] = plist['prefix'] + plist['name']; //Update the name based on the prefix
plist['prefix'] = ''; //Blank the prefix as we have handled it
newPlaylist(plist['name']); // Run the automation to create a new playlist
return; // Drop out of this tick
}
}
window.plinfo['RunSong'] = true; //All playlists have been created, run songs now.
}
}
function loadandscrapepl(){
console.log(window.gplexport['exportpls']);
window.gplexport['plon'] = window.gplexport['exportpls'].shift();
if (window.gplexport['plon'] != undefined){
jQuery('a[data-type=pl]').each(function(){
if(jQuery(this).text().trim() == window.gplexport['plon']){
location.hash = "/pl/" + jQuery(this).attr('data-id');
waitForGPLload(window.gplexport['plon'] + '');
return false;
}
});
} else {
doprompt(); // Scrape complete
}
}
function waitForGPLload(plname){
if(jQuery("a.selected[data-id=" + unescape(location.hash.split("/")[2]).replace(/([ #;?%&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1') + "]").length > 0){ // If we can find a selected <a> element on the page with a data id matching the hash location we have loaded the page
setTimeout(function(){window.scrapeSongs(function(a){gplDidScrape(a, plname)})}, 500);
} else {
setTimeout(function(){waitForGPLload(plname)}, 100);
}
}
function gplDidScrape(tarrayobj, plname){
var plobj = {};
plobj['xname'] = window.gplexport['plon'];
plobj['name'] = plname;
plobj['tracks'] = tarrayobj;
console.log(tarrayobj);
window.gplexport['pldone'].push(plobj);
loadandscrapepl();
}
function gotPlaylists(playlists, rstr){ // Callback for when users personal playlists are loaded
window.plinfo['mypls'] = playlists;
doprompt();
}
function confirmPlaylists(){ // Used to copy the playlists from mypls into the lists array in the proper format. Called once playlist selection is confirmed.
for (var i = 0; i < window.plinfo['mypls'].length; i++) {
newpl = {};
newpl['name'] = window.plinfo['mypls'][i]['name'];
newpl['mode'] = 'link';
newpl['prefix'] = window.plinfo['prefix'];
newpl['createdpl'] = false
newpl['loaded'] = false;
newpl['link'] = window.plinfo['mypls'][i]['href'];
newpl['tracks'] = [];
window.plinfo['lists'].push(newpl);
}
}
function playlistToggle(source) { // Toggle all checkboxes on the playlist selection dialog
checkboxes = document.getElementsByName('playlist');
for (var i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = source.checked;
}
}
function initmodal(){ // Initial questions
mds = window.modalstage;
switch (mds) {
case 0: // Initial Welcome
bootbox.dialog({
buttons: {
impmyspot: {
label: "Import My Spotify Playlists",
className: "btn-primary",
callback: function() {
window.afterOauth = 'MyPL';
window.modalGo('Oauth', true);
}
},
impotherspot: {
label: "Import a Spotify Playlist From a Link",
className: "btn-success",
callback: function() {
window.afterOauth = 'PlLink';
window.modalGo('Oauth', true);
}
},
exportgoogle: {
label: "Export My Google Playlists",
className: "btn-success",
callback: function() {
//window.afterOauth = 'GPlExport';
window.modalGo('GPlExport', true);
}
},
googlepldelete: {
label: "Bulk Delete Google Playlists",
className: "btn-success",
callback: function() {
window.modalGo('GPlDelete', true);
}
}
},
message: "<h1>Welcome to Portify.JS<\/h1><br>What would you like to do today?"
});
break;
}
}
function Oauthmodal(){ // Oauth prompt
mds = window.modalstage;
switch (mds) {
case 0: // Oauth token instructions
if (window.spotifyoauth === undefined){
bootbox.alert('Go --> <a target="_blank" href="https://developer.spotify.com/web-api/console/get-current-user-playlists/">HERE</a> <--. Click GET OAUTH TOKEN. Check all of the checkboxes. Then REQUEST TOKEN. Finally, copy the stuff in the OAuth Token text box to your clipboard and hit OK back over here.', doprompt);
} else {
setTimeout(function(){window.modalGo(window.afterOauth, true);}, 500);// Skip oauth prompt if we have a token set
}
break;
case 1: // Oauth token input page
bootbox.prompt("Enter the OAUTH token here", function(result) {
if (result === null) {
window.location.reload();
} else {
window.spotifyoauth = result;
window.modalGo(window.afterOauth, true);
}
});
break;
}
}
function gplexportmodal(){//Google playlist export dialog
mds = window.modalstage;
switch (mds) {
case 0: // Export all playlists or pick specific ones?
window.gplexport = {};
bootbox.confirm({
buttons: {
confirm: {
label: 'Pick Playlists',
className: 'confirm-button-class'
},
cancel: {
label: 'Export Everything',
className: 'cancel-button-class'
}
},
message: "I see " + jQuery('a[data-type=pl]').length.toString() + " playlists.<br>Do you want to pick which playlists to export or would you rather export everything?",
callback: function(result) {
window.gplexport['specific'] = result;
doprompt();
}
});
break;
case 1: //
if (window.gplexport['specific']){
aa = [];jQuery('a[data-type=pl]').each(function(){aa.push(jQuery(this).text().trim())});
playlistSelect(aa, "Select Playlist to Include", function(){ // Launch the playlist selector dialog, run the code below once confirmed
window.gplexport['exportpls'] = [];
jQuery("input[name='playlist']:checked").each(function(){
window.gplexport['exportpls'].push(jQuery(this).val());
});
bootbox.alert('Ready?', doprompt);
});
} else {
aa = [];
jQuery('a[data-type=pl]').each(function(){
aa.push(jQuery(this).text().trim())
});
window.gplexport['exportpls'] = aa;
bootbox.alert('Ready?', doprompt);
}
break;
case 2:
removejscssfile("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css", "css");
gplloadpls(window.gplexport['exportpls']);
break;
case 3:
addstyle("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css");
bootbox.confirm({
buttons: {
confirm: {
label: 'Export To .csv Dump',
className: 'confirm-button-class'
},
cancel: {
label: 'Push To Spotify',
className: 'cancel-button-class'
}
},
message: "Playlist loaded, what do you want to do with them?",
callback: function(result) {
if (result){
masterrows = [];
for(var i in window.gplexport['pldone']){ // For each playlist
pl = window.gplexport['pldone'][i]['name'];
trs = window.gplexport['pldone'][i]['tracks']
for(var ib in trs){ // For each track
srow = [pl];
for(var ic in trs[ib]){ // For each track var
srow.push(trs[ib][ic]);
}
masterrows.push(srow);
}
}
exportToCsv('playlists.csv', masterrows);
blankscriptfiles();
} else {
window.afterOauth = 'GtoSpot';
window.modalGo('Oauth', true);
}
}
});
break;
}
}
function gplaytospotmodal(){
mds = window.modalstage;
switch (mds) {
case 0:
getSpotDirect("https://api.spotify.com/v1/me", function(dat){window.gplexport['spot_uid'] = dat["id"]; doprompt();}, function(){});
case 1:
bootbox.prompt("Enter a prefix for your playlists like 'gplay-' (or leave blank to import them without altering their names)", function(result) {
if (result === null || result == "") {
window.gplexport['prefix'] = "";
doprompt();
} else {
window.gplexport['prefix'] = result;
doprompt();
}
});
break;
case 2:
bootbox.confirm("Are you sure your ready?", function(result) {
if (result == true){;
//Set start time to time run
window.portifystart = new Date().getTime();
//Start loading
bootbox.alert('Now copying <span id=\"portnowcopying\"><\/span>', function() {
});
window.gplayToSpot();
} else{
window.location.reload();
}
});
break;
}
}
function gplayToSpot(){
for(var i in window.gplexport['pldone']){ // For each playlist
if (window.gplexport['pldone'][i]['created'] === undefined){
createSpotPl(window.gplexport['pldone'][i]);
return;
}
}
for(var i in window.gplexport['pldone']){ // For each playlist
if (window.gplexport['pldone'][i]['tdone'] === undefined){
trs = window.gplexport['pldone'][i]['tracks']
for(var ib in trs){ // For each track
if (trs[ib]['created'] === undefined){
createSpotTrack(window.gplexport['pldone'][i]['id'], trs[ib]);
return;
}
}
window.gplexport['pldone'][i]['tdone'] = true;
}
}
jQuery("#portnowcopying").text("NULL; Import Complete");
}
function createSpotPl(plobj){
dat = {};
dat["name"] = window.gplexport['prefix'] + plobj['name'];
dat["public"] = false;
jQuery("#portnowcopying").text(dat["name"]);
postSpotDirect("https://api.spotify.com/v1/users/" + window.gplexport['spot_uid'] + "/playlists", dat, function(dbac){
plobj['created'] = true;
plobj['id'] = dbac['id'];
gplayToSpot();
}, function(r){
alert("error creating playlists, cant recover.")
});
}
function createSpotTrack(plid, trackobj){
tstr = "";
if (trackobj['artist'] != -1){
tstr = tstr + trackobj['artist'] + ' - ';
}
trackobj['created'] = true;
tstr = tstr + trackobj['title'];
jQuery("#portnowcopying").text(tstr);
getSpotDirect("https://api.spotify.com/v1/search?q=" + encodeURI(tstr) + "&type=track&limit=1&offset=0", function(dbac){
for (i in dbac['tracks']['items']){
dat = "";
postSpotDirect("https://api.spotify.com/v1/users/" + window.gplexport['spot_uid'] + "/playlists/" + plid + "/tracks?uris=" + encodeURI(dbac['tracks']['items'][i]["uri"]), dat, function(dbac){
gplayToSpot();
},function(r){
if (r.status == 429){
setTimeout(function(){createSpotTrack(plid, trackobj)}, 2000);
} else{
gplayToSpot();
}
});
return false;
}
gplayToSpot();
}, function(r){
if (r.status == 429){
setTimeout(function(){createSpotTrack(plid, trackobj)}, 2000);
} else {
gplayToSpot();
}
});
}
function gplloadpls(plarray){
window.gplexport['pldone'] = [];
loadandscrapepl();
//window.gplexport['RunPL'] = true;
}
function gpldeletemodal(){ //Bulk playlist delete dialog
bootbox.prompt("<h1>Google Playlist Bulk Deletion<\/h1><br>This utility deletes playlists in bulk<br>If you type something into the prompt all playlists containing that string will be deleted instantly when you hit the button<br>This tool is VERY dangerous<br>If you want to delete everything type in \"EVERYTHING\" without quotes<br>If you leave the box blank nothing will happen", function(result) {
if (result === null) {
} else {
if (result.length > 1){
if (result == "EVERYTHING"){
deleteMatchingPlaylists('');
return;
}
deleteMatchingPlaylists(result);
}
}
blankscriptfiles();
});
}
function PlLinkmodal(){ // Playlist from link
mds = window.modalstage;
switch (mds) {
case 0: // Enter playlist Name
bootbox.prompt("Name the playlist", function(result) {
if (result === null || result == "") {
window.location.reload();
} else {
//Create playlist object with name set
newpl = {};
newpl['name'] = result;
newpl['mode'] = 'link';
newpl['prefix'] = '';
newpl['createdpl'] = false
newpl['loaded'] = false;
window.plinfo['lists'].push(newpl);
doprompt();
}
});
break;
case 1: // Enter playlist link
bootbox.prompt("Paste the link in here", function(result) {
if (result === null) {
window.location.reload();
} else {
//Get user name and playlist name fron string, put it inot aaray
var res = result.split("/");
var atog = false;
var aaray = [];
for(var ssplit in res){
if (atog == true){
aaray.push(res[ssplit]);
atog = false;
}
if (res[ssplit] == 'user' || res[ssplit] == 'playlist'){
atog = true;
}
}
// If we got the user and playlist correctly then set the link value on our playlist object from the prior stage and run the confirm dialog
if(aaray.length == 2){
window.plinfo['lists'][0]['link'] = "https://api.spotify.com/v1/users/" + aaray[0] + "/playlists/" + aaray[1];
window.modalGo('confirm', true);;
}
}
});
break;
}
}
function MyPLmodal(){ // Get Playlists From Self
mds = window.modalstage;
switch (mds){
case 0: // Enter playlist prefix
bootbox.prompt("Enter a prefix for your playlists like 'spotify-' (or leave blank to import them without altering their names)", function(result) {
if (result === null || result == "") {
window.plinfo['prefix'] = "";
} else {
window.plinfo['prefix'] = result;
}
console.log(window.modalstage);
doprompt();
});
break;
case 1: // Fire off request to get self playlists from spotify, no dialog
window.plinfo['mypls'] = [];
getItems('https://api.spotify.com/v1/me/playlists', 'gotpl', gotPlaylists, []);
break;
case 2: // Self Playlist state number of playlists and ask if import all or import some
bootbox.confirm({
buttons: {
confirm: {
label: 'Pick Playlists',
className: 'confirm-button-class'
},
cancel: {
label: 'Import Everything',
className: 'cancel-button-class'
}
},
message: "I see " + window.plinfo['mypls'].length.toString() + " playlists.<br>Do you want to pick which playlists to import or would you rather import everything?",
callback: function(result) {
if (result == false){
window.modalGo('confirm', false);
window.confirmPlaylists();
//window.plarrayFIX = window.plarray;
}
else{
doprompt();
}
}
});
break;
case 3: // Self Playlist selection detail dialog
if (window.plinfo['mypls'].length > 0){
var aar = [];
for (i in window.plinfo['mypls']){
aar.push(window.plinfo['mypls'][i]['name']);
}
playlistSelect(aar, "Select Playlist to Include", function(){ // Launch the playlist selector dialog, run the code below once confirmed
mypls = window.plinfo['mypls']
newpls = [];
jQuery("input[name='playlist']:checked").each(function(){
for (i in mypls){
if (mypls[i]['name'] == jQuery(this).val()){
newpls.push(mypls[i]);
}
}
});
window.plinfo['mypls'] = newpls;
window.confirmPlaylists(); //Lock in picked playlists
window.modalGo('confirm', true); // Go to the confirm diag
});
}
break;
}
}
function playlistSelect(plarray, message, confirmcb){ // Launch the playlist selection dialog with a playlist name array, a message, and a callback on confirmation
// Generate checkbox playlist selection elements
var playlist = jQuery('<div>',{style:"height:300px;overflow:scroll;"});
var div = jQuery("<div>", {class:"checkbox"})
var label = jQuery("<label>",{for:"plToggle"});
var input = jQuery("<input>", {name:"plToggle", id:"plToggle", onclick:"playlistToggle(this);", type:"checkbox", checked:"checked"});
label.append(input).append('[Check/Uncheck All]');
div.append(label);
playlist.append(div);
for(i in plarray){
var div = jQuery("<div>", {class:"checkbox"})
var label = jQuery("<label>",{for:"playlist-"+i});
var input = jQuery("<input>", {name:"playlist", id:"playlist-"+i, value:plarray[i], type:"checkbox", checked:"checked"});
label.append(input).append(''+plarray[i]);
div.append(label);
playlist.append(div);
}
//Dialog
bootbox.dialog({
title: message,
message: playlist.prop('outerHTML'),
buttons: {
cancel: {
label: 'Back',
className: 'cancel-button-class',
callback: function () {
window.modalstage = window.modalstage - 2;
doprompt();
}
},
confirm: {
label: 'Go',
className: 'confirm-button-class',
callback: confirmcb
}
}
});
}
function confirmmodal(){ // Final Confirm
bootbox.confirm("This can take quite a while, are you sure your ready?", function(result) {
if (result == true){
//Unload script files (mainly css)
blankscriptfiles();
//Set start time to time run
window.portifystart = new Date().getTime();
//Start loading
window.loadpls();
} else{
window.location.reload();
}
});
}
function modalGo(type, doprompts){ // Pop into another dialog branch.
window.modalstage = 0;
window.modalmode = type;
if (doprompts === true){
doprompt();
}
}
function doprompt(){ // Dialog master router
mds = window.modalstage;
mdm = window.modalmode;
console.log(mds);
console.log(mdm);
switch (mdm){
case "init":
initmodal();
break;
case "Oauth":
Oauthmodal();
break;
case "PlLink":
PlLinkmodal();
break;
case "MyPL":
MyPLmodal();
break;
case "confirm":
confirmmodal();
break;
case "GPlDelete":
gpldeletemodal();
break;
case "GPlExport":
gplexportmodal();
break;
case "GtoSpot":
gplaytospotmodal();
break;
}
window.modalstage = window.modalstage + 1;
}
function blankscriptfiles(){ // Remove scripts and css files we have loaded.
removejscssfile("https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css", "css");
removejscssfile('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js', "js");
removejscssfile('https://cdnjs.cloudflare.com/ajax/libs/bootbox.js/4.4.0/bootbox.min.js', "js");
}
function clearwaves(){ // Clear ripple animations, they build up if we dont.
x = jQuery("div.wave-container").parent().parent()[0];
if (x !== undefined){
rl = x.ripples.length;
for (var i = 0; i < rl; i++) {
x.removeRipple(x.ripples[0]);
}
}
}
function deleteMatchingPlaylists(search){ // Used to undo my test playlists. Will delete all playlists containing the search string
jQuery('a:contains(' + search + ')').each(function( index ) {
jQuery(this).find('paper-icon-button').first().click()
doclick(jQuery("div[class='goog-menuitem-content']:contains('Delete playlist')")[0])
jQuery('button:contains(Delete playlist)').click()
});
}
function dospotimport(){ // Launches internally once scripts are loaded.
window.canImage = false;
doprompt();
}
function setupPlInfo(){ // Init plinfo requirements
window.plinfo = {};
window.plinfo['lists'] = [];
window.plinfo['prefix'] = "";
}
function portifyjs(mstage){ // Launch portify at init modal with stage mstage
window.loadstep = 0;
window.modalstage = mstage;
window.modalmode = "init";
window.items = {};
setupPlInfo();
doloadstep();
}
if (window.location.host != "play.google.com"){
window.location = "https://play.google.com/music/listen";
}
if (window.portifyWorking != true){
window.portifyWorking = true;
portifyjs(0);
}