Refactor searching
This commit is contained in:
parent
87649b5a3a
commit
bbcbe8d871
9 changed files with 155 additions and 122 deletions
|
@ -10,17 +10,21 @@ module.exports = function(req, res, next) {
|
|||
req.db.matches.find().sort({created_at:-1}).limit(6).toArray().then(function(docs){
|
||||
var recents = [];
|
||||
docs.forEach(function(doc) {
|
||||
if (doc._id.indexOf("$$") > -1) {
|
||||
recents.push(doc.services[doc._id.split("$$")[0]]);
|
||||
}
|
||||
var shares = Object.keys(doc.services).map(function (key) {return doc.services[key]});
|
||||
shares.some(function(item) {
|
||||
if (item.service == doc._id.split("$$")[0]) {
|
||||
recents.push(item);
|
||||
return false;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
Router.run(routes, req.url, function (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>"));
|
||||
});
|
||||
}).catch(function(err) {
|
||||
console.log(err)
|
||||
}).catch(function(error) {
|
||||
next(error);
|
||||
});
|
||||
}
|
|
@ -1,60 +1,36 @@
|
|||
"use strict";
|
||||
var parse = require('url').parse;
|
||||
var path = require('path');
|
||||
|
||||
var 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;
|
||||
}
|
||||
});
|
||||
var lookup = require('../lib/lookup');
|
||||
var services = require('../lib/services');
|
||||
|
||||
module.exports = function(req, res, next) {
|
||||
var url = parse(req.body.url);
|
||||
var searching = false;
|
||||
|
||||
if (!url.host) {
|
||||
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);
|
||||
if (matched) {
|
||||
searching = true;
|
||||
services[id].parseUrl(req.body.url).timeout(10000).then(function(result) {
|
||||
if (!result.id) {
|
||||
res.json({error:{message:"No match found for url"}});
|
||||
} else {
|
||||
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.json(item);
|
||||
});
|
||||
});
|
||||
}
|
||||
}, function(error) {
|
||||
if (error.code == "ETIMEDOUT") {
|
||||
error = new Error("Error talking to music service");
|
||||
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;
|
||||
return res.json({error:{message:"You need to submit a url."}});
|
||||
}
|
||||
|
||||
lookup(req.body.url).then(function(item) {
|
||||
item.matched_at = new Date();
|
||||
var matches = {};
|
||||
matches[item.service] = item;
|
||||
services.forEach(function(service) {
|
||||
if (service.id == item.service) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (url.host && !searching) {
|
||||
res.json({error:{message:"No match found for url"}});
|
||||
}
|
||||
matches[service.id] = {service: service.id};
|
||||
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);
|
||||
});
|
||||
}, function(error) {
|
||||
console.log(error.stack)
|
||||
res.json({error: "No matches found for url"});
|
||||
});
|
||||
};
|
||||
|
|
|
@ -9,72 +9,36 @@ var Router = require('react-router');
|
|||
var nodejsx = require('node-jsx').install();
|
||||
var routes = require('../views/app.jsx').routes;
|
||||
|
||||
var 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;
|
||||
}
|
||||
});
|
||||
var services = require('../lib/services');
|
||||
|
||||
|
||||
module.exports = 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;
|
||||
var matchedService;
|
||||
services.some(function(service) {
|
||||
matchedService = serviceId == service.id ? service : null;
|
||||
return matchedService;
|
||||
});
|
||||
|
||||
if (!matchedService || (type != "album" && type != "track")) {
|
||||
return next();
|
||||
}
|
||||
|
||||
req.db.matches.findOne({_id:serviceId + "$$" + itemId}, function(err, doc) {
|
||||
if (err) {
|
||||
return next(new Error());
|
||||
} else if (!doc) {
|
||||
return next();
|
||||
return req.db.matches.findOne({_id:serviceId + "$$" + itemId}).then(function(doc) {
|
||||
var shares = Object.keys(doc.services).map(function (key) {return doc.services[key]});
|
||||
if (req.params.format == "json") {
|
||||
return res.json({shares:shares});
|
||||
}
|
||||
var shares = [];
|
||||
for (var docService in Object.keys(services)) {
|
||||
var loopServiceId = Object.keys(services)[docService];
|
||||
shares.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 shares = shares.filter(function(item) {
|
||||
return item.service != serviceId;
|
||||
Router.run(routes, req.url, function (Handler) {
|
||||
var App = React.createFactory(Handler);
|
||||
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>"));
|
||||
});
|
||||
|
||||
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) {
|
||||
var App = React.createFactory(Handler);
|
||||
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>"));
|
||||
});
|
||||
}
|
||||
|
||||
}).catch(function (error) {
|
||||
return next(error);
|
||||
});
|
||||
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue