Refactor searching
This commit is contained in:
parent
87649b5a3a
commit
bbcbe8d871
9 changed files with 155 additions and 122 deletions
13
app.js
13
app.js
|
@ -2,7 +2,6 @@
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var helmet = require('helmet');
|
var helmet = require('helmet');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var url = require('url');
|
|
||||||
var favicon = require('serve-favicon');
|
var favicon = require('serve-favicon');
|
||||||
var logger = require('morgan');
|
var logger = require('morgan');
|
||||||
var session = require('express-session');
|
var session = require('express-session');
|
||||||
|
@ -79,15 +78,22 @@ app.get('*', function(req,res,next) {
|
||||||
|
|
||||||
app.get('/', index);
|
app.get('/', index);
|
||||||
app.post('/search', search);
|
app.post('/search', search);
|
||||||
app.get('/:service/:type/:id', share);
|
app.get('/:service/:type/:id.:format?', share);
|
||||||
app.get('/itunes/*', itunesProxy);
|
app.get('/itunes/*', itunesProxy);
|
||||||
app.get('/recent', function(req, res, next) {
|
app.get('/recent', function(req, res, next) {
|
||||||
req.db.matches.find().sort({created_at:-1}).limit(6).toArray().then(function(docs){
|
req.db.matches.find().sort({created_at:-1}).limit(6).toArray().then(function(docs){
|
||||||
var recents = [];
|
var recents = [];
|
||||||
docs.forEach(function(doc) {
|
docs.forEach(function(doc) {
|
||||||
recents.push(doc.services[doc._id.split("$$")[0]]);
|
doc.services.some(function(item) {
|
||||||
|
if (item.service == doc._id.split("$$")[0]) {
|
||||||
|
recents.push(item);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
res.json({recents:recents});
|
res.json({recents:recents});
|
||||||
|
}).catch(function (error) {
|
||||||
|
return next(error);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -104,6 +110,7 @@ app.use(function(req, res, next) {
|
||||||
// will print stacktrace
|
// will print stacktrace
|
||||||
if (app.get('env') === 'development') {
|
if (app.get('env') === 'development') {
|
||||||
app.use(function(err, req, res, next) {
|
app.use(function(err, req, res, next) {
|
||||||
|
console.log(err)
|
||||||
res.status(err.status || 500);
|
res.status(err.status || 500);
|
||||||
|
|
||||||
var content = React.renderToString(ErrorView({status: err.status || 500, message: err.message, error: err}));
|
var content = React.renderToString(ErrorView({status: err.status || 500, message: err.message, error: err}));
|
||||||
|
|
29
lib/lookup.js
Normal file
29
lib/lookup.js
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
"use strict";
|
||||||
|
var path = require('path');
|
||||||
|
|
||||||
|
var services = [];
|
||||||
|
|
||||||
|
require("fs").readdirSync(path.join(__dirname, "services")).forEach(function(file) {
|
||||||
|
var service = require(path.join(__dirname, "services", file));
|
||||||
|
if (service.search) {
|
||||||
|
services.push(service);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = function(url) {
|
||||||
|
|
||||||
|
var matchedService;
|
||||||
|
services.some(function(service) {
|
||||||
|
matchedService = service.match(url) ? service : null;
|
||||||
|
return matchedService;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (matchedService) {
|
||||||
|
return matchedService.parseUrl(url).timeout(10000).then(function(result) {
|
||||||
|
return matchedService.lookupId(result.id, result.type).then(function(item) {
|
||||||
|
return item;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
};
|
13
lib/services.js
Normal file
13
lib/services.js
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
"use strict";
|
||||||
|
var path = require('path');
|
||||||
|
var Promise = require("bluebird");
|
||||||
|
|
||||||
|
module.exports = [];
|
||||||
|
|
||||||
|
require("fs").readdirSync(path.join(__dirname, "services")).forEach(function(file) {
|
||||||
|
var service = require(path.join(__dirname, "services", file));
|
||||||
|
if (service.search) {
|
||||||
|
module.exports.push(service);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
|
@ -10,17 +10,21 @@ module.exports = function(req, res, next) {
|
||||||
req.db.matches.find().sort({created_at:-1}).limit(6).toArray().then(function(docs){
|
req.db.matches.find().sort({created_at:-1}).limit(6).toArray().then(function(docs){
|
||||||
var recents = [];
|
var recents = [];
|
||||||
docs.forEach(function(doc) {
|
docs.forEach(function(doc) {
|
||||||
if (doc._id.indexOf("$$") > -1) {
|
var shares = Object.keys(doc.services).map(function (key) {return doc.services[key]});
|
||||||
recents.push(doc.services[doc._id.split("$$")[0]]);
|
shares.some(function(item) {
|
||||||
|
if (item.service == doc._id.split("$$")[0]) {
|
||||||
|
recents.push(item);
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
Router.run(routes, req.url, function (Handler) {
|
Router.run(routes, req.url, function (Handler) {
|
||||||
var App = React.createFactory(Handler);
|
var App = React.createFactory(Handler);
|
||||||
var content = React.renderToString(App({recents: recents}));
|
var content = React.renderToString(new App({recents: recents}));
|
||||||
res.send('<!doctype html>\n' + content.replace("</body></html>", "<script>var recents = " + JSON.stringify(recents) + "</script></body></html>"));
|
res.send('<!doctype html>\n' + content.replace("</body></html>", "<script>var recents = " + JSON.stringify(recents) + "</script></body></html>"));
|
||||||
});
|
});
|
||||||
}).catch(function(err) {
|
}).catch(function(error) {
|
||||||
console.log(err)
|
next(error);
|
||||||
});
|
});
|
||||||
}
|
}
|
|
@ -1,60 +1,36 @@
|
||||||
"use strict";
|
"use strict";
|
||||||
var parse = require('url').parse;
|
var parse = require('url').parse;
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
|
var lookup = require('../lib/lookup');
|
||||||
var services = {};
|
var services = require('../lib/services');
|
||||||
|
|
||||||
require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach(function(file) {
|
|
||||||
var service = require("../lib/services/" + file);
|
|
||||||
if (service.search) {
|
|
||||||
services[service.id] = service;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = function(req, res, next) {
|
module.exports = function(req, res, next) {
|
||||||
var url = parse(req.body.url);
|
var url = parse(req.body.url);
|
||||||
var searching = false;
|
|
||||||
|
|
||||||
if (!url.host) {
|
if (!url.host) {
|
||||||
res.json({error:{message:"You need to submit a url."}});
|
return res.json({error:{message:"You need to submit a url."}});
|
||||||
} 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);
|
lookup(req.body.url).then(function(item) {
|
||||||
if (matched) {
|
item.matched_at = new Date();
|
||||||
searching = true;
|
var matches = {};
|
||||||
services[id].parseUrl(req.body.url).timeout(10000).then(function(result) {
|
matches[item.service] = item;
|
||||||
if (!result.id) {
|
services.forEach(function(service) {
|
||||||
res.json({error:{message:"No match found for url"}});
|
if (service.id == item.service) {
|
||||||
} else {
|
return;
|
||||||
services[id].lookupId(result.id, result.type).then(function(item) {
|
}
|
||||||
items[id] = item;
|
matches[service.id] = {service: service.id};
|
||||||
req.db.matches.save({_id:id + "$$" + result.id, created_at: new Date(), services:items}).then(function() {
|
service.search(item).then(function(match) {
|
||||||
|
match.matched_at = new Date();
|
||||||
|
var update = {};
|
||||||
|
update["services." + match.service] = match;
|
||||||
|
req.db.matches.update({_id: item.service + "$$" + item.id}, {"$set": update});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return req.db.matches.save({_id: item.service + "$$" + item.id, created_at: new Date(), services:matches}).then(function() {
|
||||||
res.json(item);
|
res.json(item);
|
||||||
});
|
});
|
||||||
});
|
|
||||||
}
|
|
||||||
}, function(error) {
|
}, function(error) {
|
||||||
if (error.code == "ETIMEDOUT") {
|
console.log(error.stack)
|
||||||
error = new Error("Error talking to music service");
|
res.json({error: "No matches found for url"});
|
||||||
error.status = 502;
|
|
||||||
next(error);
|
|
||||||
} else if (!error || !error.status) {
|
|
||||||
error = new Error("An unexpected error happenend");
|
|
||||||
error.status = 500;
|
|
||||||
next(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
res.json({error:{message:"No match found for url"}});
|
|
||||||
});
|
});
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (url.host && !searching) {
|
|
||||||
res.json({error:{message:"No match found for url"}});
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -9,72 +9,36 @@ var Router = require('react-router');
|
||||||
var nodejsx = require('node-jsx').install();
|
var nodejsx = require('node-jsx').install();
|
||||||
var routes = require('../views/app.jsx').routes;
|
var routes = require('../views/app.jsx').routes;
|
||||||
|
|
||||||
var services = {};
|
var services = require('../lib/services');
|
||||||
|
|
||||||
require("fs").readdirSync(path.join(__dirname, "..", "lib", "services")).forEach(function(file) {
|
|
||||||
var service = require("../lib/services/" + file);
|
|
||||||
if (service.search) {
|
|
||||||
services[service.id] = service;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
|
|
||||||
module.exports = function(req, res, next) {
|
module.exports = function(req, res, next) {
|
||||||
var serviceId = req.params.service;
|
var serviceId = req.params.service;
|
||||||
var type = req.params.type;
|
var type = req.params.type;
|
||||||
var itemId = req.params.id;
|
var itemId = req.params.id;
|
||||||
var promises = [];
|
|
||||||
|
|
||||||
if (!services[serviceId] || (type != "album" && type != "track")) {
|
var matchedService;
|
||||||
next();
|
services.some(function(service) {
|
||||||
return;
|
matchedService = serviceId == service.id ? service : null;
|
||||||
}
|
return matchedService;
|
||||||
|
});
|
||||||
|
|
||||||
req.db.matches.findOne({_id:serviceId + "$$" + itemId}, function(err, doc) {
|
if (!matchedService || (type != "album" && type != "track")) {
|
||||||
if (err) {
|
|
||||||
return next(new Error());
|
|
||||||
} else if (!doc) {
|
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
var shares = [];
|
|
||||||
for (var docService in Object.keys(services)) {
|
return req.db.matches.findOne({_id:serviceId + "$$" + itemId}).then(function(doc) {
|
||||||
var loopServiceId = Object.keys(services)[docService];
|
var shares = Object.keys(doc.services).map(function (key) {return doc.services[key]});
|
||||||
shares.push(doc.services[loopServiceId]);
|
if (req.params.format == "json") {
|
||||||
if (doc.services[loopServiceId].id === undefined) {
|
return res.json({shares:shares});
|
||||||
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 shares = shares.filter(function(item) {
|
|
||||||
return item.service != serviceId;
|
|
||||||
});
|
|
||||||
|
|
||||||
shares.sort(function(a, b) {
|
|
||||||
return a.type == "video" && b.type != "video";
|
|
||||||
});
|
|
||||||
|
|
||||||
shares.unshift(doc.services[serviceId]);
|
|
||||||
if (req.accepts(['html', 'json']) === 'json') {
|
|
||||||
req.db.matches.findOne({_id:serviceId + "$$" + itemId}, function(err, doc) {
|
|
||||||
res.json({shares:shares});
|
|
||||||
});
|
|
||||||
} else {
|
|
||||||
Router.run(routes, req.url, function (Handler) {
|
Router.run(routes, req.url, function (Handler) {
|
||||||
var App = React.createFactory(Handler);
|
var App = React.createFactory(Handler);
|
||||||
var content = React.renderToString(App({shares: shares}));
|
var content = React.renderToString(App({shares: shares}));
|
||||||
res.send('<!doctype html>\n' + content.replace("</body></html>", "<script>var shares = " + JSON.stringify(shares) + "</script></body></html>"));
|
res.send('<!doctype html>\n' + content.replace("</body></html>", "<script>var shares = " + JSON.stringify(shares) + "</script></body></html>"));
|
||||||
});
|
});
|
||||||
}
|
}).catch(function (error) {
|
||||||
|
return next(error);
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
15
test/lookup.js
Normal file
15
test/lookup.js
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
"use strict";
|
||||||
|
require('should');
|
||||||
|
|
||||||
|
var search = require("../lib/search");
|
||||||
|
|
||||||
|
describe('Search with url', function(){
|
||||||
|
|
||||||
|
it('should find album by url', function(done){
|
||||||
|
search("https://play.google.com/music/listen#/album/Bz6wrjczddcj5hurijsv6ohdoay").then(function(result) {
|
||||||
|
result.name.should.equal("Phase 5");
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
|
@ -39,25 +39,49 @@ var SearchForm = React.createClass({
|
||||||
|
|
||||||
mixins: [ Router.Navigation, Router.State ],
|
mixins: [ Router.Navigation, Router.State ],
|
||||||
|
|
||||||
|
getInitialState: function () {
|
||||||
|
return {
|
||||||
|
submitting: true
|
||||||
|
};
|
||||||
|
},
|
||||||
|
|
||||||
handleSubmit: function(e) {
|
handleSubmit: function(e) {
|
||||||
|
this.setState({
|
||||||
|
submitting: true
|
||||||
|
});
|
||||||
var that = this;
|
var that = this;
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
var url = this.refs.url.getDOMNode().value.trim();
|
var url = this.refs.url.getDOMNode().value.trim();
|
||||||
if (!url) {
|
if (!url) {
|
||||||
|
that.setState({
|
||||||
|
submitting: false
|
||||||
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
request.post('/search').send({url:url}).end(function(res) {
|
request.post('/search').send({url:url}).end(function(res) {
|
||||||
|
that.setState({
|
||||||
|
submitting: false
|
||||||
|
});
|
||||||
|
if (res.body.error) {
|
||||||
|
return alert(res.body.error.message)
|
||||||
|
}
|
||||||
that.transitionTo("share", res.body);
|
that.transitionTo("share", res.body);
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
|
componentDidMount: function () {
|
||||||
|
this.setState({
|
||||||
|
submitting: false
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
return (
|
return (
|
||||||
<form role="form" method="post" action="/search" onSubmit={this.handleSubmit}>
|
<form role="form" method="post" action="/search" onSubmit={this.handleSubmit}>
|
||||||
<div className="input-group input-group-lg">
|
<div className="input-group input-group-lg">
|
||||||
<input type="text" name="url" placeholder="Paste link here" className="form-control" autofocus ref="url" />
|
<input type="text" name="url" placeholder="Paste link here" className="form-control" autofocus ref="url" />
|
||||||
<span className="input-group-btn">
|
<span className="input-group-btn">
|
||||||
<input type="submit" className="btn btn-lg btn-custom" value="Share Music" />
|
<input type="submit" className="btn btn-lg btn-custom" value="Share Music" disabled={this.state.submitting} />
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -9,7 +9,7 @@ var Foot = require('./foot.jsx');
|
||||||
var MusicItem = React.createClass({
|
var MusicItem = React.createClass({
|
||||||
|
|
||||||
render: function() {
|
render: function() {
|
||||||
if (typeof this.props.item.id === "undefined") {
|
if (!this.props.item.matched_at) {
|
||||||
return (
|
return (
|
||||||
<div className="col-md-3 col-xs-6">
|
<div className="col-md-3 col-xs-6">
|
||||||
<div className="service">
|
<div className="service">
|
||||||
|
@ -21,7 +21,7 @@ var MusicItem = React.createClass({
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
} else if (this.props.item.id === null) {
|
} else if (!this.props.item.id) {
|
||||||
return (
|
return (
|
||||||
<div className="col-md-3 col-xs-6">
|
<div className="col-md-3 col-xs-6">
|
||||||
<div className="service">
|
<div className="service">
|
||||||
|
@ -102,17 +102,18 @@ module.exports = React.createClass({
|
||||||
var complete = this.state.shares.length > 0;
|
var complete = this.state.shares.length > 0;
|
||||||
|
|
||||||
this.state.shares.forEach(function(share) {
|
this.state.shares.forEach(function(share) {
|
||||||
if (typeof share.id === "undefined") {
|
if (typeof share.matched_at === "undefined") {
|
||||||
|
console.log(share)
|
||||||
complete = false;
|
complete = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
var getShares = function() {
|
var getShares = function() {
|
||||||
request.get(this.getPathname()).set('Accept', 'application/json').end(function(res) {
|
request.get(this.getPathname() + ".json").end(function(res) {
|
||||||
var shares = res.body.shares;
|
var shares = res.body.shares;
|
||||||
complete = true;
|
complete = true;
|
||||||
shares.forEach(function(share) {
|
shares.forEach(function(share) {
|
||||||
if (typeof share.id === "undefined") {
|
if (typeof share.matched_at === "undefined") {
|
||||||
complete = false;
|
complete = false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue