use pydantic for proxy communication
All checks were successful
continuous-integration/drone Build is passing
All checks were successful
continuous-integration/drone Build is passing
This commit is contained in:
parent
e2107c66eb
commit
6cf319940f
@ -4,7 +4,10 @@ import os
|
|||||||
import pprint
|
import pprint
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
from typing import Literal, Optional, cast
|
||||||
|
|
||||||
|
|
||||||
|
import pydantic
|
||||||
import aiohttp
|
import aiohttp
|
||||||
import streamlink
|
import streamlink
|
||||||
from tornado import template
|
from tornado import template
|
||||||
@ -38,6 +41,22 @@ stream_server = os.environ.get("STREAM_SERVER")
|
|||||||
proxy_server = os.environ.get("PROXY_SERVER")
|
proxy_server = os.environ.get("PROXY_SERVER")
|
||||||
|
|
||||||
|
|
||||||
|
class ProxyCreateLink(pydantic.BaseModel):
|
||||||
|
upstream: pydantic.HttpUrl
|
||||||
|
ctype: Optional[pydantic.StrictStr]
|
||||||
|
region: pydantic.StrictStr
|
||||||
|
|
||||||
|
|
||||||
|
class ProxyRequest(pydantic.BaseModel):
|
||||||
|
action: Literal["create-url", "read-config"]
|
||||||
|
urls: Optional[list[ProxyCreateLink]]
|
||||||
|
|
||||||
|
|
||||||
|
class ProxyResponse(pydantic.BaseModel):
|
||||||
|
action: Literal["create-url", "read-config"]
|
||||||
|
urls: Optional[list[pydantic.HttpUrl]]
|
||||||
|
|
||||||
|
|
||||||
class ProxyElem:
|
class ProxyElem:
|
||||||
def __init__(self, proxy, region):
|
def __init__(self, proxy, region):
|
||||||
self.proxy = proxy
|
self.proxy = proxy
|
||||||
@ -52,44 +71,41 @@ class ProxyElem:
|
|||||||
|
|
||||||
async def proxy_url(self, urls):
|
async def proxy_url(self, urls):
|
||||||
clean_urls = []
|
clean_urls = []
|
||||||
|
link_requests: list[ProxyCreateLink] = []
|
||||||
for url in urls:
|
for url in urls:
|
||||||
if isinstance(url, tuple):
|
if isinstance(url, tuple):
|
||||||
clean_urls.append(url[0])
|
clean_urls.append(url[0])
|
||||||
|
link_requests.append(ProxyCreateLink.parse_obj({"upstream": url[0], "ctype": url[1], "region": self.region}))
|
||||||
else:
|
else:
|
||||||
clean_urls.append(url)
|
clean_urls.append(url)
|
||||||
|
link_requests.append(ProxyCreateLink.parse_obj({"upstream": url, "region": self.region}))
|
||||||
if not isinstance(proxy_server, str):
|
if not isinstance(proxy_server, str):
|
||||||
return clean_urls
|
return clean_urls
|
||||||
|
|
||||||
jdata = None
|
response_data: ProxyRequest
|
||||||
data_list = []
|
response_data: ProxyResponse
|
||||||
for url in urls:
|
|
||||||
data = {}
|
try:
|
||||||
if isinstance(url, str):
|
request_data = ProxyRequest.parse_obj({"urls": link_requests, "action": "create-url"})
|
||||||
data["upstream"] = url
|
except pydantic.ValidationError as e:
|
||||||
elif isinstance(url, tuple):
|
logger.info(e)
|
||||||
if isinstance(url[0], str):
|
return clean_urls
|
||||||
data["upstream"] = url[0]
|
|
||||||
if isinstance(url[1], str):
|
|
||||||
data["ctype"] = url[1]
|
|
||||||
|
|
||||||
data["region"] = self.region
|
|
||||||
data_list.append(data)
|
|
||||||
try:
|
try:
|
||||||
async with self.local() as session:
|
async with self.local() as session:
|
||||||
resp = await session.post(proxy_server, json=data_list)
|
resp = await session.post(proxy_server, json=request_data)
|
||||||
text = await resp.text()
|
response_data = cast(ProxyResponse, ProxyResponse.parse_obj(await resp.text()))
|
||||||
jdata = json.loads(text)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.info(e)
|
logger.info(e)
|
||||||
if isinstance(jdata, list):
|
else:
|
||||||
ret_data = []
|
ret_data = []
|
||||||
for src, dst in zip(clean_urls, jdata):
|
if response_data.urls is not None:
|
||||||
|
for src, dst in zip(clean_urls, response_data.urls):
|
||||||
if isinstance(src, str):
|
if isinstance(src, str):
|
||||||
ret_data.append(dst)
|
ret_data.append(dst)
|
||||||
else:
|
else:
|
||||||
ret_data.append(None)
|
ret_data.append(None)
|
||||||
return ret_data
|
return ret_data
|
||||||
else:
|
|
||||||
return clean_urls
|
return clean_urls
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user