Created
December 20, 2012 21:13
-
-
Save niedbalski/4348631 to your computer and use it in GitHub Desktop.
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
import zmq | |
import sys | |
context = zmq.Context() | |
socket = context.socket(zmq.PUSH) | |
socket.connect("tcp://172.21.0.49:5000") | |
socket.send_json({'video': sys.argv[1]}) | |
sys.exit(0) |
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
import zmq | |
import urlparse | |
import urllib | |
import json | |
DEFAULT_TIMEOUT=5 | |
def parse(link): | |
url_data = urlparse.urlparse(link) | |
query = urlparse.parse_qs(url_data.query) | |
video = query["v"][0] | |
return video | |
def duration(video): | |
try: | |
f = urllib.urlopen("http://gdata.youtube.com/feeds/api/videos/%s?alt=json" % video) | |
readed = json.loads(f.read()) | |
timeout = readed['entry']['media$group']["media$content"][0]['duration'] | |
except: | |
timeout = DEFAULT_TIMEOUT | |
finally: | |
return float(timeout) | |
def timeout_command(command, timeout): | |
import subprocess, datetime, os, time, signal | |
cmd = command.split(" ") | |
start = datetime.datetime.now() | |
process = subprocess.Popen(cmd, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
while True: | |
r = process.poll() | |
if (datetime.datetime.now() - start).seconds > timeout: | |
os.system('killall chromium-browser') | |
return None | |
time.sleep(0.1) | |
return | |
def play(message): | |
video = parse(message['video']) | |
timeout_command("chromium-browser --kiosk http://www.youtube.com/watch_popup?v=%s --start-maximized" % video, duration(video)) | |
def main(): | |
context = zmq.Context() | |
socket = context.socket(zmq.PULL) | |
socket.bind("tcp://*:5000") | |
while True: | |
message = socket.recv_json() | |
play(message) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment