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