Last active
August 29, 2015 14:20
-
-
Save MeoMix/b8e5e4a38802aff2c11e to your computer and use it in GitHub Desktop.
Screencast of what to do is here: https://streamus.com/grooveshark.swf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// RUN THIS CODE ON groovebackup.com's console, see screencast for more info. | |
var playlistTitleAndEntryList = []; | |
function recursiveParseLinks(links, callback){ | |
if(links.length > 0){ | |
var link = links.shift(); | |
if(link.href.indexOf('exportPlaylist') !== -1){ | |
var xmlHttpRequest = new XMLHttpRequest(); | |
xmlHttpRequest.onreadystatechange = function(){ | |
if (xmlHttpRequest.readyState == 4 ) { | |
var rows = xmlHttpRequest.responseText.split('\n'); | |
var nonHeaderRows = rows.slice(1, rows.length); | |
var titleList = []; | |
nonHeaderRows.forEach(function(row){ | |
var columns = row.split(','); | |
if(columns.length === 3){ | |
// Column[0] is Song name, | |
// Column[1] is artist | |
var entry = columns[1] + ' ' + columns[0]; | |
entry = entry.replace(/"/g, ""); | |
titleList.push(entry); | |
} | |
}); | |
if(titleList.length > 0){ | |
playlistTitleAndEntryList.push({ | |
title: link.innerHTML, | |
titleList: titleList | |
}); | |
} | |
recursiveParseLinks(links, callback); | |
} | |
}; | |
console.log('Working...'); | |
xmlHttpRequest.open('get', link.href, true); | |
xmlHttpRequest.send(); | |
} | |
else { | |
recursiveParseLinks(links, callback); | |
} | |
} else { | |
callback(); | |
} | |
} | |
var foundLinks = Array.prototype.slice.call(document.querySelectorAll('a')); | |
recursiveParseLinks(foundLinks, function(){ | |
console.log('Data scrape - COMPLETE. TAKE THIS DATA TO STREAMUS.'); | |
console.log(JSON.stringify(playlistTitleAndEntryList)); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run this code on streamus' extension background page. | |
// YOU MUST MODIFY THIS CODE BY COPY/PASTING IN YOUR PLAYLIST INFO FROM THE ABOVE PAGE INTO THE VARIABLE BELOW. | |
// See screencast for more details. | |
var playlistsToImport = null; | |
var YouTubeV3API = require('background/model/youTubeV3API'); | |
var GroovesharkImporter = function() { | |
this.getPlaylistSongs = function(playlist, foundSongs, onCompleteCallback) { | |
if (playlist.titleList.length > 0) { | |
var title = playlist.titleList.shift(); | |
this.searchAndAddByTitle({ | |
title: title, | |
foundSongs: foundSongs, | |
playlist: playlist, | |
error: function(error) { | |
console.error('Failed to add song by title: ' + title, error); | |
}, | |
complete: this.getPlaylistSongs.bind(this, playlist, foundSongs, onCompleteCallback) | |
}); | |
} | |
else { | |
console.log('Complete, found songs:', foundSongs.length, foundSongs); | |
onCompleteCallback({ | |
playlist: playlist, | |
foundSongs: foundSongs | |
}); | |
} | |
}; | |
this.searchAndAddByTitle = function(options) { | |
YouTubeV3API.getSongByTitle({ | |
title: options.title, | |
success: function(song) { | |
options.foundSongs.push(song); | |
if (options.success) { | |
options.success(); | |
} | |
}.bind(this), | |
error: options.error, | |
complete: options.complete | |
}); | |
}; | |
}; | |
var groovesharkImporter = new GroovesharkImporter(); | |
// Iterate over each playlist that I need to create, search for its songs, then call addPlaylistWithSongs. | |
function importPlaylists(playlistsToImport) { | |
if (playlistsToImport.length > 0) { | |
console.log('Working...'); | |
var playlistToImport = playlistsToImport.shift(); | |
groovesharkImporter.getPlaylistSongs(playlistToImport, [], function(response) { | |
var playlists = window.signInManager.get('signedInUser').get('playlists'); | |
playlists.addPlaylistWithSongs(response.playlist.title, response.foundSongs); | |
// Import one playlist every 5 seconds to not break my server. | |
setTimeout(function() { | |
importPlaylists(playlistsToImport); | |
}, 3000); | |
}); | |
} else { | |
console.log('done'); | |
} | |
} | |
importPlaylists(playlistsToImport); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment