Created
May 13, 2013 17:01
-
-
Save jlewin/5569791 to your computer and use it in GitHub Desktop.
Adaption of my dumpToDiskViaFileSystemAPI.js Gist to free playlist data from Rhapsody
This file contains hidden or 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
| // Define the success callback for webkitRequestFileSystem() | |
| function onInitFs(fs) { | |
| // Grab each playlist element from the Rhapsody Playlists view, collect data for the playlist | |
| // then invoke the 'Add to Mixer' url to collect the playlist JSON | |
| $('#content-frame li').each(function () { | |
| // Markup template: | |
| // <li class="playlist playlist-item" href="/members/071tm1/playlists/mp.154595841" playlist_id="mp.154595841" playlist_name="2007-05"> | |
| var $this = $(this), | |
| fileName = $this.attr('playlist_name') + '.js'; | |
| // Now execute an xhr to collect the playlist json and finally save the results to disk | |
| $.ajax({ | |
| url: $this.attr('href') + '/queue', // Url to collect playlist json is built by simply appending /queue to path | |
| dataType: 'text', | |
| success: function (jsonResults) { | |
| // Begin the persist operation | |
| fs.root.getFile(fileName, { create: true }, function (fileEntry) { | |
| // Create a FileWriter object for our FileEntry | |
| fileEntry.createWriter(function (fileWriter) { | |
| fileWriter.onwriteend = function (e) { | |
| console.log('Write completed.'); | |
| }; | |
| 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([jsonResults], { 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