add key proxy

This commit is contained in:
Roy Olav Purser 2021-05-09 12:37:36 +02:00
parent 15f350ae80
commit afbbc032ee
Signed by: roypur
GPG Key ID: E14D26A036F21656

View File

@ -6,6 +6,7 @@ import requests
import json import json
import os import os
import urllib.parse import urllib.parse
import re
providers = {} providers = {}
providers["nrk"] = "https://tv.nrk.no" providers["nrk"] = "https://tv.nrk.no"
@ -48,6 +49,16 @@ if icecast_server is not None and stream_server is not None:
playlist += f'#EXTINF:0 radio="false", {name}\n' playlist += f'#EXTINF:0 radio="false", {name}\n'
playlist += stream_server + key + "\n" playlist += stream_server + key + "\n"
def get_proxy_url(proxy, current, path):
data = {}
data["upstream"] = urllib.parse.urljoin(current, path)
data["proxy"] = proxy
ret = None
if proxy is None or proxy_server is None:
return data["upstream"]
presp = requests.post(proxy_server, json=data)
return presp.text
def rewrite(current, provider): def rewrite(current, provider):
proxy_req = proxies_req.get(provider) proxy_req = proxies_req.get(provider)
proxy = proxies_raw.get(provider) proxy = proxies_raw.get(provider)
@ -64,17 +75,15 @@ def rewrite(current, provider):
if resp.text is not None: if resp.text is not None:
ndata = "" ndata = ""
for line in resp.text.splitlines(): for line in resp.text.splitlines():
if line.startswith("#"): if line.startswith("#EXT-X-KEY:METHOD="):
matches = re.findall(r'(?<=URI=").+(?=")', line)
if len(matches) == 1:
new_url = get_proxy_url(proxy, current, matches[0])
ndata += re.sub(r'URI=".+"', f'URI="{new_url}"', line)
elif line.startswith("#"):
ndata += line ndata += line
else: else:
data = {} ndata += get_proxy_url(proxy, current, line)
data["upstream"] = urllib.parse.urljoin(current, line)
data["proxy"] = proxy
if proxy is None or proxy_server is None:
ndata += data["upstream"]
else:
presp = requests.post(proxy_server, json=data)
ndata += presp.text
ndata += "\n" ndata += "\n"
return ndata return ndata