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,37 +37,65 @@ def get_ip():
s.close() s.close()
return IP 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] 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 # 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()

@ -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