Add support for matching from Youtube urls

This commit is contained in:
Jonathan Cremin 2015-01-12 17:38:42 +00:00
parent 7d3173c57e
commit 599e9b0850
12 changed files with 130 additions and 14 deletions

View file

@ -120,7 +120,11 @@ module.exports.search = function(data) {
albumClean = data.name.match(/([^\(\[]+)/)[0];
} else if (type == "track") {
query = data.artist.name + " " + data.album.name + " " + data.name;
albumClean = data.album.name.match(/([^\(\[]+)/)[0];
try {
albumClean = data.album.name.match(/([^\(\[]+)/)[0];
} catch(e) {
albumClean = "";
}
}
return rdio.apiAsync("", "", {query: query, method: 'search', types: type}).then(function(results) {
@ -129,14 +133,14 @@ module.exports.search = function(data) {
var result = results.filter(function(result) {
if (type == "album" && result.name.match(/([^\(\[]+)/)[0] == albumClean) {
return result;
} else if (type == "track" && result.album.match(/([^\(\[]+)/)[0] == albumClean) {
} else if (type == "track" && (result.album.match(/([^\(\[]+)/)[0] == albumClean || !albumClean)) {
return result;
}
}).shift();
if (!result) {
var matches = albumClean.match(/^[^\(\[]+/);
if (matches[0] && matches[0] != albumClean) {
if (matches && matches[0] && matches[0] != albumClean) {
var cleanedData = JSON.parse(JSON.stringify(data));
if (type == "album") {
cleanedData.name = matches[0].trim();

View file

@ -72,14 +72,14 @@ module.exports.search = function(data) {
query = "artist:" + data.artist.name.replace(":", "") + " album:" + data.name.replace(":", "");
album = data.name;
} else if (type == "track") {
query = "artist:" + data.artist.name.replace(":", "") + " album:" + data.album.name.replace(":", "") + " track:" + data.name.replace(":", "");
query = "artist:" + data.artist.name.replace(":", "") + " track:" + data.name.replace(":", "") + ( data.album.name.length > 0 ? " album: " + data.album.name.replace(":", ""): "");
album = data.album.name;
}
return spotify.searchAsync({query: query, type: type}).then(function(results) {
if (!results[type + "s"].items[0]) {
var matches = album.match(/^[^\(\[]+/);
if (matches[0] && matches[0] != album) {
if (matches && matches[0] && matches[0] != album) {
var cleanedData = JSON.parse(JSON.stringify(data));
if (type == "album") {
cleanedData.name = matches[0].trim();

View file

@ -0,0 +1,20 @@
"use strict";
var parse = require('url').parse;
var Promise = require('bluebird');
var request = require('superagent');
require('superagent-bluebird-promise');
var credentials = {
key: process.env.YOUTUBE_KEY,
};
var apiRoot = "https://www.googleapis.com/freebase/v1/topic";
module.exports.get = function(topic) {
return request.get(apiRoot + topic + "?key=" + credentials.key).promise().then(function(res) {
return res.body;
})
}
module.exports.get("/m/0dwcrm_");

View file

@ -1,5 +1,7 @@
"use strict";
var parse = require('url').parse;
var freebase = require('./freebase');
var querystring = require('querystring');
var Promise = require('bluebird');
var request = require('superagent');
require('superagent-bluebird-promise');
@ -19,6 +21,65 @@ var apiRoot = "https://www.googleapis.com/youtube/v3";
module.exports.match = require('./url').match;
module.exports.parseUrl = function(url) {
var parsed = parse(url);
var query = querystring.parse(parsed.query);
var id = query.v;
if (!id) {
id = parsed.path.substr(1);
if (!id) {
throw new Error();
}
}
return module.exports.lookupId(id, "track");
}
module.exports.lookupId = function(id, type) {
var path = "/videos?part=snippet%2Cstatus%2CtopicDetails&id=" + id + "&key=" + credentials.key;
return request.get(apiRoot + path).promise().then(function(res) {
var item = res.body.items[0];
if (item.topicDetails.topicIds) {
var promises = [];
item.topicDetails.topicIds.forEach(function(topicId) {
promises.push(freebase.get(topicId).then(function(topic) {
return topic.property["/music/recording/song"] ? topic : false;
}, function(err) {
console.log(err)
}));
})
return Promise.all(promises).then(function(topics) {
for (var key in topics) {
var topic = topics[key];
if (topic) {
console.log(topic.property['/music/recording/song'])
return {
id: id,
service: "youtube",
type: "track",
name: topic.property['/music/recording/song'].values[0].text,
artist: {name: topic.property['/music/recording/artist'].values[0].text},
album: {name: ""},
streamUrl: "https://youtu.be/" + id,
purchaseUrl: null,
artwork: {
small: item.snippet.thumbnails.medium.url,
large: item.snippet.thumbnails.high.url,
}
}
}
}
});
} else {
return {service: "youtube"};
}
}, function(res) {
return {service: "youtube"};
});
};
module.exports.search = function(data) {
var query, album;
var type = data.type;

View file

@ -1,5 +1,15 @@
"use strict";
var parse = require("url").parse;
var querystring = require('querystring');
module.exports.match = function(url, type) {
var parsed = parse(url);
if (parsed.host.match(/youtu\.be$/)) {
return true;
} else if (parsed.host.match(/youtube\.com$/)) {
var query = querystring.parse(parsed.query);
return !!query.v;
}
return false;
};