-
Notifications
You must be signed in to change notification settings - Fork 1
/
handlers.py
55 lines (47 loc) · 2.09 KB
/
handlers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from requests import Session
from urllib.parse import ParseResult
from modifier import proxied_m3u8
from pools import pools
import gzip
import falcon
def chunked_read(up_res, chunk=64 * 1024, decode_content=None):
while True:
data = up_res.read(chunk, decode_content=decode_content)
if not data:
break
yield data
def full_read(up_res, decode_content=None):
data = up_res.read(decode_content=decode_content)
return data
def handle_ts(url: bytes, res) -> None:
up_res = pools.urlopen(url=url.decode(), method='GET', preload_content=False)
res.content_type = up_res.headers.get('Content-Type', falcon.MEDIA_HTML)
res.status = falcon.code_to_http_status(up_res.status)
res.set_header('content-encoding', up_res.headers.get('content-encoding', ''))
res.set_header('Access-Control-Allow-Origin', '*')
res.stream = chunked_read(up_res, decode_content=False)
def handle_m3u8(url: bytes, res) -> None:
up_res = pools.urlopen(url=url.decode(), method='GET', preload_content=False)
res.content_type = up_res.headers.get('Content-Type', falcon.MEDIA_HTML)
res.status = falcon.code_to_http_status(up_res.status)
res.set_header('content-encoding', 'gzip')
res.set_header('Access-Control-Allow-Origin', '*')
res.body = gzip.compress(b'\n'.join(proxied_m3u8(url, full_read(up_res))))
def handle_key(url: bytes, res) -> None:
up_res = pools.urlopen(url=url.decode(), method='GET', preload_content=False)
res.content_type = up_res.headers.get('Content-Type', falcon.MEDIA_HTML)
res.status = falcon.code_to_http_status(up_res.status)
res.set_header('Access-Control-Allow-Origin', '*')
res.body = full_read(up_res, decode_content=False)
def handle_srt(url: bytes, res) -> None:
up_res = pools.urlopen(url=url.decode(), method='GET', preload_content=False)
res.content_type = up_res.headers.get('Content-Type', falcon.MEDIA_HTML)
res.status = falcon.code_to_http_status(up_res.status)
res.set_header('Access-Control-Allow-Origin', '*')
res.body = full_read(up_res, decode_content=False)
handlers = {
'.ts': handle_ts,
'.m3u8': handle_m3u8,
'.srt': handle_srt,
'.key': handle_key
}