Permalink
Browse files

Support Atom podcasts

  • Loading branch information...
1 parent b8b7809 commit 452ee2f315a31fec4b3abc9b9ece2440f21b98ce @jwheare committed Dec 14, 2009
Showing with 84 additions and 14 deletions.
  1. +58 −2 src/js/importers.js
  2. +26 −12 src/js/importers/url.js
View
@@ -179,12 +179,12 @@ IMPORTERS = {
// XML to JSON converters often return single item lists as single items
var trackList = $.makeArray(podcast.item);
if (!trackList.length) {
- throw exception('No tracks in Podcast', jspf.trackList);
+ throw exception('No tracks in Podcast', podcast.item);
}
// Create the playlist
var image = podcast.image ? podcast.image.href : (podcast.thumbnail ? podcast.thumbnail.url : '');
var subtitle = IMPORTERS.getStringItem(podcast.subtitle);
- var description = IMPORTERS.getStringItem(podcast.description);
+ var description = IMPORTERS.getStringItem(podcast.description || podcast.summary);
if (description == subtitle) {
description = '';
}
@@ -220,6 +220,62 @@ IMPORTERS = {
}
return playlist;
},
+ createPlaylistFromAtomPodcast: function (source, podcast, callback, exception) {
+ if (!podcast.entry) {
+ throw exception('No tracks in Atom Podcast response', podcast);
+ }
+ // XML to JSON converters often return single item lists as single items
+ var trackList = $.makeArray(podcast.entry);
+ if (!trackList.length) {
+ throw exception('No tracks in Atom Podcast', podcast.entry);
+ }
+ // Create the playlist
+ var image = podcast.image ? podcast.image.href : podcast.logo;
+ var subtitle = IMPORTERS.getStringItem(podcast.subtitle);
+ var description = IMPORTERS.getStringItem(podcast.description || podcast.summary);
+ if (description == subtitle) {
+ description = '';
+ }
+ var linkObject = $.grep(podcast.link, function (value, i) {
+ return value.rel == 'alternate';
+ })[0];
+ var link = linkObject ? linkObject.href : '';
+ var playlist = new MODELS.Playlist({
+ name: IMPORTERS.getStringItem(podcast.title),
+ subtitle: subtitle,
+ description: description,
+ copyright: IMPORTERS.getStringItem(podcast.copyright),
+ image: IMPORTERS.getAbsoluteUrl(image),
+ url: link,
+ source: source
+ });
+ // Load tracks
+ $.each(trackList, function (i, data) {
+ var trackDoc = {
+ name: data.title,
+ artist: data.author ? (data.author.name || IMPORTERS.getStringItem(data.author)) : podcast.author
+ };
+ // TODO support media:content alternative
+ // e.g. <media:content url="blah.mp3" fileSize="46669578" type="audio/mpeg">
+ var trackLinks = $.makeArray(data.link);
+ var trackLink = $.grep(trackLinks, function (value, i) {
+ return value.rel && value.rel == 'enclosure';
+ })[0];
+ if (trackLink) {
+ trackDoc.url = trackLink.href;
+ trackDoc.size = trackLink['length'];
+ trackDoc.mimetype = trackLink.type;
+ }
+ playlist.add_track(new MODELS.Track(trackDoc));
+ });
+ // Save
+ playlist.save();
+ // Call the IMPORTERS.createPlaylistFromPodcast callback
+ if (callback) {
+ callback(playlist);
+ }
+ return playlist;
+ },
defaultExceptionHandler: function (exception) {
if (PLAYLICK.debug) {
console.warn(exception);
View
@@ -17,6 +17,7 @@ var Url = {};
* Available subclasses:
* Url.XspfException
* Url.PodcastException
+ * Url.AtomPodcastException
**/
Url.Exception = function (url, message, diagnostics) {
this.url = url;
@@ -32,11 +33,14 @@ Url.Exception.prototype.toString = function () {
/**
* class Url.XspfException < Url.Exception
* class Url.PodcastException < Url.Exception
+ * class Url.AtomPodcastException < Url.Exception
**/
Url.XspfException = Url.Exception;
Url.XspfException.prototype.name = 'UrlXspfException';
Url.PodcastException = Url.Exception;
Url.PodcastException.prototype.name = 'UrlPodcastException';
+Url.AtomPodcastException = Url.Exception;
+Url.AtomPodcastException.prototype.name = 'UrlAtomPodcastException';
/**
* Url.url(url[, callback][, exceptionHandler])
* - url (String): URL to import the playlist from
@@ -50,26 +54,36 @@ Url.PodcastException.prototype.name = 'UrlPodcastException';
Url.url = function (source, callback, exceptionHandler) {
var exception = new Url.XspfException(source);
IMPORTERS.getJsonFomXml(source, function (json) {
- var podcast = json.query.results.rss ? json.query.results.rss.channel : '';
- var jspf = json.query.results.lfm ? json.query.results.lfm.playlist : json.query.results.playlist;
- if (!podcast && !jspf) {
- throw exception('Invalid Podcast/XSPF', json.query.results);
+ var root = json.query.results;
+ var podcast = root.rss ? root.rss.channel : '';
+ var atom = root.feed;
+ var jspf = root.lfm ? root.lfm.playlist : root.playlist;
+ if (!podcast && !atom && !jspf) {
+ throw exception('Invalid Podcast/XSPF', root);
}
- if (podcast) {
- var playlist = IMPORTERS.createPlaylistFromPodcast(
+ var playlist;
+ if (jspf) {
+ var metadata = {};
+ playlist = IMPORTERS.createPlaylistFromJspf(
+ source,
+ jspf,
+ metadata,
+ callback,
+ new Url.XspfException(source)
+ );
+ } else if (podcast) {
+ playlist = IMPORTERS.createPlaylistFromPodcast(
source,
podcast,
callback,
new Url.PodcastException(source)
);
- } else if (jspf) {
- var metadata = {};
- var playlist = IMPORTERS.createPlaylistFromJspf(
+ } else if (atom) {
+ playlist = IMPORTERS.createPlaylistFromAtomPodcast(
source,
- jspf,
- metadata,
+ atom,
callback,
- new Url.XspfException(source)
+ new Url.AtomPodcastException(source)
);
}
}, exception, exceptionHandler);

0 comments on commit 452ee2f

Please sign in to comment.