69 lines
1.7 KiB
Python
69 lines
1.7 KiB
Python
import urllib.request
|
|
import contextlib
|
|
import base64
|
|
import io
|
|
import threading
|
|
from PIL import Image
|
|
import socket
|
|
import traceback
|
|
import time
|
|
import sys
|
|
from datetime import datetime
|
|
from time import sleep
|
|
import threading
|
|
|
|
headers = {'User-Agent': 'curl/7.88.1'}
|
|
timeout = 60
|
|
def mkreq(u): return urllib.request.Request(u, data=None, headers=headers)
|
|
|
|
|
|
img_lock = threading.Lock()
|
|
img = None
|
|
|
|
|
|
def stream_worker():
|
|
global img
|
|
while True:
|
|
try:
|
|
with contextlib.closing(urllib.request.urlopen(mkreq('http://us2.g-load.eu:9090/stream'), timeout=timeout)) as resp:
|
|
for line in resp:
|
|
if not line.startswith(b'data:'):
|
|
continue
|
|
imgn = Image.open(io.BytesIO(base64.b64decode(line[5:-1])))
|
|
with img_lock:
|
|
if img is None:
|
|
img = imgn.convert(mode='RGBA')
|
|
else:
|
|
imgn = imgn.convert(mode='RGBA')
|
|
img.paste(imgn, (0, 0), imgn)
|
|
except Exception:
|
|
traceback.print_exc()
|
|
time.sleep(3)
|
|
|
|
|
|
def wait_until_time_unit(unit):
|
|
t = datetime.utcnow()
|
|
t.timestamp()
|
|
sleep(unit - t.timestamp() % unit)
|
|
|
|
|
|
def output_worker():
|
|
global img
|
|
while True:
|
|
wait_until_time_unit(0.04)
|
|
with img_lock:
|
|
if img is None:
|
|
continue
|
|
sys.stdout.buffer.write(img.convert('RGB').tobytes())
|
|
sys.stdout.buffer.flush()
|
|
|
|
|
|
if __name__ == '__main__':
|
|
stream = threading.Thread(target=stream_worker, daemon=True)
|
|
stream.start()
|
|
output = threading.Thread(target=output_worker, daemon=True)
|
|
output.start()
|
|
|
|
stream.join()
|
|
output.join()
|