Add twitch vod support

This commit is contained in:
Calvin Montgomery 2016-09-04 18:53:38 -07:00
parent 7b4126c32f
commit f62d9bc271
5 changed files with 70 additions and 12 deletions

View File

@ -12,6 +12,12 @@ window.TwitchPlayer = class TwitchPlayer extends Player
init: (data) -> init: (data) ->
removeOld() removeOld()
if data.type is 'tv'
# VOD
options =
video: data.id
else
# Livestream
options = options =
channel: data.id channel: data.id
@twitch = new Twitch.Player('ytapiplayer', options) @twitch = new Twitch.Player('ytapiplayer', options)
@ -19,10 +25,12 @@ window.TwitchPlayer = class TwitchPlayer extends Player
@setVolume(VOLUME) @setVolume(VOLUME)
@twitch.setQuality(@mapQuality(USEROPTS.default_quality)) @twitch.setQuality(@mapQuality(USEROPTS.default_quality))
@twitch.addEventListener(Twitch.Player.PLAY, => @twitch.addEventListener(Twitch.Player.PLAY, =>
@paused = false
if CLIENT.leader if CLIENT.leader
sendVideoUpdate() sendVideoUpdate()
) )
@twitch.addEventListener(Twitch.Player.PAUSE, => @twitch.addEventListener(Twitch.Player.PAUSE, =>
@paused = true
if CLIENT.leader if CLIENT.leader
sendVideoUpdate() sendVideoUpdate()
) )
@ -35,6 +43,11 @@ window.TwitchPlayer = class TwitchPlayer extends Player
load: (data) -> load: (data) ->
@setMediaProperties(data) @setMediaProperties(data)
try try
if data.type is 'tv'
# VOD
@twitch.setVideo(data.id)
else
# Livestream
@twitch.setChannel(data.id) @twitch.setChannel(data.id)
catch error catch error
console.error(error) console.error(error)
@ -42,12 +55,14 @@ window.TwitchPlayer = class TwitchPlayer extends Player
pause: -> pause: ->
try try
@twitch.pause() @twitch.pause()
@paused = true
catch error catch error
console.error(error) console.error(error)
play: -> play: ->
try try
@twitch.play() @twitch.play()
@paused = false
catch error catch error
console.error(error) console.error(error)
@ -59,7 +74,7 @@ window.TwitchPlayer = class TwitchPlayer extends Player
getTime: (cb) -> getTime: (cb) ->
try try
cb(@twitch.getVolume()) cb(@twitch.getCurrentTime())
catch error catch error
console.error(error) console.error(error)

View File

@ -9,6 +9,7 @@ TYPE_MAP =
sc: SoundCloudPlayer sc: SoundCloudPlayer
li: LivestreamPlayer li: LivestreamPlayer
tw: TwitchPlayer tw: TwitchPlayer
tv: TwitchPlayer
cu: CustomEmbedPlayer cu: CustomEmbedPlayer
rt: RTMPPlayer rt: RTMPPlayer
hb: HitboxPlayer hb: HitboxPlayer

View File

@ -13,6 +13,7 @@ var Vimeo = require("cytube-mediaquery/lib/provider/vimeo");
var Vidme = require("cytube-mediaquery/lib/provider/vidme"); var Vidme = require("cytube-mediaquery/lib/provider/vidme");
var Streamable = require("cytube-mediaquery/lib/provider/streamable"); var Streamable = require("cytube-mediaquery/lib/provider/streamable");
var GoogleDrive = require("cytube-mediaquery/lib/provider/googledrive"); var GoogleDrive = require("cytube-mediaquery/lib/provider/googledrive");
var TwitchVOD = require("cytube-mediaquery/lib/provider/twitch-vod");
/* /*
* Preference map of quality => youtube formats. * Preference map of quality => youtube formats.
@ -395,6 +396,25 @@ var Getters = {
callback(false, media); callback(false, media);
}, },
/* twitch VOD */
tv: function (id, callback) {
var m = id.match(/([cv]\d+)/);
if (m) {
id = m[1];
} else {
process.nextTick(callback, "Invalid Twitch VOD ID");
return;
}
TwitchVOD.lookup(id).then(video => {
const media = new Media(video.id, video.title, video.duration,
"tv", video.meta);
process.nextTick(callback, false, media);
}).catch(function (err) {
callback(err.message || err, null);
});
},
/* ustream.tv */ /* ustream.tv */
us: function (id, callback) { us: function (id, callback) {
/** /**

View File

@ -983,20 +983,28 @@
TwitchPlayer.prototype.init = function(data) { TwitchPlayer.prototype.init = function(data) {
var options; var options;
removeOld(); removeOld();
if (data.type === 'tv') {
options = {
video: data.id
};
} else {
options = { options = {
channel: data.id channel: data.id
}; };
}
this.twitch = new Twitch.Player('ytapiplayer', options); this.twitch = new Twitch.Player('ytapiplayer', options);
return this.twitch.addEventListener(Twitch.Player.READY, (function(_this) { return this.twitch.addEventListener(Twitch.Player.READY, (function(_this) {
return function() { return function() {
_this.setVolume(VOLUME); _this.setVolume(VOLUME);
_this.twitch.setQuality(_this.mapQuality(USEROPTS.default_quality)); _this.twitch.setQuality(_this.mapQuality(USEROPTS.default_quality));
_this.twitch.addEventListener(Twitch.Player.PLAY, function() { _this.twitch.addEventListener(Twitch.Player.PLAY, function() {
_this.paused = false;
if (CLIENT.leader) { if (CLIENT.leader) {
return sendVideoUpdate(); return sendVideoUpdate();
} }
}); });
_this.twitch.addEventListener(Twitch.Player.PAUSE, function() { _this.twitch.addEventListener(Twitch.Player.PAUSE, function() {
_this.paused = true;
if (CLIENT.leader) { if (CLIENT.leader) {
return sendVideoUpdate(); return sendVideoUpdate();
} }
@ -1014,7 +1022,11 @@
var error, error1; var error, error1;
this.setMediaProperties(data); this.setMediaProperties(data);
try { try {
if (data.type === 'tv') {
return this.twitch.setVideo(data.id);
} else {
return this.twitch.setChannel(data.id); return this.twitch.setChannel(data.id);
}
} catch (error1) { } catch (error1) {
error = error1; error = error1;
return console.error(error); return console.error(error);
@ -1024,7 +1036,8 @@
TwitchPlayer.prototype.pause = function() { TwitchPlayer.prototype.pause = function() {
var error, error1; var error, error1;
try { try {
return this.twitch.pause(); this.twitch.pause();
return this.paused = true;
} catch (error1) { } catch (error1) {
error = error1; error = error1;
return console.error(error); return console.error(error);
@ -1034,7 +1047,8 @@
TwitchPlayer.prototype.play = function() { TwitchPlayer.prototype.play = function() {
var error, error1; var error, error1;
try { try {
return this.twitch.play(); this.twitch.play();
return this.paused = false;
} catch (error1) { } catch (error1) {
error = error1; error = error1;
return console.error(error); return console.error(error);
@ -1054,7 +1068,7 @@
TwitchPlayer.prototype.getTime = function(cb) { TwitchPlayer.prototype.getTime = function(cb) {
var error, error1; var error, error1;
try { try {
return cb(this.twitch.getVolume()); return cb(this.twitch.getCurrentTime());
} catch (error1) { } catch (error1) {
error = error1; error = error1;
return console.error(error); return console.error(error);
@ -1491,6 +1505,7 @@
sc: SoundCloudPlayer, sc: SoundCloudPlayer,
li: LivestreamPlayer, li: LivestreamPlayer,
tw: TwitchPlayer, tw: TwitchPlayer,
tv: TwitchPlayer,
cu: CustomEmbedPlayer, cu: CustomEmbedPlayer,
rt: RTMPPlayer, rt: RTMPPlayer,
hb: HitboxPlayer, hb: HitboxPlayer,

View File

@ -1299,7 +1299,14 @@ function parseMediaLink(url) {
}; };
} }
if((m = url.match(/twitch\.tv\/([^\?&#]+)/))) { if((m = url.match(/twitch\.tv\/(?:.*?)\/([cv])\/(\d+)/))) {
return {
id: m[1] + m[2],
type: "tv"
};
}
if((m = url.match(/twitch\.tv\/([\w-]+)/))) {
return { return {
id: m[1], id: m[1],
type: "tw" type: "tw"