Permalink
Browse files

Add mechanism for supporting rechecking subscribed playlists. Save th…

…e importer namespace, method and arguments used to fetch and parse a playlist for Url.url, LastFm.lovedTracks and LastFm.getUserPlaylist.
  • Loading branch information...
1 parent fbab33e commit ed72a9e10a8332443fdf7318f0beaa30ae596fb7 @jwheare committed Dec 17, 2009
@@ -32,7 +32,7 @@ function Playlist () {
}
Playlist.prototype = {
register: function (playlist) {
- // May be needed later. See how MVC stuff goes
+ playlist.save();
},
/* COUCHDB */
@@ -108,7 +108,6 @@ Playlist.prototype = {
var albumElements = [];
var that = this;
var playlists = MODELS.Playlist.fetchAll(function callback (playlist) {
- that.register(playlist);
if (playlist.isAlbum()) {
albumElements.push(playlist.element.get()[0]);
} else {
@@ -137,7 +136,6 @@ Playlist.prototype = {
} else {
// Create the playlist object
this.current = new MODELS.Playlist();
- this.register(this.current);
playlistItem = this.createLink.parent('li');
}
// Update the sidebar
@@ -460,6 +458,23 @@ Playlist.prototype = {
}
},
+ /* SUBSCRIPTIONS */
+ checkSubscription: function (playlist) {
+ if (!playlist.isSubscription()) {
+ // Not a subscription
+ return;
+ }
+ var sub = playlist.subscription;
+ // Add callback and exception handler to the arguments
+ sub.arguments.push(function callback (newPlaylist) {
+ // compare with saved playlist and update/warn for conflicts?
+ });
+ sub.arguments.push(function exceptionHandler (exception) {
+ // show a warning icon and message
+ });
+ IMPORTERS[sub.namespace][sub.method].apply(this, sub.arguments);
+ },
+
/* UNLOAD */
onUnload : function (playlist) {
PLAYDAR.hideSM2Container();
View
@@ -36,7 +36,7 @@ IMPORTERS = {
// Check the response, call the callback and handle any exceptions
try {
IMPORTERS.checkResponse.call(this, json, exception);
- callback.call(this, json);
+ callback.call(this, json, url, params);
} catch (e) {
exceptionHandler(e);
}
@@ -58,11 +58,11 @@ IMPORTERS = {
IMPORTERS.getJson(IMPORTERS.YQL_URL, {
q: IMPORTERS.yqlSelectXML(url),
format: 'json'
- }, function (json) {
+ }, function (json, requestUrl, requestParams) {
if (!json.query || !json.query.results) {
throw exception('Invalid URL', json);
}
- callback.call(this, json);
+ callback.call(this, json, requestUrl, requestParams);
}, exception, exceptionHandler);
},
autocompleteFromXml: function (element, url, params, parse, formatItem) {
@@ -142,7 +142,8 @@ IMPORTERS = {
image: IMPORTERS.getAbsoluteUrl(metadata.image || jspf.image, source),
description: description,
url: IMPORTERS.getAbsoluteUrl(url, source),
- source: source
+ source: source,
+ subscription: metadata.subscription
});
// Load tracks
$.each(trackList, function (i, data) {
@@ -161,9 +162,7 @@ IMPORTERS = {
playlist.add_track(new MODELS.Track(trackDoc));
}
});
- // Save
- playlist.save();
- // Call the IMPORTERS.createPlaylistFromJspf callback
+ // Call the callback
if (callback) {
callback(playlist);
}
@@ -184,7 +183,7 @@ IMPORTERS = {
}
return string;
},
- createPlaylistFromPodcast: function (source, podcast, callback, exception) {
+ createPlaylistFromPodcast: function (source, podcast, metadata, callback, exception) {
if (!podcast.item) {
throw exception('No tracks in Podcast response', podcast);
}
@@ -201,6 +200,8 @@ IMPORTERS = {
description = '';
}
var playlist = new MODELS.Playlist({
+ type: metadata.type,
+ subscription: metadata.subscription,
title: IMPORTERS.getStringItem(podcast.title),
subtitle: subtitle,
description: description,
@@ -224,15 +225,13 @@ IMPORTERS = {
}
playlist.add_track(new MODELS.Track(trackDoc));
});
- // Save
- playlist.save();
- // Call the IMPORTERS.createPlaylistFromPodcast callback
+ // Call the callback
if (callback) {
callback(playlist);
}
return playlist;
},
- createPlaylistFromAtomPodcast: function (source, podcast, callback, exception) {
+ createPlaylistFromAtomPodcast: function (source, podcast, metadata, callback, exception) {
if (!podcast.entry) {
throw exception('No tracks in Atom Podcast response', podcast);
}
@@ -253,6 +252,8 @@ IMPORTERS = {
})[0];
var link = linkObject ? linkObject.href : '';
var playlist = new MODELS.Playlist({
+ type: metadata.type,
+ subscription: metadata.subscription,
title: IMPORTERS.getStringItem(podcast.title),
subtitle: subtitle,
description: description,
@@ -280,9 +281,7 @@ IMPORTERS = {
}
playlist.add_track(new MODELS.Track(trackDoc));
});
- // Save
- playlist.save();
- // Call the IMPORTERS.createPlaylistFromPodcast callback
+ // Call the callback
if (callback) {
callback(playlist);
}
View
@@ -81,7 +81,7 @@ LastFm.getPlaylist = function (url, metadata, callback, exceptionHandler) {
playlistURL: url
};
var exception = new LastFm.Exception(LastFm.generateSignature(method, params));
- LastFm.getJson(method, params, function (json) {
+ LastFm.getJson(method, params, function (json, requestUrl, requestParams) {
var playlist = IMPORTERS.createPlaylistFromJspf(this.url, json.playlist, metadata, callback, exception);
}, exception, exceptionHandler);
};
@@ -94,7 +94,13 @@ LastFm.getUserPlaylist = function (data, callback, exceptionHandler) {
var url = "lastfm://playlist/" + data.id;
// Extract other metadata from playlist info
var metadata = {
- url: data.url
+ type: 'subscription',
+ url: data.url,
+ subscription: {
+ namespace: 'LastFm',
+ method: 'getUserPlaylist',
+ arguments: [data]
+ }
};
var image = $.grep(data.image, function (value, i) {
return value.size == 'medium';
@@ -123,7 +129,7 @@ LastFm.userPlaylists = function (user, callback, noPlaylistHandler, exceptionHan
user: user
};
var exception = new LastFm.Exception(LastFm.generateSignature(method, params));
- LastFm.getJson(method, params, function (json) {
+ LastFm.getJson(method, params, function (json, requestUrl, requestParams) {
if (!json.playlists) {
throw exception('No playlists in response', json);
}
@@ -153,7 +159,7 @@ LastFm.lovedTracks = function (user, callback, exceptionHandler) {
user: user
};
var exception = new LastFm.Exception(LastFm.generateSignature(method, params));
- LastFm.getJson(method, params, function (json) {
+ LastFm.getJson(method, params, function (json, requestUrl, requestParams) {
if (!json.lovedtracks || !json.lovedtracks.track) {
throw exception('No loved tracks in response', json);
}
@@ -164,7 +170,13 @@ LastFm.lovedTracks = function (user, callback, exceptionHandler) {
}
// Create the playlist
var playlist = new MODELS.Playlist({
- title: 'Loved tracks for ' + json.lovedtracks['@attr'].user
+ type: 'subscription',
+ title: 'Loved tracks for ' + json.lovedtracks['@attr'].user,
+ subscription: {
+ namespace: 'LastFm',
+ method: 'lovedTracks',
+ arguments: [user]
+ }
});
// Load tracks
$.each(trackList, function (i, data) {
@@ -174,8 +186,6 @@ LastFm.lovedTracks = function (user, callback, exceptionHandler) {
};
playlist.add_track(new MODELS.Track(trackDoc));
});
- // Save
- playlist.save();
// Call the LastFm.lovedtracks callback
if (callback) {
callback(playlist);
@@ -201,7 +211,7 @@ LastFm.album = function (artist, album, callback, exceptionHandler) {
album: album
};
var exception = new LastFm.Exception(LastFm.generateSignature(method, params));
- LastFm.getJson(method, params, function (json) {
+ LastFm.getJson(method, params, function (json, requestUrl, requestParams) {
if (!json.album) {
throw exception('No album data', json);
}
@@ -235,7 +245,7 @@ LastFm.getTopTracks = function (artist, callback, exceptionHandler) {
artist: artist
};
var exception = new LastFm.Exception(LastFm.generateSignature(method, params));
- LastFm.getJson(method, params, function (json) {
+ LastFm.getJson(method, params, function (json, requestUrl, requestParams) {
if (!json.toptracks || !json.toptracks.track) {
throw exception('No top tracks for ' + artist, json);
}
@@ -253,7 +263,7 @@ LastFm.generateUsersPlaylist = function (userA, userB, callback, exceptionHandle
limit: 20
};
var exception = new LastFm.Exception(LastFm.generateSignature(method, params));
- LastFm.getJson(method, params, function (json) {
+ LastFm.getJson(method, params, function (json, requestUrl, requestParams) {
if (!json.comparison || !json.comparison.result || !json.comparison.result.artists || !json.comparison.result.artists.artist) {
throw exception("No shared artists found", json);
}
@@ -276,9 +286,7 @@ LastFm.generateUsersPlaylist = function (userA, userB, callback, exceptionHandle
if (!playlist.tracks.length) {
return exceptionHandler(exception("No valid tracks found for artists", playlistTracks));
}
- // Save
- playlist.save();
- // Call the LastFm.generateUsersPlaylist callback
+ // Call the callback
if (callback) {
callback(playlist);
}
@@ -120,7 +120,7 @@ Spotify.album = function (url, callback, exceptionHandler) {
extras: 'trackdetail'
});
var exception = new Spotify.AlbumException(url);
- IMPORTERS.getJsonFomXml(albumLookupUrl, function (json) {
+ IMPORTERS.getJsonFomXml(albumLookupUrl, function (json, requestUrl, requestParams) {
if (!json.query.results.album) {
throw exception('No album', json);
}
@@ -154,8 +154,6 @@ Spotify.album = function (url, callback, exceptionHandler) {
Spotify.addTrack(playlist, trackData);
}
});
- // Save
- playlist.save();
// Call the Spotify.album callback
if (callback) {
callback(playlist);
@@ -179,7 +177,7 @@ Spotify.track = function (url, callback, exceptionHandler) {
uri: url
});
var exception = new Spotify.TrackException(url);
- IMPORTERS.getJsonFomXml(trackLookupUrl, function (json) {
+ IMPORTERS.getJsonFomXml(trackLookupUrl, function (json, requestUrl, requestParams) {
if (!json.query || !json.query.results || !json.query.results.track) {
throw exception('No track', json);
}
@@ -195,8 +193,6 @@ Spotify.track = function (url, callback, exceptionHandler) {
});
trackData.href = url;
Spotify.addTrack(playlist, trackData);
- // Save
- playlist.save();
// Call the Spotify.track callback
if (callback) {
callback(playlist);
View
@@ -53,7 +53,7 @@ Url.AtomPodcastException.prototype.name = 'UrlAtomPodcastException';
**/
Url.url = function (source, callback, exceptionHandler) {
var exception = new Url.XspfException(source);
- IMPORTERS.getJsonFomXml(source, function (json) {
+ IMPORTERS.getJsonFomXml(source, function (json, requestUrl, requestParams) {
var root = json.query.results;
var podcast = root.rss ? root.rss.channel : '';
var atom = root.feed;
@@ -62,8 +62,15 @@ Url.url = function (source, callback, exceptionHandler) {
throw exception('Invalid Podcast/XSPF', root);
}
var playlist;
+ var metadata = {
+ type: 'subscription',
+ subscription: {
+ namespace: 'Url',
+ method: 'url',
+ arguments: [source]
+ }
+ };
if (jspf) {
- var metadata = {};
playlist = IMPORTERS.createPlaylistFromJspf(
source,
jspf,
@@ -75,13 +82,15 @@ Url.url = function (source, callback, exceptionHandler) {
playlist = IMPORTERS.createPlaylistFromPodcast(
source,
podcast,
+ metadata,
callback,
new Url.PodcastException(source)
);
} else if (atom) {
playlist = IMPORTERS.createPlaylistFromAtomPodcast(
source,
atom,
+ metadata,
callback,
new Url.AtomPodcastException(source)
);
Oops, something went wrong.

0 comments on commit ed72a9e

Please sign in to comment.