From be96e2df81acdec233c001b4a556787e660acefb Mon Sep 17 00:00:00 2001 From: girst Date: Sat, 20 Mar 2021 21:26:58 +0100 Subject: [PATCH] don't load livestreams through proxy for osx(?) devices some browsers reporting as osx will request the hls_manifest way too often, causing strain on the server. however, it just so happens that osx supports native hls playback, so we can just have them talk to google's servers directly. this commit also introduces EXT-X-ENDLIST to */hls_variant/*, so it only gets requested once. --- app/proxy/__init__.py | 2 ++ app/youtube/__init__.py | 42 +++++++++++++---------------- app/youtube/templates/watch.html.j2 | 3 ++- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/app/proxy/__init__.py b/app/proxy/__init__.py index 02d69f0..ccfacdd 100644 --- a/app/proxy/__init__.py +++ b/app/proxy/__init__.py @@ -77,6 +77,8 @@ def hls_manifest(path): return "", r.status_code rv = re.sub(r"^https://[a-z0-9-]+\.googlevideo\.com", "", r.text, flags=re.M) + if "/api/manifest/hls_variant" in request.path: + rv = rv + "\n#EXT-X-ENDLIST" return rv, { 'Content-Type': 'application/x-mpegURL', 'Access-Control-Allow-Origin': cors_origin() diff --git a/app/youtube/__init__.py b/app/youtube/__init__.py index 08f4ca9..2fff34e 100644 --- a/app/youtube/__init__.py +++ b/app/youtube/__init__.py @@ -5,7 +5,7 @@ import requests from urllib.parse import urlparse #from flask_login import current_user, login_required from flask_login import LoginManager, UserMixin, current_user, login_user, logout_user, login_required -from flask import Blueprint, render_template, request, redirect, flash, url_for, jsonify, g +from flask import Blueprint, render_template, request, redirect, flash, url_for, jsonify, g, current_app from werkzeug.exceptions import NotFound, BadGateway from ..common.common import * @@ -90,29 +90,23 @@ def watch(): }.get(error) # if the video is geolocked, and the proxy is enabled, we can still play it: - try: - if error == 'geolocked': - videoplayback = url_for('proxy.videoplayback') - - query = urlparse(video_url).query - video_url = f"{videoplayback}?{query}" - for s in stream_map['adaptive']: - query = urlparse(s['url']).query - s['url'] = f"{videoplayback}?{query}" - for s in stream_map['muxed']: - query = urlparse(s['url']).query - s['url'] = f"{videoplayback}?{query}" - - error = None - except: pass - - try: - if error == 'livestream': - proxy_enabled = url_for('proxy.videoplayback') # will raise if disabled, i think - video_url = urlparse(stream_map['hlsManifestUrl']).path - - error = None - except: pass + if error == 'geolocked' and 'proxy' in current_app.blueprints.keys(): + videoplayback = url_for('proxy.videoplayback') + query = urlparse(video_url).query + video_url = f"{videoplayback}?{query}" + for s in stream_map['adaptive']: + query = urlparse(s['url']).query + s['url'] = f"{videoplayback}?{query}" + for s in stream_map['muxed']: + query = urlparse(s['url']).query + s['url'] = f"{videoplayback}?{query}" + error = None + + # if the proxy is enabled, we can also play livestreams: + if error == 'livestream' and 'proxy' in current_app.blueprints.keys(): + # Note: hlsManifestUrl's hostname will be replaced client-side + video_url = stream_map['hlsManifestUrl'] + error = None # if the instance is blocked, try submitting a job to the anti captcha service: if error == 'banned' and cf['captcha']['api_key']: diff --git a/app/youtube/templates/watch.html.j2 b/app/youtube/templates/watch.html.j2 index 76f6bb7..5abd16e 100644 --- a/app/youtube/templates/watch.html.j2 +++ b/app/youtube/templates/watch.html.j2 @@ -56,7 +56,8 @@ var sha256=function a(b){function c(a,b){return a>>>b|a<<32-b}for(var d,e,f=Math let vid = document.querySelector('video'); if (!vid.canPlayType('application/vnd.apple.mpegurl') && Hls.isSupported()) { var hls = new Hls(); - hls.loadSource(document.querySelector("source").src); + let source = new URL(document.querySelector("source").src).pathname; + hls.loadSource(source); hls.attachMedia(vid); } -- 2.39.3