Commits

Mathias Panzenböck committed 66da2d7

more url matching

Comments (0)

Files changed (2)

 	},
 	'album': function (req, res, done) {
 		var albumname = req.param('name');
-		db.get(
-			'select albumname, artist, also, description, sku, '+
-			'launchdate, itunes from albums where albumname = ? limit 1',
-			[albumname],
+		var sql = 'select albumname, artist, also, description, sku, '+
+			'launchdate, itunes from albums where ';
+		var args;
+
+		if (albumname === undefined) {
+			sql += 'sku = ? limit 1';
+			args = [req.param('sku')];
+		}
+		else {
+			sql += 'albumname = ? limit 1';
+			args = [albumname];
+		}
+
+		db.get(sql, args,
 			function (err, album) {
 				if (err) return done(err);
 				if (!album) return done(null,null);
 
 				function songs () {
 					db.all('select number, desc, duration, mp3 from songs where albumname = ? order by number',
-						[albumname],
+						[album.albumname],
 						function (err, rows) {
 							if (err) return done(err);
 							album.songs = rows;
 				}
 			});
 	},
+	'song': function (req, res, done) {
+		var mp3 = req.param('mp3');
+		var sql = 'select albumname, number, desc, duration, mp3 from songs where ';
+		var args;
+
+		if (mp3 === undefined) {
+			sql += 'albumname = ? and number = ? limit 1';
+			args = [req.param('albumname'),parseInt(req.param('number'),10)];
+		}
+		else {
+			sql += 'mp3 = ? limit 1';
+			args = [mp3];
+		}
+
+		console.log(sql);
+		console.log(JSON.stringify(args));
+
+		db.get(sql, args,
+			function (err, song) {
+				done(err,song||null);
+			});
+	},
 	'artist': function (req, res, done) {
 		db.get(
 			'select artist, description, homepage, city, state, '+
 				['albums',  [['artist'],['albumname'],['sku']]],
 				['artists', [['artist']]],
 				['genres',  [['albumname'],['genre']]],
-				['songs',   [['albumname'],['number']]]],
+				['songs',   [['albumname'],['number']]],
+				['songs',   [['mp3']]]],
 				function (item, next) {
 					var table = item[0];
 					var indices = item[1];

javascripts/magnatune.js

 			}
 		},
 		_guessSongsFromUrl: function (url, done) {
-			var prefix = location.protocol+"//"+location.host+location.pathname;
+			var prefix = location.protocol+"//"+location.host+location.pathname, m;
+
+			function onerror (request, textStatus, errorThrown) {
+				done(errorThrown||new Error(textStatus));
+			}
+
 			if (url.slice(0,prefix.length) === prefix) {
-				var m = /^#\/([^\/]*)\/([^\/]*)/.exec(url.slice(prefix.length));
+				m = /^#\/([^\/]*)\/([^\/]*)/.exec(url.slice(prefix.length));
 				if (!m) return done(new Error("Faild to load songs from url: "+url));
 				var name = decodeURIComponent(m[2]);
 				switch (m[1]) {
 								if (!data.body) return done(new Error("Album \u00bb"+name+"\u00ab was not found."));
 								done(null,data.body.songs);
 							},
-							error: function (request, textStatus, errorThrown) {
-								done(errorThrown||new Error(textStatus));
-							}
+							error: onerror
 						});
 						break;
 
 						return done(new Error("Faild to load songs from url: "+url));
 				}
 			}
+			else if ((m = /^(?:https?:\/\/)?(?:\w+\.)?magnatune\.com\/artists\/albums\/([^\/#\?]+)([^#\?]*)/.exec(url))) {
+				var sku = m[1];
+				var songnr = m[2]||NaN;
+				if (songnr) {
+					m = /^\/(\d+)(?:[-_]\w+\.\w+)$/.exec(songnr);
+					songnr = m ? parseInt(m[1],10) : NaN;
+				}
+
+				Magnatune.Collection.request({
+					args: {action: 'album', sku: sku},
+					success: function (data) {
+						if (!data.body) return done(new Error("Album \u00bb"+sku+"\u00ab was not found."));
+						var albumname = data.body.albumname;
+						if (isNaN(songnr)) {
+							var songs = data.body.songs;
+							for (var i = 0; i < songs.length; ++ i) {
+								songs[i].albumname = albumname;
+							}
+							done(null,songs);
+						}
+						else {
+							var song = data.body.songs[songnr-1];
+							if (song) {
+								done(null,[song]);
+							}
+							else {
+								done(new Error("Album \u00bb"+albumname+"\u00ab has no song with number "+songnr+"."));
+							}
+						}
+					},
+					error: onerror
+				});
+			}
+			else if ((m = /^(?:https?:\/\/)?(?:\w+\.)?magnatune\.com\/music\/([^\/#\?]+)\/([^\/#\?]+)([^#\?]*)/.exec(url))) {
+				var artist = decodeURIComponent(m[1]);
+				var albumname = decodeURIComponent(m[2]);
+				var songnr = m[3]||NaN;
+				if (songnr) {
+					m = /^\/(\d+)(?:[-_][^\.]+\.\w+)$/.exec(songnr);
+					songnr = m ? parseInt(m[1],10) : NaN;
+				}
+
+				Magnatune.Collection.request({
+					args: {action: 'album', name: albumname},
+					success: function (data) {
+						if (!data.body) return done(new Error("Album \u00bb"+albumname+"\u00ab was not found."));
+						if (isNaN(songnr)) {
+							var songs = data.body.songs;
+							for (var i = 0; i < songs.length; ++ i) {
+								songs[i].albumname = albumname;
+							}
+							done(null,songs);
+						}
+						else {
+							var song = data.body.songs[songnr-1];
+							if (song) {
+								done(null,[song]);
+							}
+							else {
+								done(new Error("Album \u00bb"+albumname+"\u00ab has no song with number "+songnr+"."));
+							}
+						}
+					},
+					error: onerror
+				});
+			}
+			else if ((m = /^(?:https?:\/\/)?(?:\w+\.)?magnatune\.com\/all\/(\d+[^\/#\?]+)\.\w+$/.exec(url))) {
+				Magnatune.Collection.request({
+					args: {action: 'song', mp3: decodeURIComponent(m[1])+".mp3"},
+					success: function (data) {
+						if (!data.body) return done(new Error("Could not determine song of URL \u00bb"+url+"\u00ab."));
+						done(null,[data.body]);
+					},
+					error: onerror
+				});
+			}
 			else {
 				// TODO: match more urls, like:
-				// http://*.magnatune.com/artists/albums/jasmineb-mews/*
 				// http://*.magnatune.com/artists/jasmine_brunch
 				// http://*.magnatune.com/genres/ambient/
-				// http://*.magnatune.com/artists/albums/sieber-hidden/06_nospeech.m3u
 				// etc.
-				var song = this._guessSongsFromUrl(url);
+				var song = this._guessSongFromUrl(url);
 				if (!song) return done(new Error("Faild to load songs from url: "+url));
 				done(null,[song]);
 			}