diff --git a/app.js b/app.js index 745ee8f..3910243 100644 --- a/app.js +++ b/app.js @@ -66,7 +66,8 @@ app.get('/', function(req, res) { }); app.post('/search', search); -app.get('/:service/:type/:id', share); +app.get('/:service/:type/:id.json', share.json); +app.get('/:service/:type/:id', share.html); app.get('/itunes/*', itunesProxy); // catch 404 and forward to error handler diff --git a/lib/services/beats/index.js b/lib/services/beats/index.js index 1df7af1..ee9ce21 100644 --- a/lib/services/beats/index.js +++ b/lib/services/beats/index.js @@ -109,6 +109,7 @@ module.exports.search = function(data) { } var path = "/search?q=" + encodeURIComponent(query) + "&type=" + type + "&client_id=" + credentials.key; + return request.get(apiRoot + path).promise().then(function(res) { if (!res.body.data[0]) { var matches = album.match(/^[^\(\[]+/); diff --git a/public/stylesheets/style.css b/public/stylesheets/style.css index 604b975..c8c6721 100644 --- a/public/stylesheets/style.css +++ b/public/stylesheets/style.css @@ -24,7 +24,6 @@ footer, .page-wrap:after { height: 100px; } footer { - background: #eee; line-height: 100px; text-align: right; } diff --git a/routes/search.js b/routes/search.js index 1f3f164..c3e34c6 100644 --- a/routes/search.js +++ b/routes/search.js @@ -19,6 +19,10 @@ module.exports = function(req, res, next) { req.flash('search-error', 'Paste a music link above to find and share the matches'); res.redirect('/'); } else { + var items = {}; + for (var id in services) { + items[id] = {service: id}; + } for (var id in services) { var matched = services[id].match(req.body.url); if (matched) { @@ -28,7 +32,12 @@ module.exports = function(req, res, next) { req.flash('search-error', 'No match found for this link'); res.redirect('/'); } else { - res.redirect("/" + id + "/" + result.type + "/" + result.id); + services[id].lookupId(result.id, result.type).then(function(item) { + items[id] = item; + req.db.matches.save({_id:id + "$$" + result.id, created_at: new Date(), services:items}).then(function() { + res.redirect("/" + id + "/" + result.type + "/" + result.id); + }); + }); } }, function(error) { if (error.code == "ETIMEDOUT") { diff --git a/routes/share.js b/routes/share.js index b71b44f..1cf6b37 100644 --- a/routes/share.js +++ b/routes/share.js @@ -1,7 +1,7 @@ "use strict"; var path = require('path'); var Promise = require('bluebird'); - +var util = require('util'); var services = {}; require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach(function(file) { @@ -12,7 +12,7 @@ require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach }); -module.exports = function(req, res, next) { +module.exports.html = function(req, res, next) { var serviceId = req.params.service; var type = req.params.type; var itemId = req.params.id; @@ -23,58 +23,65 @@ module.exports = function(req, res, next) { return; } - req.db.matches.findOne({_id:serviceId + "-" + itemId}).then(function(doc) { - if (doc) { - res.render(type, { - page: type, - title: doc.items[0].name + " by " + doc.items[0].artist.name, - items: doc.items, - thisUrl: req.userProtocol + '://' + req.get('host') + req.originalUrl - }); - } else { - services[serviceId].lookupId(itemId, type).timeout(10000).then(function(item) { - for (var id in services) { - if (id != serviceId) { - promises.push(services[id].search(item).timeout(10000)); - } - } - - Promise.settle(promises).then(function(results) { - var items = results.map(function(result) { - if (result.isFulfilled()) { - return result.value(); - } - }).filter(function(result) { - return result || false; - }); - - items.sort(function(a, b) { - return !a.id || !b.id; - }).sort(function(a, b) { - return !a.streamUrl || b.streamUrl; - }).sort(function(a, b) { - return a.type == "video" && b.type != "video"; - }); - - items.unshift(item); - req.db.matches.save({_id:serviceId + "-" + itemId, items:items}); - res.render(type, { - page: type, - title: item.name + " by " + item.artist.name, - items: items, - thisUrl: req.userProtocol + '://' + req.get('host') + req.originalUrl - }); - }); - }, function(error) { - if (error.code == "ETIMEDOUT") { - error = new Error("Error talking to music service"); - error.status = "502"; - } else if (!error.status) { - error = new Error("An unexpected error happenend"); - error.status = 500; - } - next(error); - }); + req.db.matches.findOne({_id:serviceId + "$$" + itemId}, function(err, doc) { + if (err) { + return next(new Error()); } + var items = []; + for (var docService in Object.keys(services)) { + var loopServiceId = Object.keys(services)[docService]; + items.push(doc.services[loopServiceId]); + if (doc.services[loopServiceId].id === undefined) { + services[loopServiceId].search(doc.services[serviceId]).timeout(15000).then(function(item) { + if (!item.id) { + item.id = null; + } + + var set = {}; + set["services." + item.service] = item; + req.db.matches.update({_id: serviceId + "$$" + itemId}, {$set: set}); + }).catch(function(err) { + console.log(err) + }); + } + } + + var items = items.filter(function(item) { + return item.service != serviceId; + }); + + items.sort(function(a, b) { + return !a.id || !b.id; + }).sort(function(a, b) { + return !a.streamUrl || b.streamUrl; + }).sort(function(a, b) { + return a.type == "video" && b.type != "video"; + }); + + items.unshift(doc.services[serviceId]); + + res.render(type, { + page: type, + title: doc.services[serviceId].name + " by " + doc.services[serviceId].artist.name, + matching: doc.services[serviceId], + matches: items, + thisUrl: req.userProtocol + '://' + req.get('host') + req.originalUrl + }); }); }; + +module.exports.json = function(req, res, next) { + var serviceId = req.params.service; + var type = req.params.type; + var itemId = req.params.id; + var promises = []; + + if (!services[serviceId] || (type != "album" && type != "track")) { + next(); + return; + } + + req.db.matches.findOne({_id:serviceId + "$$" + itemId}, function(err, doc) { + res.json(doc); + }); +}; \ No newline at end of file diff --git a/views/album.ejs b/views/album.ejs index b40c689..a887df4 100644 --- a/views/album.ejs +++ b/views/album.ejs @@ -12,19 +12,19 @@