Refactor get-info module to be a Server property

This commit is contained in:
calzoneman 2013-08-03 10:58:21 -04:00
parent aa3234eb6a
commit 1aeec527f6
4 changed files with 442 additions and 572 deletions

View File

@ -15,7 +15,6 @@ var Poll = require("./poll.js").Poll;
var Media = require("./media.js").Media; var Media = require("./media.js").Media;
var formatTime = require("./media.js").formatTime; var formatTime = require("./media.js").formatTime;
var Logger = require("./logger.js"); var Logger = require("./logger.js");
var InfoGetter = require("./get-info.js");
var Rank = require("./rank.js"); var Rank = require("./rank.js");
var Auth = require("./auth.js"); var Auth = require("./auth.js");
var ChatCommand = require("./chatcommand.js"); var ChatCommand = require("./chatcommand.js");
@ -527,7 +526,7 @@ Channel.prototype.search = function(query, callback) {
if(query.trim() == "") { if(query.trim() == "") {
return; return;
} }
InfoGetter.Getters["ytSearch"](query, function(err, vids) { this.server.infogetter.Getters["ytSearch"](query, function(err, vids) {
if(!err) { if(!err) {
callback(vids); callback(vids);
} }
@ -1056,130 +1055,6 @@ Channel.prototype.autoTemp = function(item, user) {
} }
} }
Channel.prototype.enqueue = function(data, user, callback) {
var after = "";
var current = this.playlist.current;
if(data.pos == "next") {
after = current ? current.uid : "prepend";
}
else if(data.pos == "end") {
after = "append";
}
if(isLive(data.type) && !this.hasPermission(user, "playlistaddlive")) {
user.socket.emit("queueFail", "You don't have permission to queue livestreams");
return;
}
// Prefer cache over looking up new data
if(data.id in this.library) {
var media = this.library[data.id].dup();
var item = this.playlist.makeItem(media);
item.queueby = user ? user.name : "";
this.autoTemp(item, user);
this.queueAdd(item, after);
this.logger.log("*** Queued from cache: id=" + data.id);
if(callback)
callback();
}
else {
switch(data.type) {
case "yt":
case "yp":
case "vi":
case "dm":
case "sc":
InfoGetter.getMedia(data.id, data.type, function(err, media) {
if(err) {
if(callback)
callback();
if(err === true)
err = false;
user.socket.emit("queueFail", err);
return;
}
var item = this.playlist.makeItem(media);
item.queueby = user ? user.name : "";
this.autoTemp(item, user);
this.queueAdd(item, after);
this.cacheMedia(media);
if(data.type == "yp")
after = item.uid;
if(callback)
callback();
}.bind(this));
break;
case "li":
var media = new Media(data.id, "Livestream.com - " + data.id, "--:--", "li");
var item = this.playlist.makeItem(media);
item.queueby = user ? user.name : "";
this.autoTemp(item, user);
this.queueAdd(item, after);
if(callback)
callback();
break;
case "tw":
var media = new Media(data.id, "Twitch.tv - " + data.id, "--:--", "tw");
var item = this.playlist.makeItem(media);
item.queueby = user ? user.name : "";
this.autoTemp(item, user);
this.queueAdd(item, after);
if(callback)
callback();
break;
case "jt":
var media = new Media(data.id, "Justin.tv - " + data.id, "--:--", "jt");
var item = this.playlist.makeItem(media);
item.queueby = user ? user.name : "";
this.autoTemp(item, user);
this.queueAdd(item, after);
if(callback)
callback();
break;
case "us":
InfoGetter.getUstream(data.id, function(id) {
var media = new Media(id, "Ustream.tv - " + data.id, "--:--", "us");
var item = this.playlist.makeItem(media);
item.queueby = user ? user.name : "";
this.autoTemp(item, user);
this.queueAdd(item, after);
if(callback)
callback();
}.bind(this));
break;
case "rt":
var media = new Media(data.id, "Livestream", "--:--", "rt");
var item = this.playlist.makeItem(media);
item.queueby = user ? user.name : "";
this.autoTemp(item, user);
this.queueAdd(item, after);
if(callback)
callback();
break;
case "jw":
var media = new Media(data.id, "JWPlayer Stream - " + data.id, "--:--", "jw");
var item = this.playlist.makeItem(media);
item.queueby = user ? user.name : "";
this.autoTemp(item, user);
this.queueAdd(item, after);
if(callback)
callback();
break;
case "im":
var media = new Media(data.id, "Imgur Album", "--:--", "im");
var item = this.playlist.makeItem(media);
item.queueby = user ? user.name : "";
this.autoTemp(item, user);
this.queueAdd(item, after);
if(callback)
callback();
break;
default:
break;
}
}
}
Channel.prototype.tryQueue = function(user, data) { Channel.prototype.tryQueue = function(user, data) {
if(!this.hasPermission(user, "playlistadd")) { if(!this.hasPermission(user, "playlistadd")) {
return; return;

View File

@ -14,12 +14,7 @@ var https = require("https");
var Logger = require("./logger.js"); var Logger = require("./logger.js");
var Media = require("./media.js").Media; var Media = require("./media.js").Media;
var Server = false; module.exports = function (Server) {
function init(srv) {
Server = srv;
}
exports.init = init;
function urlRetrieve(transport, options, callback) { function urlRetrieve(transport, options, callback) {
var req = transport.request(options, function (res) { var req = transport.request(options, function (res) {
var buffer = ""; var buffer = "";
@ -463,12 +458,14 @@ var Getters = {
var media = new Media(id, title, "--:--", "im"); var media = new Media(id, title, "--:--", "im");
callback(false, media); callback(false, media);
} }
} };
exports.Getters = Getters; return {
Getters: Getters,
exports.getMedia = function(id, type, callback) { getMedia: function (id, type, callback) {
if(type in Getters) { if(type in this.Getters) {
Getters[type](id, callback); this.Getters[type](id, callback);
} }
} }
};
}

View File

@ -11,7 +11,6 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
ULList = require("./ullist").ULList; ULList = require("./ullist").ULList;
var Media = require("./media").Media; var Media = require("./media").Media;
var InfoGetter = require("./get-info");
var AllPlaylists = {}; var AllPlaylists = {};
function PlaylistItem(media, uid) { function PlaylistItem(media, uid) {
@ -56,8 +55,8 @@ function Playlist(chan) {
this._qaInterval = false; this._qaInterval = false;
AllPlaylists[name] = this; AllPlaylists[name] = this;
if(chan) {
this.channel = chan; this.channel = chan;
this.server = chan.server;
var pl = this; var pl = this;
this.on("mediaUpdate", function(m) { this.on("mediaUpdate", function(m) {
chan.sendAll("mediaUpdate", m.timeupdate()); chan.sendAll("mediaUpdate", m.timeupdate());
@ -74,7 +73,6 @@ function Playlist(chan) {
}); });
}); });
} }
}
Playlist.prototype.queueAction = function(data) { Playlist.prototype.queueAction = function(data) {
this.action_queue.push(data); this.action_queue.push(data);
@ -252,7 +250,7 @@ Playlist.prototype.addMedia = function(data, callback) {
return; return;
} }
InfoGetter.getMedia(data.id, data.type, function(err, media) { this.server.infogetter.getMedia(data.id, data.type, function(err, media) {
if(err) { if(err) {
action.expire = 0; action.expire = 0;
callback(err, null); callback(err, null);
@ -321,7 +319,7 @@ Playlist.prototype.addYouTubePlaylist = function(data, callback) {
} }
var pl = this; var pl = this;
InfoGetter.getMedia(data.id, data.type, function(err, vids) { this.server.infogetter.getMedia(data.id, data.type, function(err, vids) {
if(err) { if(err) {
callback(err, null); callback(err, null);
return; return;

View File

@ -208,7 +208,7 @@ var Server = {
this.stats = require("./stats")(this); this.stats = require("./stats")(this);
// init media retriever // init media retriever
require("./get-info").init(this); this.infogetter = require("./get-info")(this);
}, },
shutdown: function () { shutdown: function () {
Logger.syslog.log("Unloading channels"); Logger.syslog.log("Unloading channels");