Skip to content

Commit

Permalink
Allow overriding kks stat url
Browse files Browse the repository at this point in the history
  • Loading branch information
vvd170501 committed Oct 15, 2023
1 parent b8ee661 commit 6825bae
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion kks/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.16.29'
__version__ = '1.16.30'
19 changes: 14 additions & 5 deletions kks/util/stat.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
}
Expand All @@ -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
Expand Down

0 comments on commit 6825bae

Please sign in to comment.