Skip to content

Instantly share code, notes, and snippets.

@marcolivierarsenault
Created January 15, 2018 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcolivierarsenault/957f0c9e03bf186e0b62049b5178cf09 to your computer and use it in GitHub Desktop.
Save marcolivierarsenault/957f0c9e03bf186e0b62049b5178cf09 to your computer and use it in GitHub Desktop.
lamda function to get all the lambda function and store them in a database
import json
import decimal
import urllib.request
import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('AltCoinHistory')
def lambda_handler(event, context):
url = 'https://api.coinmarketcap.com/v1/ticker/?limit=300'
response = urllib.request.urlopen(url).read().decode('utf-8')
cryptos = json.loads(response, parse_float = decimal.Decimal)
for crypto in cryptos:
symbol = (crypto['symbol'])
price_usd = decimal.Decimal(crypto['price_usd'] or 0)
price_btc = decimal.Decimal(crypto['price_btc'] or 0)
a24h_volume_usd = decimal.Decimal(crypto['24h_volume_usd'] or 0)
percent_change_1h = decimal.Decimal(crypto['percent_change_1h'] or 0)
percent_change_24h = decimal.Decimal(crypto['percent_change_24h'] or 0)
percent_change_7d = decimal.Decimal(crypto['percent_change_7d'] or 0)
last_updated = int(crypto['last_updated'] or 0)
table.put_item(
Item={
'symbol': symbol,
'last_updated': last_updated,
'price_usd': price_usd,
'price_btc': price_btc,
'24h_volume_usd': a24h_volume_usd,
'percent_change_1h': percent_change_1h,
'percent_change_24h': percent_change_24h,
'percent_change_7d': percent_change_7d,
})
return "done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment