diff options
Diffstat (limited to 'server.py')
-rw-r--r-- | server.py | 29 |
1 files changed, 17 insertions, 12 deletions
@@ -84,9 +84,12 @@ def proxy_site(env, start_response, video=False): else: response, cleanup_func = util.fetch_url_response(url, send_headers) - response_headers = response.getheaders() + response_headers = response.headers if isinstance(response_headers, urllib3._collections.HTTPHeaderDict): response_headers = response_headers.items() + if video: + response_headers = (list(response_headers) + +[('Access-Control-Allow-Origin', '*')]) if first_attempt: start_response(str(response.status) + ' ' + response.reason, @@ -94,7 +97,7 @@ def proxy_site(env, start_response, video=False): content_length = int(dict(response_headers).get('Content-Length', 0)) if response.status >= 400: - print('Error: Youtube returned "%d %s" while routing %s' % ( + print('Error: YouTube returned "%d %s" while routing %s' % ( response.status, response.reason, url.split('?')[0])) total_received = 0 @@ -113,7 +116,7 @@ def proxy_site(env, start_response, video=False): content_part = response.read(32*8192) total_received += len(content_part) if not content_part: - # Sometimes Youtube closes the connection before sending all of + # Sometimes YouTube closes the connection before sending all of # the content. Retry with a range request for the missing # content. See # https://github.com/user234683/youtube-local/issues/40 @@ -130,7 +133,7 @@ def proxy_site(env, start_response, video=False): fail_byte = start + total_received send_headers['Range'] = 'bytes=%d-%d' % (fail_byte, end) print( - 'Warning: Youtube closed the connection before byte', + 'Warning: YouTube closed the connection before byte', str(fail_byte) + '.', 'Expected', start+content_length, 'bytes.' ) @@ -146,14 +149,14 @@ def proxy_site(env, start_response, video=False): yield content_part cleanup_func(response) if retry: - # Youtube will return 503 Service Unavailable if you do a bunch + # YouTube will return 503 Service Unavailable if you do a bunch # of range requests too quickly. time.sleep(1) continue else: break else: # no break - print('Error: Youtube closed the connection before', + print('Error: YouTube closed the connection before', 'providing all content. Retried three times:', url.split('?')[0]) @@ -166,8 +169,8 @@ site_handlers = { 'youtube-nocookie.com': yt_app, 'youtu.be': youtu_be, 'ytimg.com': proxy_site, - 'yt3.ggpht.com': proxy_site, - 'lh3.googleusercontent.com': proxy_site, + 'ggpht.com': proxy_site, + 'googleusercontent.com': proxy_site, 'sponsor.ajay.app': proxy_site, 'googlevideo.com': proxy_video, } @@ -247,12 +250,14 @@ def site_dispatch(env, start_response): class FilteredRequestLog: '''Don't log noisy thumbnail and avatar requests''' - filter_re = re.compile(r"""(?x)^ - "GET /https://(i[.]ytimg[.]com/| + filter_re = re.compile(r'''(?x) + "GET\ /https://( + i[.]ytimg[.]com/| www[.]youtube[.]com/data/subscription_thumbnails/| yt3[.]ggpht[.]com/| - www[.]youtube[.]com/api/timedtext).*" 200 - """) + www[.]youtube[.]com/api/timedtext| + [-\w]+[.]googlevideo[.]com/).*"\ (200|206) + ''') def __init__(self): pass |