new proto

master
Anton Lydike 4 years ago
parent 5c69d6bc1a
commit 5688e89ba7

@ -1,4 +1,5 @@
import os import os
import re
import sys import sys
import socket import socket
import secrets import secrets
@ -10,12 +11,7 @@ from threading import Thread
from urllib.request import urlopen from urllib.request import urlopen
PORT = 4040 PORT = 4040
BEAM_SERVER = '192.168.0.3:5005' BEAM_SERVER = '192.168.0.192:4005'
d = tempfile.TemporaryDirectory()
print("using tempdir " + d.name)
def handler_for_dir(dir): def handler_for_dir(dir):
def myhandler(*args, **kwargs): def myhandler(*args, **kwargs):
@ -41,11 +37,10 @@ def get_ip():
s.close() s.close()
return IP return IP
target = sys.argv[1] def local_session(target):
d = tempfile.TemporaryDirectory()
id = secrets.token_hex(16) print("using tempdir " + d.name)
# symlink target to tempdir so we only expose a single file
os.symlink(os.path.abspath(target), d.name + '/stream') os.symlink(os.path.abspath(target), d.name + '/stream')
handler = handler_for_dir(d.name + '/') handler = handler_for_dir(d.name + '/')
@ -59,7 +54,7 @@ try:
t.start() t.start()
print("calling beam target...") print("calling beam target...")
host = get_ip() host = get_ip()
resp = get_request(f'http://{BEAM_SERVER}/open?host={host}&port={PORT}') resp = get_request(f'http://{BEAM_SERVER}/open/local?host={host}&port={PORT}')
if resp[0]: if resp[0]:
token = resp[1].strip() token = resp[1].strip()
print(f"successfully started video - session {token}") print(f"successfully started video - session {token}")
@ -75,3 +70,32 @@ finally:
if token: if token:
get_request(f'http://{BEAM_SERVER}/stop/{token}') get_request(f'http://{BEAM_SERVER}/stop/{token}')
d.cleanup() 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]
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

@ -8,14 +8,25 @@ SESSIONS = dict()
app = Flask(__name__) app = Flask(__name__)
@app.route('/open') @app.route('/open/local')
def open_stream(): def open_stream():
url = "http://" + request.args['host'] + ':' + request.args.get('port', '4040') + '/stream' url = "http://" + request.args['host'] + ':' + request.args.get('port', '4040') + '/stream'
id = secrets.token_hex(16) id = secrets.token_hex(16)
SESSIONS[id] = { SESSIONS[id] = {
'url': url, 'url': url,
'id': id, 'id': id,
'proc': Popen(['vlc', url]) 'proc': Popen(['mpv', url])
}
return id
@app.route('/open/youtube/<id>')
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 return id

Loading…
Cancel
Save