CyTube/get-info.js

400 lines
13 KiB
JavaScript
Raw Normal View History

2013-03-24 02:28:20 +00:00
/*
The MIT License (MIT)
Copyright (c) 2013 Calvin Montgomery
2013-05-02 15:28:29 +00:00
2013-03-24 02:28:20 +00:00
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
2013-05-02 15:28:29 +00:00
2013-03-24 02:28:20 +00:00
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2013-05-02 15:28:29 +00:00
2013-03-24 02:28:20 +00:00
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
2013-02-16 17:19:59 +00:00
var http = require("http");
var https = require("https");
2013-03-27 19:28:51 +00:00
var Logger = require("./logger.js");
2013-04-03 03:56:44 +00:00
var Media = require("./media.js").Media;
2013-02-16 05:02:42 +00:00
// Helper function for making an HTTP request and getting the result
// as JSON
2013-07-14 02:25:34 +00:00
function getJSONInternal(transport, options, callback) {
var req = transport.request(options, function(res) {
var buffer = "";
res.setEncoding("utf8");
res.on("data", function (chunk) {
2013-02-16 05:02:42 +00:00
buffer += chunk;
});
res.on("end", function() {
2013-03-18 01:07:18 +00:00
try {
var data = JSON.parse(buffer);
}
catch(e) {
2013-06-26 03:09:37 +00:00
var m = buffer.match(/<internalReason>([^<]+)<\/internalReason>/);
2013-07-10 02:55:40 +00:00
if(m === null)
m = buffer.match(/<code>([^<]+)<\/code>/);
if(m === null)
m = buffer.match(/([0-9]+ not found)/);
2013-07-14 02:25:34 +00:00
Logger.errlog.log("Media request failed: "+options.host+options.path);
if(m) {
2013-07-16 15:46:09 +00:00
if(m[1] === "too_many_recent_calls") {
m[1] = "YouTube is throttling the server right "+
"now for making too many requests. "+
"Please try again in a moment.";
}
2013-07-14 02:25:34 +00:00
Logger.errlog.log("Reason: " + m[1]);
callback(m[1], res.statusCode, null);
}
else {
callback(true, res.statusCode, null);
}
2013-05-19 17:06:39 +00:00
return;
}
callback(false, res.statusCode, data);
2013-03-23 18:17:39 +00:00
});
});
req.end();
};
2013-07-14 02:25:34 +00:00
function getJSON(options, callback) {
getJSONInternal(http, options, callback);
}
2013-02-16 05:02:42 +00:00
2013-07-14 02:25:34 +00:00
function getJSONHTTPS(options, callback) {
getJSONInternal(https, options, callback);
}
2013-02-16 05:02:42 +00:00
// Look up YouTube metadata
// Fairly straightforward
exports.getYTInfo = function(id, callback) {
getJSON({
host: "gdata.youtube.com",
port: 80,
path: "/feeds/api/videos/" + id + "?v=2&alt=json",
method: "GET",
dataType: "jsonp",
timeout: 1000}, callback);
}
2013-07-02 15:38:13 +00:00
// Look up a YouTube playlist
2013-04-03 18:18:10 +00:00
exports.getYTPlaylist = function(id, callback, url) {
var path = "/feeds/api/playlists/" + id + "?v=2&alt=json";
if(url) {
path = "/" + url.split("gdata.youtube.com")[1];
}
2013-03-25 19:39:03 +00:00
getJSON({
host: "gdata.youtube.com",
port: 80,
2013-04-03 18:18:10 +00:00
path: path,
2013-03-25 19:39:03 +00:00
method: "GET",
dataType: "jsonp",
timeout: 1000}, callback);
}
2013-07-02 15:38:13 +00:00
// Search YouTube
exports.searchYT = function(terms, callback) {
2013-05-02 15:28:29 +00:00
// I really miss Python's list comprehensions
for(var i = 0; i < terms.length; i++) {
terms[i] = escape(terms[i]);
}
2013-05-05 23:52:16 +00:00
var query = terms.join("+");
getJSON({
host: "gdata.youtube.com",
port: 80,
2013-05-05 23:52:16 +00:00
path: "/feeds/api/videos/?q=" + query + "&v=2&alt=json",
method: "GET",
dataType: "jsonp",
timeout: 1000}, callback);
}
exports.getYTSearchResults = function(query, callback) {
2013-05-19 17:06:39 +00:00
var cback = function(err, res, data) {
if(err || res != 200) {
callback(true, null);
return;
}
var vids = [];
try {
2013-05-05 23:52:16 +00:00
if(data.feed.entry.length === undefined) {
return;
}
for(var i = 0; i < data.feed.entry.length; i++) {
try {
var item = data.feed.entry[i];
var id = item.media$group.yt$videoid.$t;
var title = item.title.$t;
var seconds = item.media$group.yt$duration.seconds;
var media = new Media(id, title, seconds, "yt");
media.thumb = item.media$group.media$thumbnail[0];
vids.push(media);
}
catch(e) {
Logger.errlog.log("getYTSearchResults failed: ");
Logger.errlog.log(e);
}
}
}
catch(e) {
Logger.errlog.log("getYTSearchResults failed: ");
Logger.errlog.log(e);
}
2013-05-19 17:06:39 +00:00
callback(false, vids);
}
2013-05-02 15:28:29 +00:00
exports.searchYT(query.split(" "), cback);
}
2013-02-16 05:02:42 +00:00
// Look up Soundcloud metadata
exports.getSCInfo = function(url, callback) {
const SC_CLIENT = "2e0c82ab5a020f3a7509318146128abd";
2013-02-16 05:02:42 +00:00
getJSON({
host: "api.soundcloud.com",
port: 80,
path: "/resolve.json?url="+url+"&client_id=" + SC_CLIENT,
method: "GET",
dataType: "jsonp",
2013-06-02 04:21:23 +00:00
timeout: 1000}, function(err, status, data) {
2013-02-16 05:02:42 +00:00
// This time we can ACTUALLY get the data we want
getJSON({
host: "api.soundcloud.com",
port: 80,
path: data.location,
method: "GET",
dataType: "jsonp",
timeout: 1000}, callback);
});
}
// Look up Vimeo metadata. Fairly straightforward
exports.getVIInfo = function(id, callback) {
getJSON({
host: "vimeo.com",
port: 80,
path: "/api/v2/video/" + id + ".json",
method: "GET",
dataType: "jsonp",
timeout: 1000}, callback);
}
2013-03-23 18:17:39 +00:00
// Look up Dailymotion info
exports.getDMInfo = function(id, callback) {
var fields = "duration,title"
getJSONHTTPS({
host: "api.dailymotion.com",
port: 443,
path: "/video/" + id + "?fields=" + fields,
method: "GET",
dataType: "jsonp",
timeout: 1000}, callback);
}
2013-04-03 03:56:44 +00:00
2013-05-14 21:22:47 +00:00
// Ustream requires a developer key for their API, and on top of that
// I couldn't figure out how to use it.
// I'm regexing the stream ID out of the HTML
// So sue me
exports.getUstream = function(name, callback) {
var opts = {
host: "www.ustream.tv",
port: 80,
path: "/" + name
};
http.get(opts, function(res) {
var html = "";
res.on("data", function(cnk) {
html += cnk;
});
res.on("end", function() {
2013-07-12 20:07:11 +00:00
if(res.statusCode != 200) {
callback(true, null);
return;
}
2013-05-14 21:22:47 +00:00
var lines = html.split("\n");
const re = /cid":([0-9]+)/;
2013-07-12 20:07:11 +00:00
var m = html.match(re);
if(m) {
callback(false, m[1]);
}
else {
callback(true, null);
}
return;
2013-05-14 21:22:47 +00:00
for(var i = 0; i < lines.length; i++) {
var m = lines[i].match(re);
if(m) {
callback(m[1]);
return;
}
}
});
}).on("error", function(err) {
Logger.errlog.log(err.message);
});
}
2013-04-03 03:56:44 +00:00
exports.getMedia = function(id, type, callback) {
switch(type) {
case "yt":
2013-05-19 17:06:39 +00:00
exports.getYTInfo(id, function(err, res, data) {
if(err || res != 200) {
2013-06-26 03:09:37 +00:00
callback(err || true, null);
2013-04-03 03:56:44 +00:00
return;
}
try {
// Whoever named this should be fired
var seconds = data.entry.media$group.yt$duration.seconds;
var title = data.entry.title.$t;
var media = new Media(id, title, seconds, "yt");
2013-05-19 17:06:39 +00:00
callback(false, media);
2013-04-03 03:56:44 +00:00
}
catch(e) {
Logger.errlog.log("getMedia failed: ");
2013-07-01 22:45:55 +00:00
Logger.errlog.log(e.stack);
2013-05-19 17:06:39 +00:00
callback(true, null);
2013-04-03 03:56:44 +00:00
}
});
break;
case "vi":
2013-05-19 17:06:39 +00:00
exports.getVIInfo(id, function(err, res, data) {
if(err || res != 200) {
2013-07-10 02:55:40 +00:00
callback(err || true, null);
2013-04-03 03:56:44 +00:00
return;
}
try {
data = data[0];
var seconds = data.duration;
var title = data.title;
var media = new Media(id, title, seconds, "vi");
2013-05-19 17:06:39 +00:00
callback(false, media);
2013-04-03 03:56:44 +00:00
}
catch(e) {
Logger.errlog.log("getMedia failed: ");
Logger.errlog.log(e);
2013-05-19 17:06:39 +00:00
callback(true, null);
2013-04-03 03:56:44 +00:00
}
});
break;
case "dm":
2013-05-19 17:06:39 +00:00
exports.getDMInfo(id, function(err, res, data) {
if(err || res != 200) {
callback(true, null);
2013-04-03 03:56:44 +00:00
return;
}
try {
var seconds = data.duration;
var title = data.title;
var media = new Media(id, title, seconds, "dm");
2013-05-19 17:06:39 +00:00
callback(false, media);
2013-04-03 03:56:44 +00:00
}
catch(e) {
Logger.errlog.log("getMedia failed: ");
Logger.errlog.log(e);
2013-05-19 17:06:39 +00:00
callback(true, null);
2013-04-03 03:56:44 +00:00
}
});
break;
case "sc":
2013-05-19 17:06:39 +00:00
exports.getSCInfo(id, function(err, res, data) {
if(err || res != 200) {
callback(true, null);
2013-04-03 03:56:44 +00:00
return;
}
try {
// Soundcloud's durations are in ms
var seconds = data.duration / 1000;
var title = data.title;
var media = new Media(id, title, seconds, "sc");
2013-05-19 17:06:39 +00:00
callback(false, media);
2013-04-03 03:56:44 +00:00
}
catch(e) {
Logger.errlog.log("getMedia failed: ");
Logger.errlog.log(e);
2013-05-19 17:06:39 +00:00
callback(true, null);
2013-04-03 03:56:44 +00:00
}
});
break;
2013-04-03 18:18:10 +00:00
case "yp":
2013-05-19 17:06:39 +00:00
var cback = function(err, res, data) {
if(err || res != 200) {
callback(true, null);
2013-04-03 18:18:10 +00:00
return;
}
try {
2013-07-03 15:26:10 +00:00
var vids = [];
2013-04-03 18:18:10 +00:00
for(var i = 0; i < data.feed.entry.length; i++) {
try {
var item = data.feed.entry[i];
var id = item.media$group.yt$videoid.$t;
var title = item.title.$t;
var seconds = item.media$group.yt$duration.seconds;
var media = new Media(id, title, seconds, "yt");
2013-07-03 15:26:10 +00:00
vids.push(media);
2013-04-03 18:18:10 +00:00
}
catch(e) {
Logger.errlog.log("getMedia failed: ");
Logger.errlog.log(e);
}
}
2013-07-03 15:26:10 +00:00
callback(false, vids);
2013-04-03 18:18:10 +00:00
var links = data.feed.link;
for(var i = 0; i < links.length; i++) {
if(links[i].rel == "next") {
exports.getYTPlaylist(id, cback, links[i].href);
}
}
}
catch(e) {
Logger.errlog.log("getMedia failed: ");
Logger.errlog.log(e);
2013-05-19 17:06:39 +00:00
callback(true, null);
2013-04-03 18:18:10 +00:00
}
}
exports.getYTPlaylist(id, cback);
2013-07-03 15:26:10 +00:00
break;
2013-07-02 15:38:13 +00:00
case "li":
case "tw":
case "jt":
case "jw":
const prefix = {
"li": "Livestream.com - ",
"tw": "Twitch.tv - ",
"jt": "Justin.tv - ",
"jw": "JWPlayer Stream - "
};
2013-07-06 05:45:15 +00:00
var media = new Media(id, prefix[type] + id, "--:--", type);
2013-07-02 15:38:13 +00:00
callback(false, media);
break;
2013-07-12 20:07:11 +00:00
case "us":
exports.getUstream(id, function(err, data) {
if(err) {
callback(true, null);
return;
}
var media = new Media(data, "Ustream.tv - " + id, "--:--", "us");
callback(false, media);
});
break;
2013-07-02 15:38:13 +00:00
case "rt":
case "im":
const names = {
"rt": "Livestream",
"im": "Imgur Album"
};
2013-07-06 05:45:15 +00:00
var media = new Media(id, names[type], "--:--", type);
2013-07-02 15:38:13 +00:00
callback(false, media);
break;
2013-04-03 03:56:44 +00:00
default:
break;
}
}