Skip to content

Instantly share code, notes, and snippets.

@jlewin
Created May 14, 2013 00:02
Show Gist options
  • Select an option

  • Save jlewin/5572573 to your computer and use it in GitHub Desktop.

Select an option

Save jlewin/5572573 to your computer and use it in GitHub Desktop.
Adaption of my dumpToDiskViaFileSystemAPI.js Gist to free Library data from Rhapsody
var context;
// Define the success callback for webkitRequestFileSystem()
function onInitFs(fs) {
// Collect list of artists from the Rhapsody Library view. For each artist, we will request the
// library details and find the albums and tracks from each. This requires an xhr foreach
// and to easy the load and appear less bot like, perform requests in sequence at
// random intervals
context = {
remaining: $('#library-artist-list li').toArray(), // (Sanity Check first 5) .slice(0, 5),
chromeFS: fs,
artists: []
};
// Begin processing the list
processArtists();
}
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Pop one artist from the list, perform the xhr to gather library info, persist the results and
// queue the next request to processArtists
function processArtists() {
// Stop processing when empty
if (context.remaining.length <= 0) return;
// Pop the next artist to be processed and wrap with jQuery
var $artistInfoLI = $(context.remaining.pop()),
artist = {
name: $artistInfoLI.text().trim(),
albums: []
},
// Build url for library tracks for the artist
artistTracksUrl = 'http://www.rhapsody.com/members/071tm1/library/artists/' + $artistInfoLI.attr('artist_id') + '/albums?me=true&l=en';
context.artists.push(artist);
console.log('Processing ' + artist.name + ' with ' + context.remaining.length + ' remaining');
// Now execute the xhr to collect the tracks list for the given artist and finally save the results to disk
$.ajax({
url: artistTracksUrl,
dataType: 'html',
success: function (resultsDom) {
// Grab each album list from the artist results page
$(resultsDom).find('.library-tracks').each(function () {
var $album = $(this),
$parent = $album.parent();
var album = {
name: $parent.attr('album_name'),
artist: $parent.attr('artist_name'),
albumUrl: $parent.attr('href'),
tracks: []
};
// Add each track to the album
$album.find('li').each(function () {
var $li = $(this);
album.tracks.push({
name: $li.attr('track_name'),
trackUrl: $li.attr('href'),
id: $li.attr('track_id')
});
});
// Add Album to artist
artist.albums.push(album);
});
}
});
if (context.remaining.length > 0) {
// Random delay recursive call
window.setTimeout(processArtists, getRandomInt(5000, 12000));
} else {
saveResults();
}
}
function saveResults() {
// Begin the persist operation
context.chromeFS.root.getFile('RhapsodyLibrary', { create: true }, function (fileEntry) {
// Create a FileWriter object for our FileEntry
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (e) {
console.log('Write completed.');
alert('Library Processing Complete');
};
fileWriter.onerror = function (e) {
console.log('Write failed: ' + e.toString());
};
// Create a new Blob and write to the output stream/FileWriter
fileWriter.write(new Blob([JSON.stringify(context.artists)], { type: "text/plain" }));
}, errorHandler);
}, errorHandler);
}
// Request persistent storage and when allowed, call into webkitRequestFileSystem and thus onInitFS
window.webkitStorageInfo.requestQuota(PERSISTENT, 1024 * 1024, function (grantedBytes) {
window.webkitRequestFileSystem(PERSISTENT, grantedBytes, onInitFs, errorHandler);
}, errorHandler);
function errorHandler(e) {
var msg = '';
switch (e.code) {
case FileError.QUOTA_EXCEEDED_ERR:
msg = 'QUOTA_EXCEEDED_ERR';
break;
case FileError.NOT_FOUND_ERR:
msg = 'NOT_FOUND_ERR';
break;
case FileError.SECURITY_ERR:
msg = 'SECURITY_ERR';
break;
case FileError.INVALID_MODIFICATION_ERR:
msg = 'INVALID_MODIFICATION_ERR';
break;
case FileError.INVALID_STATE_ERR:
msg = 'INVALID_STATE_ERR';
break;
default:
msg = 'Unknown Error';
break;
};
console.log('Error: ' + msg);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment