Skip to content

Commit

Permalink
Fix table data
Browse files Browse the repository at this point in the history
  • Loading branch information
BurnySc2 committed Dec 5, 2024
1 parent 815c3c1 commit 4d4b80d
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
3 changes: 2 additions & 1 deletion gather/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
REGIONS = ["us", "eu", "kr"]
TIERS = list(reversed(range(1, 4)))
ROW_DESCRIPTIONS = [
(f"{league} {tier}" if league != "GrandMaster" else league)
f"{league} {tier}"
for league in LEAGUES
for tier in TIERS
if league != "GrandMaster"
] + ["GrandMaster"]
RACES = ["P", "T", "Z", "R"]

Expand Down
23 changes: 14 additions & 9 deletions gather/create_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
from dpath.util import get, merge
from loguru import logger

from .constants import LEAGUES, MODES, REGIONS, ROW_DESCRIPTIONS, TABLE_HEADER
from .constants import LEAGUES, MODES, REGIONS, TABLE_HEADER
from .helper import get_region_from_href


async def prepare_mmr_table_data(responses: List[dict]):
async def prepare_mmr_table_data(responses: List[dict]) -> dict:
data = {}
for response in responses:
# There was an error in the response, empty dict is returned
Expand Down Expand Up @@ -56,24 +56,29 @@ async def prepare_mmr_table_data(responses: List[dict]):
logger.info("Outputting info to 'data_table_raw.json'")
with open("data_table_raw.json", "w") as f:
json.dump(data, f, indent=4, sort_keys=True)

return data


async def create_mmr_tables(prepared_data: dict):
async def create_mmr_tables(prepared_data: dict) -> dict:
formatted_table = {}
for mode in MODES:
new_table = []
row_number = 0
for league_id, league in enumerate(LEAGUES):
for tier_id in reversed(range(3)):
# Skip if it doesnt exist, e.g. for GM when GM is locked
if not get(prepared_data, f"{mode}/{league_id}/{tier_id}", default={}):
for tier_id in range(2, -1, -1):
# TODO Skip if it doesnt exist, e.g. for GM when GM is locked
# if not get(prepared_data, f"{mode}/{league_id}/{tier_id}", default={}):
# continue

if league == "GrandMaster" and tier_id != 2:
continue

new_row = [ROW_DESCRIPTIONS[row_number]]
new_row = [f"{league} {tier_id + 1}"]
if league == "GrandMaster":
new_row = [f"{league}"]

row_number += 1
for region_id, region_name in enumerate(REGIONS, start=1):
for _region_id, region_name in enumerate(REGIONS, start=1):
min_rating = str(
get(
prepared_data,
Expand Down
2 changes: 1 addition & 1 deletion gather/get_api_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
async def get_sc2_league_api_data(
client: httpx.AsyncClient, access_token: str, season_number: int, fetch_delay: float
):
url = "https://{}.api.blizzard.com/data/sc2/league/{}/{}/{}/{}?locale=en_US&"
url = "https://{}.api.blizzard.com/data/sc2/league/{}/{}/{}/{}?locale=en_US"

urls = []
for region in ["us", "eu", "kr"]:
Expand Down
5 changes: 3 additions & 2 deletions gather/get_gm_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from copy import deepcopy

import httpx
from dpath.util import merge, new
from dpath.util import new
from loguru import logger

from .constants import REGIONS
Expand All @@ -27,7 +27,8 @@ async def get_sc2_gm_api_data(


async def mix_gm_data(prepared_data, gm_borders):
merge(prepared_data, gm_borders)
prepared_data["201"]["6"] = {}
prepared_data["201"]["6"]["2"] = gm_borders["201"][6][0]
with open("data_ladder_api_with_gm.json", "w") as f:
json.dump(prepared_data, f, indent=4, sort_keys=True)
return prepared_data
Expand Down
25 changes: 10 additions & 15 deletions gather/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,16 @@ async def fetch(
if fetch_delay > 0:
await asyncio.sleep(fetch_delay)
logger.info(f"Fetching url {url}")
response = await client.get(
url,
headers={
"Authorization": f"Bearer {access_token}",
},
)
logger.info(f"Done fetching url {url}")
if response.status_code == 200:
# TODO What error may occur?
json_response = response.json()
return json_response
logger.error(
f"Unable to decode url '{url}', receiving response status '{response.status_code}' and error '{response.text}'"
)
return {}
try:
response = await client.get(
url,
headers={
"Authorization": f"Bearer {access_token}",
},
)
return response.json()
except (httpx.ReadTimeout, httpx.ReadError, json.JSONDecodeError):
return {}


async def fetch_multiple(
Expand Down

0 comments on commit 4d4b80d

Please sign in to comment.