From eaf01f4d8abe34351f6a3b7f4d0c8572fcc7769e Mon Sep 17 00:00:00 2001 From: calzoneman Date: Tue, 7 Jan 2014 00:46:30 -0600 Subject: [PATCH] Vimeo workaround --- lib/config.js | 1 + lib/get-info.js | 39 ++++++++++++++++++++++++++++-- lib/media.js | 16 +++++++++---- lib/playlist.js | 49 +++++++++++++++++++++++++------------- www/assets/js/callbacks.js | 7 +++++- www/assets/js/player.js | 3 ++- 6 files changed, 91 insertions(+), 24 deletions(-) diff --git a/lib/config.js b/lib/config.js index 13bdea26..4748b25c 100644 --- a/lib/config.js +++ b/lib/config.js @@ -45,6 +45,7 @@ var defaults = { "ytv3apikey" : "", "enable-ytv3" : false, "ytv2devkey" : "", + "vimeo-workaround" : false, "stat-interval" : 3600000, "stat-max-age" : 86400000, "alias-purge-interval" : 3600000, diff --git a/lib/get-info.js b/lib/get-info.js index 5603cc23..0952f310 100644 --- a/lib/get-info.js +++ b/lib/get-info.js @@ -357,7 +357,7 @@ var Getters = { callback("API failure", null); return; } else if(status !== 200) { - callback("YTv2 HTTP " + status, null); + callback("Vimeo HTTP " + status, null); return; } @@ -702,6 +702,40 @@ var Getters = { } }; +function VimeoIsADoucheCopter(id, cb) { + var options = { + host: "player.vimeo.com", + path: "/video/" + id + }; + + var parse = function (data) { + var i = data.indexOf("a={"); + var j = data.indexOf("};", i); + var json = data.substring(i+2, j+1); + try { + json = JSON.parse(json); + var codec = json.request.files.codecs[0]; + var files = json.request.files[codec]; + cb(files); + } catch (e) { + cb({}); + } + }; + + http.get(options, function (res) { + res.setEncoding("utf-8"); + var buffer = ""; + + res.on("data", function (data) { + buffer += data; + }); + + res.on("end", function () { + parse(buffer); + }); + }); +}; + module.exports = { Getters: Getters, getMedia: function (id, type, callback) { @@ -710,5 +744,6 @@ module.exports = { } else { callback("Unknown media type '" + type + "'", null); } - } + }, + VimeoIsADoucheCopter: VimeoIsADoucheCopter }; diff --git a/lib/media.js b/lib/media.js index 407e6735..d394e46e 100644 --- a/lib/media.js +++ b/lib/media.js @@ -1,11 +1,11 @@ /* The MIT License (MIT) Copyright (c) 2013 Calvin Montgomery - + 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: - + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - + 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. */ @@ -40,7 +40,7 @@ Media.prototype.pack = function() { duration: this.duration, type: this.type, }; - + if (this.object) { x.object = this.object; } @@ -68,6 +68,9 @@ Media.prototype.fullupdate = function() { if (this.params) { x.params = this.params; } + if (this.direct) { + x.direct = this.direct; + } return x; } @@ -79,4 +82,9 @@ Media.prototype.timeupdate = function() { }; } +Media.prototype.reset = function () { + delete this.currentTime; + delete this.direct; +}; + exports.Media = Media; diff --git a/lib/playlist.js b/lib/playlist.js index 36ebe898..5f2119b1 100644 --- a/lib/playlist.js +++ b/lib/playlist.js @@ -13,6 +13,8 @@ ULList = require("./ullist").ULList; var AsyncQueue = require("./asyncqueue"); var Media = require("./media").Media; var AllPlaylists = {}; +var Server = require("./server"); +var VimeoIsADoucheCopter = require("./get-info").VimeoIsADoucheCopter; function PlaylistItem(media, uid) { this.media = media; @@ -212,6 +214,7 @@ Playlist.prototype.addMedia = function (data) { var m = new Media(data.id, data.title, data.seconds, data.type); m.object = data.object; m.params = data.params; + m.direct = data.direct; var item = this.makeItem(m); item.queueby = data.queueby; item.temp = data.temp; @@ -257,6 +260,7 @@ Playlist.prototype.next = function() { return; var it = this.current; + it.media.reset(); if (it.temp) { if (this.remove(it.uid)) { @@ -290,6 +294,7 @@ Playlist.prototype.jump = function(uid) { return false; var it = this.current; + it.media.reset(); this.current = jmp; @@ -338,29 +343,41 @@ Playlist.prototype.lead = function(lead) { } Playlist.prototype.startPlayback = function (time) { - if(!this.current || !this.current.media) + var self = this; + if (!self.current || !self.current.media) { return false; - if (!this.leading) { - this.current.media.paused = false; - this.current.media.currentTime = time || 0; - this.on("changeMedia")(this.current.media); + } + + if (self.current.media.type === "vi" && + !self.current.media.direct && + Server.getServer().cfg["vimeo-workaround"]) { + VimeoIsADoucheCopter(self.current.media.id, function (direct) { + self.current.media.direct = direct; + self.startPlayback(time); + }); + return; + } + + if (!self.leading) { + self.current.media.paused = false; + self.current.media.currentTime = time || 0; + self.on("changeMedia")(self.current.media); return; } time = time || -3; - this.current.media.paused = time < 0; - this.current.media.currentTime = time; + self.current.media.paused = time < 0; + self.current.media.currentTime = time; - var pl = this; - if(this._leadInterval) { - clearInterval(this._leadInterval); - this._leadInterval = false; + if(self._leadInterval) { + clearInterval(self._leadInterval); + self._leadInterval = false; } - this.on("changeMedia")(this.current.media); - if(!isLive(this.current.media.type)) { - this._lastUpdate = Date.now(); - this._leadInterval = setInterval(function() { - pl._leadLoop(); + self.on("changeMedia")(self.current.media); + if(!isLive(self.current.media.type)) { + self._lastUpdate = Date.now(); + self._leadInterval = setInterval(function() { + self._leadLoop(); }, 1000); } } diff --git a/www/assets/js/callbacks.js b/www/assets/js/callbacks.js index b8ed06f3..826797ad 100644 --- a/www/assets/js/callbacks.js +++ b/www/assets/js/callbacks.js @@ -1032,7 +1032,12 @@ Callbacks = { $("#ytapiplayer_wrapper").remove(); } - if(data.type != PLAYER.type) { + if (data.type === "vi" && data.direct) { + data.type = "jw"; + data.id = data.direct.sd.url; + } + + if (data.type != PLAYER.type) { loadMediaPlayer(data); } diff --git a/www/assets/js/player.js b/www/assets/js/player.js index bed857aa..0e8be01f 100644 --- a/www/assets/js/player.js +++ b/www/assets/js/player.js @@ -1086,7 +1086,8 @@ var constructors = { "jw": JWPlayer, "im": ImgurPlayer, "cu": CustomPlayer, - "gd": GoogleDocsPlayer + "gd": GoogleDocsPlayer, + "me": MediaElementsPlayer }; function loadMediaPlayer(data) {