ETL Cryptocurrency Data
ETL that obtains data from the first 10 cryptocurrencies. It then generates a dataset to send a WhatsApp message through Twilio.
Step I - Extract
#!pip install twilio==7.16.0
# Libraries
import requests
import time
import pandas as pd
from apikey import API_KEY
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
from twilio.rest import Client
from twilio_config import TWILIO_ACCOUNT_SID,TWILIO_AUTH_TOKEN
from twilio_phone_number import MY_PHONE_NUMBER,TWILIO_PHONE_NUMBER
# Function to get the data(Extraction)
def getCriptoValue():
'''
Function that allows extract data of criptos from API of coinmarketcap
by: Carlos Astorga
'''
headers = {
'Accepts': 'application/json',
'X-CMC_PRO_API_KEY': API_KEY,
}
params = {
'start':'1',
'limit':'10',
'convert':'USD'
}
url = ' https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
try:
json = requests.get(url, params=params, headers = headers).json()
coins = json['data'] # data es el diccionario con la info q me interesa
#print(json)
print("Extrayendo datos...")
for coin in coins:
file = open("./data.csv", "a")# a = append - w = write - r = read
file.write(coin['name'] + ',' + ' ')
file.write(coin['symbol'] + ',' + ' ')
file.write(coin['last_updated'] + ',' + ' ')
file.write(str(coin['max_supply']) + ',' + ' ')
file.write(str(coin['quote']['USD']['market_cap']) + ',' + ' ')
file.write(str(round(coin['quote']['USD']['price'], 2)) + "\n")
file.close()
print("Datos extraidos con éxito!")
#time.sleep(60)
except (ConnectionError, Timeout, TooManyRedirects) as e:
print(e)
# Function call
getCriptoValue()
Extrayendo datos...
Datos extraidos con éxito!
# Reading the data
f = open("./data.csv", "r")
file = f.readlines()
for line in file:
print(line, end="")
Bitcoin, BTC, 2023-01-01T21:20:00.000Z, 21000000, 319743356378.97455, 16610.5
Ethereum, ETH, 2023-01-01T21:20:00.000Z, None, 147054174276.57526, 1201.68
Tether, USDT, 2023-01-01T21:20:00.000Z, None, 66238016726.689514, 1.0
USD Coin, USDC, 2023-01-01T21:20:00.000Z, None, 44575747046.78236, 1.0
BNB, BNB, 2023-01-01T21:20:00.000Z, 200000000, 39249358508.98231, 245.36
XRP, XRP, 2023-01-01T21:20:00.000Z, 100000000000, 17035276841.381462, 0.34
Binance USD, BUSD, 2023-01-01T21:20:00.000Z, None, 16567166072.302385, 1.0
Dogecoin, DOGE, 2023-01-01T21:20:00.000Z, None, 9350225552.348335, 0.07
Cardano, ADA, 2023-01-01T21:20:00.000Z, 45000000000, 8579977685.3243, 0.25
Polygon, MATIC, 2023-01-01T21:20:00.000Z, 10000000000, 6632093508.028664, 0.76
Step II - Transform
# Split each row by comma and save to list NewLine
newFile = []
for line in file:
newFile.append(line.split(','))
# Viewing the result
for line in newFile:
print(line)
['Bitcoin', ' BTC', ' 2023-01-01T21:20:00.000Z', ' 21000000', ' 319743356378.97455', ' 16610.5\n']
['Ethereum', ' ETH', ' 2023-01-01T21:20:00.000Z', ' None', ' 147054174276.57526', ' 1201.68\n']
['Tether', ' USDT', ' 2023-01-01T21:20:00.000Z', ' None', ' 66238016726.689514', ' 1.0\n']
['USD Coin', ' USDC', ' 2023-01-01T21:20:00.000Z', ' None', ' 44575747046.78236', ' 1.0\n']
['BNB', ' BNB', ' 2023-01-01T21:20:00.000Z', ' 200000000', ' 39249358508.98231', ' 245.36\n']
['XRP', ' XRP', ' 2023-01-01T21:20:00.000Z', ' 100000000000', ' 17035276841.381462', ' 0.34\n']
['Binance USD', ' BUSD', ' 2023-01-01T21:20:00.000Z', ' None', ' 16567166072.302385', ' 1.0\n']
['Dogecoin', ' DOGE', ' 2023-01-01T21:20:00.000Z', ' None', ' 9350225552.348335', ' 0.07\n']
['Cardano', ' ADA', ' 2023-01-01T21:20:00.000Z', ' 45000000000', ' 8579977685.3243', ' 0.25\n']
['Polygon', ' MATIC', ' 2023-01-01T21:20:00.000Z', ' 10000000000', ' 6632093508.028664', ' 0.76\n']
# Creation of dataset
df = pd.DataFrame(data = newFile, columns=('Name','Symbol','Last_updated','Max_supply','Market_cap','Price'))
df
Nameobject
Bitcoin10%
Ethereum10%
8 others80%
Symbolobject
BTC10%
ETH10%
8 others80%
0
Bitcoin
BTC
1
Ethereum
ETH
2
Tether
USDT
3
USD Coin
USDC
4
BNB
BNB
5
XRP
XRP
6
Binance USD
BUSD
7
Dogecoin
DOGE
8
Cardano
ADA
9
Polygon
MATIC
df = df[['Symbol','Price']]
df
Symbolobject
BTC10%
ETH10%
8 others80%
Priceobject
1.0
30%
16610.5
10%
6 others60%
0
BTC
16610.5
1
ETH
1201.68
2
USDT
1.0
3
USDC
1.0
4
BNB
245.36
5
XRP
0.34
6
BUSD
1.0
7
DOGE
0.07
8
ADA
0.25
9
MATIC
0.76
# template twilio para el mensaje
template = '\nHola Carlos! \n\n Ranking 10 principales Criptos de hoy: \n\n '+ str(df)
print(template)
Hola Carlos!
Ranking 10 principales Criptos de hoy:
Symbol Price
0 BTC 16610.5\n
1 ETH 1201.68\n
2 USDT 1.0\n
3 USDC 1.0\n
4 BNB 245.36\n
5 XRP 0.34\n
6 BUSD 1.0\n
7 DOGE 0.07\n
8 ADA 0.25\n
9 MATIC 0.76\n
Step III -Load
from twilio.rest import Client
account_sid = 'ACdd05e680da1a13fec131bcebcf59f155'
auth_token = TWILIO_AUTH_TOKEN
client = Client(account_sid, auth_token)
message = client.messages.create(
from_=TWILIO_PHONE_NUMBER,
body=template,
to=MY_PHONE_NUMBER
)
print(message.sid)
SMa7b3877f38647687f6bc9cdbdb75b665