From 5032d2b97413e799d20fbbf06ed93b0554eae0ff Mon Sep 17 00:00:00 2001 From: Emily Doherty Date: Sun, 26 May 2024 07:04:56 -0700 Subject: [PATCH] Support Classic wow token --- wow-token-historical.py | 26 +++++++++++++++++++------- wow-token-updater.py | 20 +++++++++++++------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/wow-token-historical.py b/wow-token-historical.py index eb58710..49b7094 100644 --- a/wow-token-historical.py +++ b/wow-token-historical.py @@ -73,6 +73,8 @@ tables = { 'timestream': 'wow-token-classic-price-history' } } + + def historical_data(time, region, version): # This shim is to permanently change the URL of 30d to 720h for local caching, # There seems to be at least 1 person using 30d (strangely with no .json) which was deprecated @@ -155,13 +157,17 @@ def dynamo_data(time, region, version): Key('region').eq(region) & Key('timestamp').gte(int(start_time_utc.timestamp())))) data = [] + last_price = 0 for item in response['Items']: - item_time = datetime.datetime.utcfromtimestamp(int(item['timestamp'])).replace( - tzinfo=datetime.timezone.utc).isoformat() - data.append({ - 'time': item_time, - 'value': int(int(item['price']) / 10000) - }) + price = int(int(item['price']) / 10000) + if last_price != price: + item_time = datetime.datetime.utcfromtimestamp(int(item['timestamp'])).replace( + tzinfo=datetime.timezone.utc).isoformat() + data.append({ + 'time': item_time, + 'value': price + }) + last_price = price return data @@ -319,7 +325,13 @@ def validate_path(split_uri: list) -> bool: if not split_uri[-1].endswith('json'): return False - return validate_region(split_uri[-2]) and validate_time(split_uri[-1].split('.')[0]) + if not validate_region(split_uri[-2]): + return False + + if not validate_time(split_uri[-1].split('.')[0]): + return False + + return True def validate_time(time: str) -> bool: diff --git a/wow-token-updater.py b/wow-token-updater.py index 88638c2..6c8f717 100644 --- a/wow-token-updater.py +++ b/wow-token-updater.py @@ -34,14 +34,18 @@ def lambda_handler(event, context): def flavor_handler(flavor: str, regions: list, timestamp: int) -> None: - live_prices = get_combined_live_price(flavor) - print(f"Current {flavor} prices:\t{live_prices}") for region in regions: + if flavor == 'retail': + namespace = 'dynamic' + else: + namespace = 'dynamic-classic' + live_price = get_token_price_from_blizzard(region, namespace) + print(f"{flavor} {region} live price {live_price}") print(f'Updating {region.upper()}...') - update_token_price(flavor, region, timestamp, live_prices[region]) + update_token_price(flavor, region, timestamp, live_price) -def update_token_price(flavor: str, region: str, current_time_epoch: int, live_price: int): +def update_token_price(flavor: str, region: str, current_time_epoch: int, live_price: int) -> None: stored_price = get_stored_price(flavor, region) regional_item = get_regional_update_item(flavor, region) print(f'Current live price {live_price}') @@ -113,7 +117,7 @@ def update_regional_price(flavor: str, region: str, price: int, current_time_epo print(f'Updated regional {flavor} {region.upper()} price to {price}') -def create_regional_item(flavor: str, region: str): +def create_regional_item(flavor: str, region: str) -> None: print(f"Creating default regional item in {flavor} {region}") if flavor == 'retail': key = region @@ -242,15 +246,16 @@ def get_combined_live_price(game_flavor: str) -> dict: } -def get_token_price_from_blizzard(region: str, namespace: str): +def get_token_price_from_blizzard(region: str, namespace: str) -> int: api_endpoint = f'https://{region}.api.blizzard.com/data/wow/token/index' params = {'namespace': f'{namespace}-{region}'} headers = {'Authorization': f'Bearer {get_oauth_token()}'} response = requests.get(api_endpoint, params=params, headers=headers) + response.raise_for_status() return int(response.json()['price']) -def get_oauth_token(): +def get_oauth_token() -> str: url = 'https://us.battle.net/oauth/token' payload = { 'grant_type': 'client_credentials', @@ -258,6 +263,7 @@ def get_oauth_token(): 'client_secret': os.environ.get('BLIZZARD_CLIENT_SECRET') } response = requests.post(url, data=payload) + response.raise_for_status() return json.loads(response.text)['access_token']