Skip to content

Instantly share code, notes, and snippets.

@austenito
Created August 28, 2011 00:54
Show Gist options
  • Save austenito/1176081 to your computer and use it in GitHub Desktop.
Save austenito/1176081 to your computer and use it in GitHub Desktop.
more
var db = require('redis').createClient(),
nowjs = require('now'),
http = require('http'),
video = require('video');
var Zeus = function() {
}
Zeus.prototype = {
init: function(room_hash, everyone) {
this.room_hash = room_hash;
queue_name = this._get_queue_name();
everyone.now.addToQueue = function(video_src) {
Zeus.prototype.add_to_queue(video_src, room_hash, queue_name);
};
everyone.now.removeFromQueue = function() {
Zeus.prototype.remove_from_queue(queue_name, function() {
nowjs.getGroup(room_hash).now.popFromVideoQueue();
});
};
},
add_to_queue: function(video_url, room_hash, queue_name) {
id = video.get_video_id(video_url);
switch(video.get_video_type(video_url)) {
case 1:
service = 'youtube';
video_key = 'video:' + service + ':' + id;
db.rpush(queue_name, video_key, function() {});
db.hget('videos', video_key, function(err, data) {
if (data == null){
json_feed = 'http://gdata.youtube.com/feeds/api/videos/' + id +
'?v=2&alt=json';
video.parse_json_feed(json_feed, function(res) {
details = {'title': res.entry.title, 'id': id, 'service': service};
json_details = JSON.stringify(details);
db.hset('videos', video_key, json_details, function() {
nowjs.getGroup(room_hash).now.pushToVideoQueue(json_details);
});
});
}
else {
nowjs.getGroup(room_hash).now.pushToVideoQueue(data);
}
});
break;
case 2:
// vimeo
break;
case 3:
// justin
break;
}
},
remove_from_queue: function(queue_name, callback) {
db.lpop(queue_name, callback);
},
get_queue: function(callback) {
// db.lrange(this._get_queue_name(), 0, -1, callback);
db.lrange(this._get_queue_name(), 0, -1, function(err, result) {
db.hmget("videos", result, callback);
});
},
_get_queue_name: function() {
return 'video_queue:'+this.room_hash;
}
};
exports.Zeus = Zeus;
@j3j3
Copy link

j3j3 commented May 16, 2012

What an amazing gist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment