81 lines
2.6 KiB
Python
81 lines
2.6 KiB
Python
import boto3
|
|
import datetime
|
|
import json
|
|
import os
|
|
|
|
dynamo_region_map = {
|
|
'us-west-1': 'us-west-1',
|
|
'us-west-2': 'us-west-2',
|
|
'us-east-1': 'us-east-1',
|
|
'us-east-2': 'us-east-2',
|
|
'ap-south-1': 'ap-south-1',
|
|
'ap-northeast-3': 'ap-northeast-1',
|
|
'ap-northeast-2': 'ap-northeast-1',
|
|
'ap-southeast-1': 'ap-southeast-1',
|
|
'ap-southeast-2': 'ap-southeast-2',
|
|
'ap-northeast-1': 'ap-northeast-1',
|
|
'ca-central-1': 'us-east-1',
|
|
'eu-central-1': 'eu-north-1',
|
|
'eu-west-1': 'eu-west-1',
|
|
'eu-west-2': 'eu-west-1',
|
|
'eu-west-3': 'eu-west-3',
|
|
'eu-north-1': 'eu-north-1',
|
|
'sa-east-1': 'sa-east-1',
|
|
'eu-south-1': 'eu-north-1'
|
|
} # This is a rough first pass at an intelligent region selector based on what is replicated
|
|
local_region = ''
|
|
if os.environ['AWS_REGION'] in dynamo_region_map:
|
|
local_region = dynamo_region_map[os.environ['AWS_REGION']]
|
|
else:
|
|
local_region = 'eu-central-1'
|
|
|
|
dynamodb_client = boto3.resource('dynamodb', region_name=local_region)
|
|
retail_table = dynamodb_client.Table('wow-token-price')
|
|
classic_table = dynamodb_client.Table('wow-token-classic-price')
|
|
|
|
regions = ['us', 'eu', 'tw', 'kr']
|
|
regional_data = {
|
|
'us': {'current_time': 0, 'price': 0},
|
|
'eu': {'current_time': 0, 'price': 0},
|
|
'tw': {'current_time': 0, 'price': 0},
|
|
'kr': {'current_time': 0, 'price': 0}
|
|
}
|
|
|
|
|
|
def token_data(version: str) -> dict:
|
|
if version == 'retail':
|
|
table = retail_table
|
|
else:
|
|
table = classic_table
|
|
|
|
items = table.scan()['Items']
|
|
data = {
|
|
'current_time': datetime.datetime.utcnow().replace(tzinfo=datetime.timezone.utc).isoformat(timespec="seconds"),
|
|
'price_data': {},
|
|
'update_times': {},
|
|
}
|
|
for item in items:
|
|
data['price_data'][item['region']] = int(int(item['price']) / 10000)
|
|
data['update_times'][item['region']] = (
|
|
datetime.datetime
|
|
.utcfromtimestamp(int(item['current_time']))
|
|
.replace(tzinfo=datetime.timezone.utc).isoformat()
|
|
)
|
|
return data
|
|
|
|
|
|
def lambda_handler(event, context):
|
|
uri = event['Records'][0]['cf']['request']['uri']
|
|
print(f"URI:\t${uri}")
|
|
split_uri = uri.split('/')
|
|
if split_uri[-3] == 'classic':
|
|
version = 'classic'
|
|
else:
|
|
version = 'retail'
|
|
data = token_data(version)
|
|
response = {'status': '200', 'statusDescription': 'OK', 'headers': {}}
|
|
response['headers']['content-type'] = [{'key': 'Content-Type', 'value': 'application/json'}]
|
|
response['body'] = json.dumps(data)
|
|
print('AWS Region:' + os.environ['AWS_REGION'] + '\tdynamodb_connect_region: ' + local_region)
|
|
return response
|