2014-12-05 16:26:01 +00:00
|
|
|
"use strict";
|
|
|
|
var parse = require('url').parse;
|
2015-01-12 17:38:42 +00:00
|
|
|
var freebase = require('./freebase');
|
|
|
|
var querystring = require('querystring');
|
2015-01-27 21:32:28 +00:00
|
|
|
var moment = require('moment');
|
2014-12-13 00:00:49 +00:00
|
|
|
var Promise = require('bluebird');
|
2014-12-05 16:26:01 +00:00
|
|
|
var request = require('superagent');
|
2014-12-13 00:00:49 +00:00
|
|
|
require('superagent-bluebird-promise');
|
2014-12-05 16:26:01 +00:00
|
|
|
|
|
|
|
module.exports.id = "youtube";
|
|
|
|
|
|
|
|
if (!process.env.YOUTUBE_KEY) {
|
|
|
|
console.warn("YOUTUBE_KEY environment variable not found, deactivating Youtube.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var credentials = {
|
|
|
|
key: process.env.YOUTUBE_KEY,
|
|
|
|
};
|
|
|
|
|
|
|
|
var apiRoot = "https://www.googleapis.com/youtube/v3";
|
|
|
|
|
2014-12-11 19:53:12 +00:00
|
|
|
module.exports.match = require('./url').match;
|
2015-01-12 17:38:42 +00:00
|
|
|
|
|
|
|
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) {
|
|
|
|
|
2015-01-27 21:32:28 +00:00
|
|
|
var path = "/videos?part=snippet%2CtopicDetails%2CcontentDetails&id=" + id + "&key=" + credentials.key;
|
2015-01-12 17:38:42 +00:00
|
|
|
|
|
|
|
return request.get(apiRoot + path).promise().then(function(res) {
|
|
|
|
var item = res.body.items[0];
|
|
|
|
if (item.topicDetails.topicIds) {
|
|
|
|
var promises = [];
|
2015-01-13 01:06:37 +00:00
|
|
|
var match = {
|
|
|
|
id: id,
|
|
|
|
service: "youtube",
|
|
|
|
name: item.snippet.title,
|
|
|
|
type: "track",
|
|
|
|
album: {name: ""},
|
|
|
|
streamUrl: "https://youtu.be/" + id,
|
|
|
|
purchaseUrl: null,
|
|
|
|
artwork: {
|
|
|
|
small: item.snippet.thumbnails.medium.url,
|
|
|
|
large: item.snippet.thumbnails.high.url,
|
|
|
|
}
|
|
|
|
};
|
2015-01-12 17:38:42 +00:00
|
|
|
item.topicDetails.topicIds.forEach(function(topicId) {
|
|
|
|
promises.push(freebase.get(topicId).then(function(topic) {
|
2015-01-13 01:06:37 +00:00
|
|
|
if (topic.property["/type/object/type"].values.some(function(value) {
|
|
|
|
return value.text == "Musical Artist";
|
|
|
|
})) {
|
|
|
|
match.artist = {name: topic.property["/type/object/name"].values[0].text};
|
|
|
|
} else if (topic.property["/type/object/type"].values.some(function(value) {
|
|
|
|
return value.text == "Musical Recording";
|
|
|
|
})) {
|
2015-01-27 21:32:28 +00:00
|
|
|
//if (moment.duration(item.contentDetails.duration).asSeconds() < 900) {
|
2015-01-13 01:06:37 +00:00
|
|
|
match.name = topic.property["/type/object/name"].values[0].text;
|
2015-01-27 21:32:28 +00:00
|
|
|
if (topic.property["/music/recording/releases"]) {
|
|
|
|
match.type = "album";
|
|
|
|
match.album.name = topic.property["/music/recording/releases"].values[0].text;
|
|
|
|
}
|
|
|
|
//}
|
2015-01-13 01:06:37 +00:00
|
|
|
} else if (topic.property["/type/object/type"].values.some(function(value) {
|
|
|
|
return value.text == "Musical Album";
|
|
|
|
})) {
|
|
|
|
match.name = topic.property["/type/object/name"].values[0].text;
|
|
|
|
match.type = "album";
|
|
|
|
}
|
2015-01-12 17:38:42 +00:00
|
|
|
}, function(err) {
|
|
|
|
console.log(err)
|
|
|
|
}));
|
2015-01-13 01:06:37 +00:00
|
|
|
});
|
|
|
|
return Promise.all(promises).then(function() {
|
|
|
|
return match;
|
|
|
|
}, function(err) {
|
|
|
|
console.log(err)
|
|
|
|
return {service: "youtube"};
|
2015-01-12 17:38:42 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return {service: "youtube"};
|
|
|
|
}
|
2015-01-13 01:06:37 +00:00
|
|
|
}, function(err) {
|
|
|
|
console.log(err)
|
2015-01-12 17:38:42 +00:00
|
|
|
return {service: "youtube"};
|
|
|
|
});
|
|
|
|
};
|
2014-12-05 16:26:01 +00:00
|
|
|
|
|
|
|
module.exports.search = function(data) {
|
|
|
|
var query, album;
|
|
|
|
var type = data.type;
|
|
|
|
|
|
|
|
if (type == "album") {
|
|
|
|
query = data.artist.name + " " + data.name;
|
|
|
|
album = data.name;
|
|
|
|
} else if (type == "track") {
|
2014-12-05 23:47:27 +00:00
|
|
|
query = data.artist.name + " " + data.name;
|
2014-12-05 16:26:01 +00:00
|
|
|
album = data.album.name
|
|
|
|
}
|
|
|
|
|
2014-12-11 19:53:12 +00:00
|
|
|
var path = "/search?part=snippet&q=" + encodeURIComponent(query) + "&type=video&videoCaption=any&videoCategoryId=10&key=" + credentials.key;
|
2014-12-05 23:47:27 +00:00
|
|
|
|
2014-12-13 00:00:49 +00:00
|
|
|
return request.get(apiRoot + path).promise().then(function(res) {
|
2014-12-05 16:26:01 +00:00
|
|
|
var result = res.body.items[0];
|
2014-12-05 23:47:27 +00:00
|
|
|
|
|
|
|
if (!result) {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {service:"youtube", type: "video"};
|
2014-12-05 23:47:27 +00:00
|
|
|
} else {
|
2014-12-13 00:00:49 +00:00
|
|
|
return {
|
2014-12-05 23:47:27 +00:00
|
|
|
service: "youtube",
|
|
|
|
type: "video",
|
|
|
|
id: result.id.videoId,
|
|
|
|
name: result.snippet.title,
|
|
|
|
streamUrl: "https://www.youtube.com/watch?v=" + result.id.videoId,
|
|
|
|
purchaseUrl: null,
|
2014-12-13 12:05:47 +00:00
|
|
|
artwork: {
|
|
|
|
small: result.snippet.thumbnails.medium.url,
|
|
|
|
large: result.snippet.thumbnails.high.url,
|
|
|
|
}
|
2014-12-13 00:00:49 +00:00
|
|
|
};
|
2014-12-05 23:47:27 +00:00
|
|
|
}
|
2015-01-13 01:06:37 +00:00
|
|
|
}, function(err) {
|
|
|
|
console.log(err)
|
|
|
|
return {service: "youtube"};
|
2014-12-05 16:26:01 +00:00
|
|
|
});
|
|
|
|
};
|