Render share page before all matches are found.
This commit is contained in:
parent
6f3e8b06a6
commit
4bfdf0dc45
7 changed files with 81 additions and 64 deletions
3
app.js
3
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
|
||||
|
|
|
@ -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(/^[^\(\[]+/);
|
||||
|
|
|
@ -24,7 +24,6 @@ footer, .page-wrap:after {
|
|||
height: 100px;
|
||||
}
|
||||
footer {
|
||||
background: #eee;
|
||||
line-height: 100px;
|
||||
text-align: right;
|
||||
}
|
||||
|
|
|
@ -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") {
|
||||
|
|
115
routes/share.js
115
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);
|
||||
});
|
||||
};
|
|
@ -12,19 +12,19 @@
|
|||
<div class="row">
|
||||
<div class="col-md-9 col-sm-8 col-xs-12">
|
||||
<h3>Matched albums for</h3>
|
||||
<h2><%= items[0].name %> <span class="artist-lighten">- <%= items[0].artist.name %></span></h2>
|
||||
<h2><%= matching.name %> <span class="artist-lighten">- <%= matching.artist.name %></span></h2>
|
||||
</div>
|
||||
<div class="col-md-3 col-sm-4 hidden-xs">
|
||||
<ul class="list-inline share-tools">
|
||||
<li>Share this</li>
|
||||
<li><a href="http://twitter.com/intent/tweet/?text=Check out <%= items[0].name + " by " + items[0].artist.name %>&url=<%= thisUrl %>&via=MatchAudio" class="share-dialog"><img src="/images/twitter.png" alt="Twitter" /></a></li>
|
||||
<li><a href="http://twitter.com/intent/tweet/?text=Check out <%= matching.name + " by " + matching.artist.name %>&url=<%= thisUrl %>&via=MatchAudio" class="share-dialog"><img src="/images/twitter.png" alt="Twitter" /></a></li>
|
||||
<li><a href="http://www.facebook.com/sharer/sharer.php?p[url]=<%= thisUrl %>" class="share-dialog"><img src="/images/facebook.png" alt="Facebook" /></a></li>
|
||||
<li><a href="https://plus.google.com/share?url=<%= thisUrl %>" class="share-dialog"><img src="/images/googleplus.png" alt="Google+" /></a></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
<div class="row">
|
||||
<% for (var i=0;i < items.length;i++) { var album = items[i]; %>
|
||||
<% for (var i=0;i < matches.length;i++) { var album = matches[i]; %>
|
||||
<% if (album.type != "video") { %><div class="col-md-3 col-xs-6"><% } else { %><div class="col-md-6 col-xs-12"><% } %>
|
||||
<div class="service <%= i==0 ? "source-service" : "" %>">
|
||||
<div class="matching-from hidden-xs"><%= i==0 ? "Found matches using this link" : "" %></div>
|
||||
|
@ -32,7 +32,7 @@
|
|||
<div class="js-video widescreen">
|
||||
<iframe width="100%" src="//www.youtube.com/embed/<%= album.id %>" frameborder="0" allowfullscreen></iframe>
|
||||
</div>
|
||||
<a href="https://www.youtube.com/results?search_query=<%= items[0].name %> <%= items[0].artist.name %>">More Youtube matches</a>
|
||||
<a href="https://www.youtube.com/results?search_query=<%= matching.name %> <%= matching.artist.name %>">More Youtube matches</a>
|
||||
<% } else if (album.streamUrl) { %>
|
||||
<a href="<%= album.streamUrl %>"><img src="<%= album.artwork.small %>" class="img-rounded album-artwork" width="100%"></a>
|
||||
<div class="service-link">
|
||||
|
@ -44,7 +44,7 @@
|
|||
<a href="<%= album.purchaseUrl %>"><img src="/images/<%= album.service %>.png" class="img-rounded"></a>
|
||||
</div>
|
||||
<% } else { %>
|
||||
<img src="<%= items[0].artwork.small %>" class="img-rounded album-artwork not-found" width="100%"></a>
|
||||
<img src="<%= matching.artwork.small %>" class="img-rounded album-artwork not-found" width="100%"></a>
|
||||
<div class="service-link">
|
||||
<img src="/images/<%= album.service %>.png" class="img-rounded not-found">
|
||||
</div>
|
||||
|
@ -57,7 +57,7 @@
|
|||
<div class="col-md-12">
|
||||
<ul class="list-inline share-tools">
|
||||
<li>Share this</li>
|
||||
<li><a href="http://twitter.com/intent/tweet/?text=Check out <%= items[0].name + " by " + items[0].artist.name %>&url=<%= thisUrl %>&via=MatchAudio" class="share-dialog"><img src="/images/twitter.png" alt="Twitter" /></a></li>
|
||||
<li><a href="http://twitter.com/intent/tweet/?text=Check out <%= matching.name + " by " + matching.artist.name %>&url=<%= thisUrl %>&via=MatchAudio" class="share-dialog"><img src="/images/twitter.png" alt="Twitter" /></a></li>
|
||||
<li><a href="http://www.facebook.com/sharer/sharer.php?p[url]=<%= thisUrl %>" class="share-dialog"><img src="/images/facebook.png" alt="Facebook" /></a></li>
|
||||
<li><a href="https://plus.google.com/share?url=<%= thisUrl %>" class="share-dialog"><img src="/images/googleplus.png" alt="Google+" /></a></li>
|
||||
</ul>
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
<div class="col-md-6 col-md-offset-3">
|
||||
<% if (recent.length) { %><h2>Recently Shared</h2><% } %>
|
||||
<div class="row recent">
|
||||
<% for (var i=0;i < recent.length;i++) { var item = recent[i].items[0]; %>
|
||||
<% for (var i=0;i < recent.length;i++) { var item = recent[i].services[recent[i]._id.split('$$')[0]]; %>
|
||||
<div class="col-sm-4 col-xs-6">
|
||||
<a href="/<%= item.service.replace("playmusic", "") %>/<%= item.type %>/<%= item.id %>"><img src="<%= item.artwork.small ? item.artwork.small : item.artwork %>" width="100%"></a>
|
||||
</div>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue