2014-07-10 04:20:14 +00:00
|
|
|
var ChannelModule = require("./module");
|
|
|
|
var Config = require("../config");
|
|
|
|
var InfoGetter = require("../get-info");
|
2014-07-10 04:46:45 +00:00
|
|
|
var Logger = require("../logger");
|
2014-07-10 04:20:14 +00:00
|
|
|
|
|
|
|
function MediaRefresherModule(channel) {
|
|
|
|
ChannelModule.apply(this, arguments);
|
|
|
|
this._interval = false;
|
|
|
|
this._media = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
MediaRefresherModule.prototype = Object.create(ChannelModule.prototype);
|
|
|
|
|
2014-07-10 04:46:45 +00:00
|
|
|
MediaRefresherModule.prototype.onPreMediaChange = function (data, cb) {
|
2014-07-10 04:20:14 +00:00
|
|
|
if (this._interval) clearInterval(this._interval);
|
|
|
|
|
|
|
|
this._media = data;
|
|
|
|
|
|
|
|
switch (data.type) {
|
|
|
|
case "gd":
|
2014-07-10 04:46:45 +00:00
|
|
|
return this.initGoogleDocs(data, function () {
|
|
|
|
cb(null, ChannelModule.PASSTHROUGH);
|
|
|
|
});
|
2014-08-07 03:12:57 +00:00
|
|
|
case "gp":
|
|
|
|
return this.initGooglePlus(data, function () {
|
|
|
|
cb(null, ChannelModule.PASSTHROUGH);
|
|
|
|
});
|
2014-07-10 04:20:14 +00:00
|
|
|
case "vi":
|
2014-07-10 04:46:45 +00:00
|
|
|
return this.initVimeo(data, function () {
|
|
|
|
cb(null, ChannelModule.PASSTHROUGH);
|
|
|
|
});
|
|
|
|
default:
|
|
|
|
return cb(null, ChannelModule.PASSTHROUGH);
|
2014-07-10 04:20:14 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-07-10 04:46:45 +00:00
|
|
|
MediaRefresherModule.prototype.initGoogleDocs = function (data, cb) {
|
2014-07-10 04:20:14 +00:00
|
|
|
var self = this;
|
2014-07-10 04:46:45 +00:00
|
|
|
self.refreshGoogleDocs(data, cb);
|
2014-07-10 04:20:14 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Refresh every 55 minutes.
|
|
|
|
* The expiration is 1 hour, but refresh 5 minutes early to be safe
|
|
|
|
*/
|
|
|
|
self._interval = setInterval(function () {
|
2014-07-10 04:46:45 +00:00
|
|
|
self.refreshGoogleDocs(data);
|
2014-07-10 04:20:14 +00:00
|
|
|
}, 55 * 60 * 1000);
|
|
|
|
};
|
|
|
|
|
2014-07-10 04:46:45 +00:00
|
|
|
MediaRefresherModule.prototype.initVimeo = function (data, cb) {
|
2014-07-10 04:20:14 +00:00
|
|
|
if (!Config.get("vimeo-workaround")) {
|
2014-07-10 05:15:14 +00:00
|
|
|
if (cb) cb();
|
2014-07-10 04:20:14 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
self.channel.activeLock.lock();
|
|
|
|
InfoGetter.vimeoWorkaround(data.id, function (hack) {
|
2014-07-13 01:39:48 +00:00
|
|
|
if (self.dead || self.channel.dead) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-10 04:20:14 +00:00
|
|
|
if (self._media === data) {
|
|
|
|
self.channel.logger.log("[mediarefresher] Refreshed vimeo video with ID " +
|
|
|
|
data.id);
|
|
|
|
data.meta.direct = hack;
|
|
|
|
}
|
|
|
|
self.channel.activeLock.release();
|
2014-07-10 04:46:45 +00:00
|
|
|
|
|
|
|
if (cb) cb();
|
2014-07-10 04:20:14 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-07-10 04:46:45 +00:00
|
|
|
MediaRefresherModule.prototype.refreshGoogleDocs = function (media, cb) {
|
2014-07-10 04:20:14 +00:00
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (self.dead || self.channel.dead) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.channel.activeLock.lock();
|
|
|
|
InfoGetter.getMedia(media.id, "gd", function (err, data) {
|
2014-07-13 01:39:48 +00:00
|
|
|
if (self.dead || self.channel.dead) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-10 04:20:14 +00:00
|
|
|
switch (err) {
|
|
|
|
case "HTTP 302":
|
|
|
|
case "Video not found":
|
|
|
|
case "Private video":
|
2014-07-13 18:33:10 +00:00
|
|
|
case "Google Docs error: Video has exceeded quota":
|
2014-12-11 05:56:17 +00:00
|
|
|
case "There is currently a bug with Google Drive which prevents playback of videos 1 hour long or longer.":
|
2014-07-13 01:39:48 +00:00
|
|
|
self.channel.logger.log("[mediarefresher] Google Docs refresh failed: " +
|
|
|
|
err);
|
2014-07-11 02:52:16 +00:00
|
|
|
self.channel.activeLock.release();
|
2014-07-11 02:54:26 +00:00
|
|
|
if (cb) cb();
|
2014-07-10 04:20:14 +00:00
|
|
|
return;
|
|
|
|
default:
|
|
|
|
if (err) {
|
2014-07-13 01:39:48 +00:00
|
|
|
self.channel.logger.log("[mediarefresher] Google Docs refresh failed: " +
|
|
|
|
err);
|
2014-07-10 04:20:14 +00:00
|
|
|
Logger.errlog.log("Google Docs refresh failed for ID " + media.id +
|
|
|
|
": " + err);
|
2014-07-10 04:46:45 +00:00
|
|
|
self.channel.activeLock.release();
|
|
|
|
if (cb) cb();
|
|
|
|
return;
|
2014-07-10 04:20:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (media !== self._media) {
|
2014-07-10 04:46:45 +00:00
|
|
|
self.channel.activeLock.release();
|
|
|
|
if (cb) cb();
|
|
|
|
return;
|
2014-07-10 04:20:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.channel.logger.log("[mediarefresher] Refreshed Google Docs video with ID " +
|
|
|
|
media.id);
|
|
|
|
media.meta = data.meta;
|
|
|
|
self.channel.activeLock.release();
|
2014-07-10 04:46:45 +00:00
|
|
|
if (cb) cb();
|
2014-07-10 04:20:14 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-08-07 03:12:57 +00:00
|
|
|
MediaRefresherModule.prototype.initGooglePlus = function (media, cb) {
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
if (self.dead || self.channel.dead) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.channel.activeLock.lock();
|
|
|
|
InfoGetter.getMedia(media.id, "gp", function (err, data) {
|
|
|
|
if (self.dead || self.channel.dead) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (err) {
|
|
|
|
case "HTTP 302":
|
|
|
|
case "Video not found":
|
|
|
|
case "Private video":
|
2014-12-11 05:56:17 +00:00
|
|
|
case "Unable to retreive duration from Google+. This might be because the video is still processing.":
|
2014-08-07 03:12:57 +00:00
|
|
|
self.channel.logger.log("[mediarefresher] Google+ refresh failed: " +
|
|
|
|
err);
|
|
|
|
self.channel.activeLock.release();
|
|
|
|
if (cb) cb();
|
|
|
|
return;
|
|
|
|
default:
|
|
|
|
if (err) {
|
|
|
|
self.channel.logger.log("[mediarefresher] Google+ refresh failed: " +
|
|
|
|
err);
|
|
|
|
Logger.errlog.log("Google+ refresh failed for ID " + media.id +
|
|
|
|
": " + err);
|
|
|
|
self.channel.activeLock.release();
|
|
|
|
if (cb) cb();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (media !== self._media) {
|
|
|
|
self.channel.activeLock.release();
|
|
|
|
if (cb) cb();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.channel.logger.log("[mediarefresher] Refreshed Google+ video with ID " +
|
|
|
|
media.id);
|
|
|
|
media.meta = data.meta;
|
|
|
|
self.channel.activeLock.release();
|
|
|
|
if (cb) cb();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-07-10 04:20:14 +00:00
|
|
|
module.exports = MediaRefresherModule;
|