Skip to content
Merged
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
28 changes: 11 additions & 17 deletions src/MaaDebugger/utils/update_checker/check.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
from concurrent.futures import ThreadPoolExecutor
from enum import auto, Enum
from typing import Any, Optional, Union

Expand All @@ -11,23 +10,21 @@
PYPI_API = "https://pypi.org/pypi/MaaDebugger/json"
TSINGHUA_PYPI_API = "https://mirrors.tuna.tsinghua.edu.cn/pypi/web/json/maadebugger"

_executor = ThreadPoolExecutor(max_workers=2)
_client = httpx.AsyncClient()


class CheckStatus(Enum):
FAILED = auto()
SKIPPED = auto()


def _sync_get_from_pypi(url: str) -> Optional[str]: # -> '1.8.0b1'
"""Synchronous version of get_from_pypi, runs in a thread pool."""
async def _get_from_pypi(url: str) -> Optional[str]: # -> '1.8.0b1'
try:
with httpx.Client() as client:
req = client.get(url, timeout=5)
if req.status_code == 200:
return req.json().get("info", {}).get("version", None)
else:
return None
req = await _client.get(url, timeout=5)
if req.status_code == 200:
return req.json().get("info", {}).get("version", None)
else:
return None
except Exception as e:
print(f"WARNING: Failed to check update from {url}", e)
return None
Expand Down Expand Up @@ -56,16 +53,13 @@ async def check_update() -> Union[CheckStatus, str, None]:
return CheckStatus.SKIPPED
elif "FAILED" in (__version__.tag_name, __version__.version):
return CheckStatus.FAILED

else:
loop = asyncio.get_event_loop()
pypi = loop.run_in_executor(_executor, _sync_get_from_pypi, PYPI_API)
tsinghua_pypi = loop.run_in_executor(
_executor, _sync_get_from_pypi, TSINGHUA_PYPI_API
vers = await asyncio.gather(
_get_from_pypi(PYPI_API),
_get_from_pypi(TSINGHUA_PYPI_API),
return_exceptions=True,
)

vers = await asyncio.gather(pypi, tsinghua_pypi, return_exceptions=True)

check_number = len(vers)
check_succeed_number = 0

Expand Down