From 5688e89ba74d046f225dc642a661d2cc1ff42f11 Mon Sep 17 00:00:00 2001 From: Anton Lydike Date: Mon, 22 Mar 2021 20:16:58 +0100 Subject: [PATCH] new proto --- client.py | 96 ++++++++++++++++++++++++++++++++++--------------------- server.py | 15 +++++++-- 2 files changed, 73 insertions(+), 38 deletions(-) diff --git a/client.py b/client.py index fd8223c..ea88fa0 100644 --- a/client.py +++ b/client.py @@ -1,4 +1,5 @@ import os +import re import sys import socket import secrets @@ -10,12 +11,7 @@ from threading import Thread from urllib.request import urlopen PORT = 4040 -BEAM_SERVER = '192.168.0.3:5005' - - -d = tempfile.TemporaryDirectory() - -print("using tempdir " + d.name) +BEAM_SERVER = '192.168.0.192:4005' def handler_for_dir(dir): def myhandler(*args, **kwargs): @@ -41,37 +37,65 @@ def get_ip(): s.close() return IP +def local_session(target): + d = tempfile.TemporaryDirectory() + print("using tempdir " + d.name) + + os.symlink(os.path.abspath(target), d.name + '/stream') + + handler = handler_for_dir(d.name + '/') + token = None + + try: + with http.server.HTTPServer(("0.0.0.0", PORT), handler) as httpd: + try: + print("serving at port", PORT) + t = Thread(target=httpd.serve_forever) + t.start() + print("calling beam target...") + host = get_ip() + resp = get_request(f'http://{BEAM_SERVER}/open/local?host={host}&port={PORT}') + if resp[0]: + token = resp[1].strip() + print(f"successfully started video - session {token}") + input("Just press enter when you are done...") + else: + print("Error statrtig video!") + finally: + print("shutting down server") + httpd.shutdown() + + finally: + print("cleaning up") + if token: + get_request(f'http://{BEAM_SERVER}/stop/{token}') + d.cleanup() + +def youtube_session(video): + id = re.match('^(https?:\/\/)?(www\.)?youtu(\.be\/|be\.com\/watch\?(.*&)?v=)([A-Za-z0-9]{5,20})', video) + if not id: + print(f"invalid match in {video}!") + return + id = id.group(5) + + print(f"watching youtube video {id}") + + resp = get_request(f'http://{BEAM_SERVER}/open/youtube/{id}') + + if resp[0]: + token = resp[1].strip() + print(f"successfully started video - session {token}") + input("Just press enter when you are done...") + get_request(f'http://{BEAM_SERVER}/stop/{token}') + else: + print("Error statrtig video!") + + target = sys.argv[1] -id = secrets.token_hex(16) +if re.match('^(https?:\/\/)?(www\.)?youtu(\.be\/|be\.com\/)', target): + youtube_session(target) +else: + local_session(target) # symlink target to tempdir so we only expose a single file -os.symlink(os.path.abspath(target), d.name + '/stream') - -handler = handler_for_dir(d.name + '/') -token = None - -try: - with http.server.HTTPServer(("0.0.0.0", PORT), handler) as httpd: - try: - print("serving at port", PORT) - t = Thread(target=httpd.serve_forever) - t.start() - print("calling beam target...") - host = get_ip() - resp = get_request(f'http://{BEAM_SERVER}/open?host={host}&port={PORT}') - if resp[0]: - token = resp[1].strip() - print(f"successfully started video - session {token}") - input("Just press enter when you are done...") - else: - print("Error statrtig video!") - finally: - print("shutting down server") - httpd.shutdown() - -finally: - print("cleaning up") - if token: - get_request(f'http://{BEAM_SERVER}/stop/{token}') - d.cleanup() diff --git a/server.py b/server.py index cc26e39..a62ca0c 100644 --- a/server.py +++ b/server.py @@ -8,14 +8,25 @@ SESSIONS = dict() app = Flask(__name__) -@app.route('/open') +@app.route('/open/local') def open_stream(): url = "http://" + request.args['host'] + ':' + request.args.get('port', '4040') + '/stream' id = secrets.token_hex(16) SESSIONS[id] = { 'url': url, 'id': id, - 'proc': Popen(['vlc', url]) + 'proc': Popen(['mpv', url]) + } + return id + +@app.route('/open/youtube/') +def open_youtube(id): + url = f"https://www.youtube.com/watch?v={id}" + id = secrets.token_hex(16) + SESSIONS[id] = { + 'url': url, + 'id': id, + 'proc': Popen(['mpv', url]) } return id