Okay not really, but I have lost the original git repo so this is where we're at qq
63 lines
2.0 KiB
Python
63 lines
2.0 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': 'eu-north-1',
|
|
'ap-northeast-3': 'ap-northeast-1',
|
|
'ap-northeast-2': 'ap-northeast-1',
|
|
'ap-southeast-1': 'ap-southeast-2',
|
|
'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-north-1',
|
|
'eu-west-2': 'eu-north-1',
|
|
'eu-west-3': 'eu-north-1',
|
|
'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-north-1'
|
|
|
|
dynamodb_client = boto3.resource('dynamodb', region_name=local_region)
|
|
table = dynamodb_client.Table('wow-token-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():
|
|
items = table.scan()['Items']
|
|
data = {
|
|
'current_time': datetime.datetime.utcfromtimestamp(int(items[0]['current_time'])).replace(
|
|
tzinfo=datetime.timezone.utc).isoformat(),
|
|
'price_data': {}
|
|
}
|
|
for item in items:
|
|
data['price_data'][item['region']] = int(int(item['price']) / 10000)
|
|
return data
|
|
|
|
|
|
def lambda_handler(event, context):
|
|
data = token_data()
|
|
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
|