Error handling for google+

This commit is contained in:
Calvin Montgomery 2014-07-13 11:29:50 -07:00
parent 937ad04967
commit 69772bf2ec
1 changed files with 81 additions and 65 deletions

View File

@ -810,90 +810,106 @@ var Getters = {
case 200: case 200:
break; /* Request is OK, skip to handling data */ break; /* Request is OK, skip to handling data */
case 400: case 400:
return callback("Invalid request", null); return cb("Invalid request", null);
case 403: case 403:
return callback("Private video", null); return cb("Private video", null);
case 404: case 404:
return callback("Video not found", null); return cb("Video not found", null);
case 500: case 500:
case 503: case 503:
return callback("Service unavailable", null); return cb("Service unavailable", null);
default: default:
return callback("HTTP " + status, null); return cb("HTTP " + status, null);
} }
var videos = {}; try {
var duration; var videos = {};
var title; var duration;
var startReading = false; var title;
var startReadingSentinel = new RegExp('"' + idparts[2] + '"'); var startReading = false;
res.split("\n").filter(function (line) { var startReadingSentinel = new RegExp('"' + idparts[2] + '"');
/* Once the duration has been set, no more lines are relevant */ res.split("\n").filter(function (line) {
if (duration) { /* Once the duration has been set, no more lines are relevant */
return false; if (duration) {
} return false;
}
var m = line.match(/"og:image" content="([^"]+)"/); var m = line.match(/"og:image" content="([^"]+)"/);
if (m) { if (m) {
var parts = m[1].replace(/%25/g, "%") var parts = m[1].replace(/%25/g, "%")
.replace(/%2B/ig, "%20").split("/"); .replace(/%2B/ig, "%20").split("/");
title = decodeURIComponent(parts[parts.length - 1]); title = decodeURIComponent(parts[parts.length - 1]);
} }
/* Hack for only reading relevant video data */ /* Hack for only reading relevant video data */
if (line.match(new RegExp(startReadingSentinel))) { if (line.match(new RegExp(startReadingSentinel))) {
startReading = true; startReading = true;
} }
if (!startReading) { if (!startReading) {
return false; return false;
} }
m = line.match(/,(\d+),,"https:\/\/video\.googleusercontent/); m = line.match(/,(\d+),,"https:\/\/video\.googleusercontent/);
if (m) { if (m) {
duration = parseInt(parseInt(m[1]) / 1000); duration = parseInt(parseInt(m[1]) / 1000);
} }
return line.match(/videoplayback/); return line.match(/videoplayback/);
}).map(function (line) { }).map(function (line) {
var parts = line.match(/\[(\d+),(\d+),(\d+),("http:\/\/redirector.*?")\]/); var parts = line.match(/\[(\d+),(\d+),(\d+),("http:\/\/redirector.*?")\]/);
return { return {
format: parseInt(parts[1]), format: parseInt(parts[1]),
width: parseInt(parts[2]), width: parseInt(parts[2]),
height: parseInt(parts[3]), height: parseInt(parts[3]),
link: JSON.parse(parts[4]) link: JSON.parse(parts[4])
};
}).forEach(function (video) {
videos[video.format] = video;
});
/*
* Preference map of quality => youtube formats.
* see https://en.wikipedia.org/wiki/Youtube#Quality_and_codecs
*/
const preference = {
"hd1080": [46, 37],
"hd720": [22, 45],
"large": [44, 35],
"medium": [43, 18],
"small": [5]
}; };
}).forEach(function (video) {
videos[video.format] = video;
});
/* var direct = {};
* Preference map of quality => youtube formats.
* see https://en.wikipedia.org/wiki/Youtube#Quality_and_codecs
*/
const preference = {
"hd1080": [46, 37],
"hd720": [22, 45],
"large": [44, 35],
"medium": [43, 18],
"small": [5]
};
var direct = {}; for (var key in preference) {
for (var i = 0; i < preference[key].length; i++) {
var format = preference[key][i];
for (var key in preference) { if (format in videos) {
for (var i = 0; i < preference[key].length; i++) { direct[key] = videos[format].link;
var format = preference[key][i]; break;
}
if (format in videos) {
direct[key] = videos[format].link;
break;
} }
} }
}
var media = new Media(id, title, duration, "gp", { gpdirect: direct }); if (Object.keys(direct).length === 0) {
cb(null, media); return cb("Unable to retrieve video data from Google+. Check that " +
"the album exists and is shared publicly.");
} else if (!title) {
return cb("Unable to retrieve title from Google+. Check that " +
"the album exists and is shared publicly.");
} else if (!duration) {
return cb("Unable to retreive duration from Google+. This might be " +
"because the video is still processing.");
}
var media = new Media(id, title, duration, "gp", { gpdirect: direct });
cb(null, media);
} catch (e) {
cb("Unknown error");
Logger.errlog.log("Unknown error for Google+ ID " + id + ": " + e.trace);
}
}); });
} }
}; };