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:
break; /* Request is OK, skip to handling data */
case 400:
return callback("Invalid request", null);
return cb("Invalid request", null);
case 403:
return callback("Private video", null);
return cb("Private video", null);
case 404:
return callback("Video not found", null);
return cb("Video not found", null);
case 500:
case 503:
return callback("Service unavailable", null);
return cb("Service unavailable", null);
default:
return callback("HTTP " + status, null);
return cb("HTTP " + status, null);
}
var videos = {};
var duration;
var title;
var startReading = false;
var startReadingSentinel = new RegExp('"' + idparts[2] + '"');
res.split("\n").filter(function (line) {
/* Once the duration has been set, no more lines are relevant */
if (duration) {
return false;
}
try {
var videos = {};
var duration;
var title;
var startReading = false;
var startReadingSentinel = new RegExp('"' + idparts[2] + '"');
res.split("\n").filter(function (line) {
/* Once the duration has been set, no more lines are relevant */
if (duration) {
return false;
}
var m = line.match(/"og:image" content="([^"]+)"/);
if (m) {
var parts = m[1].replace(/%25/g, "%")
.replace(/%2B/ig, "%20").split("/");
title = decodeURIComponent(parts[parts.length - 1]);
}
var m = line.match(/"og:image" content="([^"]+)"/);
if (m) {
var parts = m[1].replace(/%25/g, "%")
.replace(/%2B/ig, "%20").split("/");
title = decodeURIComponent(parts[parts.length - 1]);
}
/* Hack for only reading relevant video data */
if (line.match(new RegExp(startReadingSentinel))) {
startReading = true;
}
/* Hack for only reading relevant video data */
if (line.match(new RegExp(startReadingSentinel))) {
startReading = true;
}
if (!startReading) {
return false;
}
if (!startReading) {
return false;
}
m = line.match(/,(\d+),,"https:\/\/video\.googleusercontent/);
if (m) {
duration = parseInt(parseInt(m[1]) / 1000);
}
m = line.match(/,(\d+),,"https:\/\/video\.googleusercontent/);
if (m) {
duration = parseInt(parseInt(m[1]) / 1000);
}
return line.match(/videoplayback/);
}).map(function (line) {
var parts = line.match(/\[(\d+),(\d+),(\d+),("http:\/\/redirector.*?")\]/);
return {
format: parseInt(parts[1]),
width: parseInt(parts[2]),
height: parseInt(parts[3]),
link: JSON.parse(parts[4])
return line.match(/videoplayback/);
}).map(function (line) {
var parts = line.match(/\[(\d+),(\d+),(\d+),("http:\/\/redirector.*?")\]/);
return {
format: parseInt(parts[1]),
width: parseInt(parts[2]),
height: parseInt(parts[3]),
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;
});
/*
* 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 = {};
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) {
for (var i = 0; i < preference[key].length; i++) {
var format = preference[key][i];
if (format in videos) {
direct[key] = videos[format].link;
break;
if (format in videos) {
direct[key] = videos[format].link;
break;
}
}
}
}
var media = new Media(id, title, duration, "gp", { gpdirect: direct });
cb(null, media);
if (Object.keys(direct).length === 0) {
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);
}
});
}
};