From 6825bae373233c8a3aee592c17101f40a96fba28 Mon Sep 17 00:00:00 2001 From: vvd170501 <36827317+vvd170501@users.noreply.github.com> Date: Sun, 15 Oct 2023 14:07:46 +0300 Subject: [PATCH] Allow overriding kks stat url --- kks/__init__.py | 2 +- kks/util/stat.py | 19 ++++++++++++++----- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/kks/__init__.py b/kks/__init__.py index d185d65..e6c5f02 100644 --- a/kks/__init__.py +++ b/kks/__init__.py @@ -1 +1 @@ -__version__ = '1.16.29' +__version__ = '1.16.30' diff --git a/kks/util/stat.py b/kks/util/stat.py index d3316ce..c5b9365 100644 --- a/kks/util/stat.py +++ b/kks/util/stat.py @@ -1,16 +1,23 @@ +from os import environ + import click from kks.ejudge import Standings, TaskInfo, StandingsRow, TaskScore from kks.util.ejudge import AuthData -KKS_STAT_API = 'https://kks.darkkeks.me/api' KKS_STAT_TIMEOUT = 3 # seconds +def api_url_or_default(url): + if url is not None: + return url + return environ.get('KKS_STAT_API', 'https://kks.darkkeks.me/api') -def send_standings(standings): +def send_standings(standings, api_url=None): import requests from requests import RequestException + api_url = api_url_or_default(api_url) + auth_data = AuthData.load_from_config() if auth_data is None: return False @@ -21,16 +28,18 @@ def send_standings(standings): data.update(standings_to_dict(standings)) try: - response = requests.post(f"{KKS_STAT_API}/send", json=data, timeout=KKS_STAT_TIMEOUT) + response = requests.post(f"{api_url}/send", json=data, timeout=KKS_STAT_TIMEOUT) return response.ok except RequestException: return False -def get_global_standings(user, year): +def get_global_standings(user, year, api_url=None): import requests from requests import RequestException + api_url = api_url_or_default(api_url) + parameters = { 'year': year, } @@ -40,7 +49,7 @@ def get_global_standings(user, year): parameters.update(auth_data_to_dict(auth_data)) try: - response = requests.get(f"{KKS_STAT_API}/get", params=parameters, timeout=KKS_STAT_TIMEOUT) + response = requests.get(f"{api_url}/get", params=parameters, timeout=KKS_STAT_TIMEOUT) except RequestException as e: click.secho(f'Failed to receive global standings: {e}', fg='red', err=True) return None