Created
August 28, 2011 00:54
-
-
Save austenito/1176081 to your computer and use it in GitHub Desktop.
more
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What an amazing gist.