Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for local emoji sources. 新增对本地emoji资源的支持。 #88

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
165 changes: 70 additions & 95 deletions ssrspeed/speedtest/method/st_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,7 @@
nf_ip_re = re.compile(r'"requestIpAddress":"(.*)"')


def retry(count=5):
def wrapper(func):
async def inner(*args, **kwargs):
for _ in range(count):
result = await func(*args, **kwargs)
if result is True:
break
else:
return False
return True

return inner

return wrapper


class StreamTest:
session = None

@classmethod
async def netflix(cls, host, headers, inner_dict, port, outbound_ip):
logger.info(f"Performing netflix test LOCAL_PORT: {port}.")
Expand Down Expand Up @@ -73,53 +55,46 @@ async def netflix(cls, host, headers, inner_dict, port, outbound_ip):
logger.error(f"Netflix error: {str(e)}")
return {}

@classmethod
@retry(5)
async def netflix_inner_method_1(cls, outbound_ip, inner_dict):
async with cls.session.get(
url="https://www.netflix.com/title/70143836"
) as response:
if response.status != 200:
return False
text = str(await response.read())
locate = text.find("preferredLocale")
netflix_ip = nf_ip_re.findall(text)[0].split(",")[0]
logger.info(f"Netflix IP : {netflix_ip}")
region = text[locate + 29 : locate + 31] if locate > 0 else "Unknown"
if outbound_ip == netflix_ip:
logger.info("Netflix test result: Full Native.")
inner_dict["Ntype"] = f"Full Native({region})"
else:
logger.info("Netflix test result: Full DNS.")
inner_dict["Ntype"] = f"Full DNS({region})"
return True

@classmethod
@retry(5)
async def netflix_inner_method_2(cls, inner_dict):
async with cls.session.get(
url="https://www.netflix.com/title/70242311"
) as response:
if response.status == 200:
logger.info("Netflix test result: Only Original.")
inner_dict["Ntype"] = "Only Original"
return True
logger.info("Netflix test result: None.")
inner_dict["Ntype"] = "None"
return False

@classmethod
async def netflix_new(cls, host, headers, inner_dict, port, outbound_ip):
logger.info(f"Performing netflix(new) test LOCAL_PORT: {port}.")
try:
async with aiohttp.ClientSession(
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
) as cls.session:
result = await cls.netflix_inner_method_1(outbound_ip, inner_dict)
if result is False:
await cls.netflix_inner_method_2(inner_dict)
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
) as session, session.get(
url="https://www.netflix.com/title/70143836" # "https://www.netflix.com/title/70242311"
) as response1:
if response1.status == 200:
text = str(await response1.read())
locate = text.find("preferredLocale")
netflix_ip = nf_ip_re.findall(text)[0].split(
","
)[0]
logger.info(f"Netflix IP : {netflix_ip}")
if locate > 0:
region = text[locate + 29:locate + 31]
else:
region = "未知"
if outbound_ip == netflix_ip:
logger.info("Netflix test result: Full Native.")
inner_dict["Ntype"] = f"Full Native({region})"
else:
logger.info("Netflix test result: Full DNS.")
inner_dict["Ntype"] = f"Full DNS({region})"
return
async with session.get(
url="https://www.netflix.com/title/70242311"
) as response2:
rg = ""
if response2.status == 200:
logger.info("Netflix test result: Only Original.")
inner_dict["Ntype"] = "Only Original"
else:
logger.info("Netflix test result: None.")
inner_dict["Ntype"] = "None"
# 测试连接状态
except Exception as e:
logger.error(f"Netflix error: {str(e)}")
return {}
Expand All @@ -129,9 +104,9 @@ async def hbomax(cls, host, headers, inner_dict, port):
logger.info(f"Performing HBO max test LOCAL_PORT: {port}.")
try:
async with aiohttp.ClientSession(
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
) as session, session.get(
url="https://www.hbomax.com/", allow_redirects=False
) as response:
Expand All @@ -144,9 +119,9 @@ async def disneyplus(cls, host, headers, inner_dict, port):
logger.info(f"Performing Disney plus test LOCAL_PORT: {port}.")
try:
async with aiohttp.ClientSession(
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=5),
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=5),
) as session, session.get(
url="https://www.disneyplus.com/"
) as response1, session.get(
Expand All @@ -171,9 +146,9 @@ async def youtube(cls, host, headers, inner_dict, port):
logger.info(f"Performing Youtube Premium test LOCAL_PORT: {port}.")
try:
async with aiohttp.ClientSession(
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
) as session, session.get(
url="https://music.youtube.com/", allow_redirects=False
) as response:
Expand All @@ -191,9 +166,9 @@ async def abema(cls, host, headers, inner_dict, port):
logger.info(f"Performing Abema test LOCAL_PORT: {port}.")
try:
async with aiohttp.ClientSession(
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
) as session, session.get(
url="https://api.abema.io/v1/ip/check?device=android",
allow_redirects=False,
Expand All @@ -208,9 +183,9 @@ async def bahamut(cls, host, headers, inner_dict, port):
logger.info(f"Performing Bahamut test LOCAL_PORT: {port}.")
try:
async with aiohttp.ClientSession(
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
) as session, session.get(
url="https://ani.gamer.com.tw/ajax/token.php?adID=89422&sn=14667",
allow_redirects=False,
Expand All @@ -225,9 +200,9 @@ async def indazn(cls, host, headers, inner_dict, port):
logger.info(f"Performing Dazn test LOCAL_PORT: {port}.")
try:
async with aiohttp.ClientSession(
headers=headers,
connector=ProxyConnector(host=host, port=port, verify_ssl=False),
timeout=aiohttp.ClientTimeout(connect=10),
headers=headers,
connector=ProxyConnector(host=host, port=port, verify_ssl=False),
timeout=aiohttp.ClientTimeout(connect=10),
) as session:
payload = {
"LandingPageKey": "generic",
Expand All @@ -239,9 +214,9 @@ async def indazn(cls, host, headers, inner_dict, port):
"Version": "2",
}
async with session.post(
url="https://startup.core.indazn.com/misl/v5/Startup",
json=payload,
allow_redirects=False,
url="https://startup.core.indazn.com/misl/v5/Startup",
json=payload,
allow_redirects=False,
) as response:
inner_dict["Dztype"] = response.status == 200
except Exception as e:
Expand All @@ -252,9 +227,9 @@ async def mytvsuper(cls, host, headers, inner_dict, port):
logger.info(f"Performing TVB test LOCAL_PORT: {port}.")
try:
async with aiohttp.ClientSession(
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
) as session, session.get(
url="https://www.mytvsuper.com/api/auth/getSession/self/",
allow_redirects=False,
Expand All @@ -269,9 +244,9 @@ async def bilibili(cls, host, headers, inner_dict, port):
logger.info(f"Performing Bilibili test LOCAL_PORT: {port}.")
try:
async with aiohttp.ClientSession(
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
headers=headers,
connector=ProxyConnector(host=host, port=port),
timeout=aiohttp.ClientTimeout(connect=10),
) as session:
params = {
"avid": 50762638,
Expand All @@ -286,9 +261,9 @@ async def bilibili(cls, host, headers, inner_dict, port):
"module": "bangumi",
}
async with session.get(
url="https://api.bilibili.com/pgc/player/web/playurl",
params=params,
allow_redirects=False,
url="https://api.bilibili.com/pgc/player/web/playurl",
params=params,
allow_redirects=False,
) as response:
if response.status == 200:
json_data = await response.json()
Expand All @@ -309,9 +284,9 @@ async def bilibili(cls, host, headers, inner_dict, port):
}
session.cookie_jar.clear()
async with session.get(
url="https://api.bilibili.com/pgc/player/web/playurl",
params=params,
allow_redirects=False,
url="https://api.bilibili.com/pgc/player/web/playurl",
params=params,
allow_redirects=False,
) as response2:
if response2.status == 200:
json_data2 = await response2.json()
Expand All @@ -327,7 +302,7 @@ async def start_stream_test(port, stream_cfg, outbound_ip):
host = "127.0.0.1"
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Safari/537.36"
}
test_list = []
inner_dict = {
Expand Down Expand Up @@ -376,7 +351,7 @@ async def start_stream_test(port, stream_cfg, outbound_ip):
if stream_cfg["NETFLIX_TEST"]:
test_list.append(
asyncio.create_task(
StreamTest.netflix_new(host, headers, inner_dict, port, outbound_ip)
StreamTest.netflix(host, headers, inner_dict, port, outbound_ip)
)
)
await asyncio.wait(test_list)
Expand Down
Loading