diff --git a/lib/services/xbox/index.js b/lib/services/xbox/index.js index fed0e9c..d0c276b 100644 --- a/lib/services/xbox/index.js +++ b/lib/services/xbox/index.js @@ -62,8 +62,11 @@ module.exports.parseUrl = function(url) { var parsed = parse(url); var parts = parsed.path.split("/"); var type = parts[1]; - var id = parts[4]; - + var idMatches = parts[4].match(/[\w\-]+/); + var id = idMatches[0]; + if (!id) { + return false; + } return module.exports.lookupId("music." + id, type); } diff --git a/routes/share.js b/routes/share.js index c2e6bf9..25ce1cf 100644 --- a/routes/share.js +++ b/routes/share.js @@ -11,6 +11,17 @@ var routes = require('../views/app.jsx').routes; var services = require('../lib/services'); +var formatAndSort = function(matches, serviceId) { + matches = Object.keys(matches).map(function (key) {return matches[key]}); + matches.sort(function(a, b) { + return a.id && !b.id; + }).sort(function(a, b) { + return b.type == "video"; + }).sort(function(a, b) { + return a.service != serviceId; + }); + return matches; +} module.exports = function(req, res, next) { var serviceId = req.params.service; @@ -46,22 +57,22 @@ module.exports = function(req, res, next) { }); }); return req.db.matches.save({_id: item.service + "$$" + item.id, created_at: new Date(), services:matches}).then(function() { - var shares = Object.keys(matches).map(function (key) {return matches[key]}); + var shares = formatAndSort(matches, serviceId); Router.run(routes, req.url, function (Handler) { var App = React.createFactory(Handler); - var content = React.renderToString(App({shares: shares})); + var content = React.renderToString(new App({shares: shares})); res.send('\n' + content.replace("", "")); }); }); }) } - var shares = Object.keys(doc.services).map(function (key) {return doc.services[key]}); + var shares = formatAndSort(doc.services, serviceId); if (req.params.format == "json") { return res.json({shares:shares}); } Router.run(routes, req.url, function (Handler) { var App = React.createFactory(Handler); - var content = React.renderToString(App({shares: shares})); + var content = React.renderToString(new App({shares: shares})); res.send('\n' + content.replace("", "")); }); }).catch(function (error) {