ETL Dollar price
Step I - Extract
!pip install twilio==7.16.0
import requests
from apikey2 import API_KEY2
import pandas as pd
import json
import os
from twilio.rest import Client
from twilio_config import TWILIO_ACCOUNT_SID,TWILIO_AUTH_TOKEN,PHONE_NUMBER
import time
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
from datetime import datetime
'''
Code that allows extract data of dollar from API apilayer
Carlos Astorga
'''
url = "https://api.apilayer.com/currency_data/live?source=¤cies=EUR"
payload = {}
headers= {
"apikey": API_KEY2
}
response = requests.request("GET", url, headers=headers, data = payload)
status_code = response.status_code
result = response.text
import time
print(result)
Step II - Transform
# because result is a string, I transform it to a dictionary with json.loads() function
data = json.loads(result)
data
df = pd.DataFrame(data = data)
print(df)
# accesing the content
df = df.iloc[0]['quotes']
print(df)
# Save to file
file = open("./dollar.txt", "w")
file.write(str(df))
print("Datos guardados con éxito!")
#--------------------------------------------------- TWILIO ---------------
# template para luego enviar al celular
template = '\nHola Carlos! \n\n El valor del Dollar para hoy es: \n\n '+ str(df)
print(template)
time.sleep(2)
account_sid = TWILIO_ACCOUNT_SID
auth_token = TWILIO_AUTH_TOKEN
client = Client(account_sid, auth_token)
message = client.messages \
.create(
body = template,
from_ = PHONE_NUMBER,
to = '+34 611 11 11 11'#your number
)
print('El mensaje fue enviado con éxito!' + message.sid)