From 69772bf2ecadc3253c9b3fba379b16a7b2028ea7 Mon Sep 17 00:00:00 2001 From: Calvin Montgomery Date: Sun, 13 Jul 2014 11:29:50 -0700 Subject: [PATCH] Error handling for google+ --- lib/get-info.js | 146 +++++++++++++++++++++++++++--------------------- 1 file changed, 81 insertions(+), 65 deletions(-) diff --git a/lib/get-info.js b/lib/get-info.js index 0cbf233f..a6da7c6d 100644 --- a/lib/get-info.js +++ b/lib/get-info.js @@ -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); + } }); } };