ETL - Extracción de datos desde weatherapi.com
!pip install twilio==7.16.0
Collecting twilio==7.16.0
Downloading twilio-7.16.0-py2.py3-none-any.whl (1.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.4/1.4 MB 73.8 MB/s eta 0:00:00
Requirement already satisfied: requests>=2.0.0 in /shared-libs/python3.9/py/lib/python3.9/site-packages (from twilio==7.16.0) (2.28.1)
Requirement already satisfied: pytz in /shared-libs/python3.9/py/lib/python3.9/site-packages (from twilio==7.16.0) (2022.5)
Requirement already satisfied: PyJWT<3.0.0,>=2.0.0 in /shared-libs/python3.9/py/lib/python3.9/site-packages (from twilio==7.16.0) (2.6.0)
Requirement already satisfied: certifi>=2017.4.17 in /shared-libs/python3.9/py/lib/python3.9/site-packages (from requests>=2.0.0->twilio==7.16.0) (2022.9.24)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /shared-libs/python3.9/py/lib/python3.9/site-packages (from requests>=2.0.0->twilio==7.16.0) (1.26.12)
Requirement already satisfied: charset-normalizer<3,>=2 in /shared-libs/python3.9/py-core/lib/python3.9/site-packages (from requests>=2.0.0->twilio==7.16.0) (2.1.1)
Requirement already satisfied: idna<4,>=2.5 in /shared-libs/python3.9/py-core/lib/python3.9/site-packages (from requests>=2.0.0->twilio==7.16.0) (3.4)
Installing collected packages: twilio
Successfully installed twilio-7.16.0
WARNING: You are using pip version 22.0.4; however, version 22.3.1 is available.
You should consider upgrading via the '/root/venv/bin/python -m pip install --upgrade pip' command.
"""
************************************************************************
* Envio de mensajes con API de Twilio (Python) *
************************************************************************
"""
import os
from twilio.rest import Client
from twilio_config import TWILIO_ACCOUNT_SID,TWILIO_AUTH_TOKEN,PHONE_NUMBER,API_KEY_WAPI
import time
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
import pandas as pd
import requests
from tqdm import tqdm
from datetime import datetime
# from utils import request_wapi,get_forecast,create_df,send_message,get_date
def get_date():
input_date = datetime.now()
input_date = input_date.strftime("%Y-%m-%d")
return input_date
def request_wapi(api_key,query):
url_clima = 'http://api.weatherapi.com/v1/forecast.json?key='+api_key+'&q='+query+'&days=1&aqi=no&alerts=no'
try :
response = requests.get(url_clima).json()
except Exception as e:
print(e)
return response
query = 'Barcelona'
api_key = API_KEY_WAPI
input_date= get_date()
response = request_wapi(api_key,query)
print(response)
{'location': {'name': 'Barcelona', 'region': 'Catalonia', 'country': 'Spain', 'lat': 41.38, 'lon': 2.18, 'tz_id': 'Europe/Madrid', 'localtime_epoch': 1672166093, 'localtime': '2022-12-27 19:34'}, 'current': {'last_updated_epoch': 1672165800, 'last_updated': '2022-12-27 19:30', 'temp_c': 14.0, 'temp_f': 57.2, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 9.4, 'wind_kph': 15.1, 'wind_degree': 220, 'wind_dir': 'SW', 'pressure_mb': 1029.0, 'pressure_in': 30.39, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 88, 'cloud': 0, 'feelslike_c': 13.8, 'feelslike_f': 56.8, 'vis_km': 10.0, 'vis_miles': 6.0, 'uv': 1.0, 'gust_mph': 5.4, 'gust_kph': 8.6}, 'forecast': {'forecastday': [{'date': '2022-12-27', 'date_epoch': 1672099200, 'day': {'maxtemp_c': 17.7, 'maxtemp_f': 63.9, 'mintemp_c': 13.0, 'mintemp_f': 55.4, 'avgtemp_c': 14.9, 'avgtemp_f': 58.7, 'maxwind_mph': 5.6, 'maxwind_kph': 9.0, 'totalprecip_mm': 0.0, 'totalprecip_in': 0.0, 'totalsnow_cm': 0.0, 'avgvis_km': 10.0, 'avgvis_miles': 6.0, 'avghumidity': 68.0, 'daily_will_it_rain': 0, 'daily_chance_of_rain': 0, 'daily_will_it_snow': 0, 'daily_chance_of_snow': 0, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'uv': 4.0}, 'astro': {'sunrise': '08:16 AM', 'sunset': '05:29 PM', 'moonrise': '11:47 AM', 'moonset': '10:27 PM', 'moon_phase': 'Waxing Crescent', 'moon_illumination': '25'}, 'hour': [{'time_epoch': 1672095600, 'time': '2022-12-27 00:00', 'temp_c': 14.1, 'temp_f': 57.4, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 5.4, 'wind_kph': 8.6, 'wind_degree': 50, 'wind_dir': 'NE', 'pressure_mb': 1029.0, 'pressure_in': 30.38, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 74, 'cloud': 2, 'feelslike_c': 13.7, 'feelslike_f': 56.7, 'windchill_c': 13.7, 'windchill_f': 56.7, 'heatindex_c': 14.1, 'heatindex_f': 57.4, 'dewpoint_c': 9.6, 'dewpoint_f': 49.3, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 9.4, 'gust_kph': 15.1, 'uv': 1.0}, {'time_epoch': 1672099200, 'time': '2022-12-27 01:00', 'temp_c': 14.0, 'temp_f': 57.2, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 4.9, 'wind_kph': 7.9, 'wind_degree': 39, 'wind_dir': 'NE', 'pressure_mb': 1029.0, 'pressure_in': 30.38, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 73, 'cloud': 2, 'feelslike_c': 13.7, 'feelslike_f': 56.7, 'windchill_c': 13.7, 'windchill_f': 56.7, 'heatindex_c': 14.0, 'heatindex_f': 57.2, 'dewpoint_c': 9.3, 'dewpoint_f': 48.7, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.5, 'gust_kph': 13.7, 'uv': 1.0}, {'time_epoch': 1672102800, 'time': '2022-12-27 02:00', 'temp_c': 13.8, 'temp_f': 56.8, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 4.7, 'wind_kph': 7.6, 'wind_degree': 29, 'wind_dir': 'NNE', 'pressure_mb': 1029.0, 'pressure_in': 30.38, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 72, 'cloud': 2, 'feelslike_c': 13.5, 'feelslike_f': 56.3, 'windchill_c': 13.5, 'windchill_f': 56.3, 'heatindex_c': 13.8, 'heatindex_f': 56.8, 'dewpoint_c': 8.9, 'dewpoint_f': 48.0, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.3, 'gust_kph': 13.3, 'uv': 1.0}, {'time_epoch': 1672106400, 'time': '2022-12-27 03:00', 'temp_c': 13.7, 'temp_f': 56.7, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 4.9, 'wind_kph': 7.9, 'wind_degree': 21, 'wind_dir': 'NNE', 'pressure_mb': 1029.0, 'pressure_in': 30.39, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 70, 'cloud': 3, 'feelslike_c': 13.4, 'feelslike_f': 56.1, 'windchill_c': 13.4, 'windchill_f': 56.1, 'heatindex_c': 13.7, 'heatindex_f': 56.7, 'dewpoint_c': 8.4, 'dewpoint_f': 47.1, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.5, 'gust_kph': 13.7, 'uv': 1.0}, {'time_epoch': 1672110000, 'time': '2022-12-27 04:00', 'temp_c': 13.5, 'temp_f': 56.3, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 4.9, 'wind_kph': 7.9, 'wind_degree': 9, 'wind_dir': 'N', 'pressure_mb': 1029.0, 'pressure_in': 30.37, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 68, 'cloud': 3, 'feelslike_c': 13.1, 'feelslike_f': 55.6, 'windchill_c': 13.1, 'windchill_f': 55.6, 'heatindex_c': 13.5, 'heatindex_f': 56.3, 'dewpoint_c': 7.8, 'dewpoint_f': 46.0, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.7, 'gust_kph': 14.0, 'uv': 1.0}, {'time_epoch': 1672113600, 'time': '2022-12-27 05:00', 'temp_c': 13.4, 'temp_f': 56.1, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 5.1, 'wind_kph': 8.3, 'wind_degree': 17, 'wind_dir': 'NNE', 'pressure_mb': 1029.0, 'pressure_in': 30.37, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 68, 'cloud': 3, 'feelslike_c': 12.9, 'feelslike_f': 55.2, 'windchill_c': 12.9, 'windchill_f': 55.2, 'heatindex_c': 13.4, 'heatindex_f': 56.1, 'dewpoint_c': 7.5, 'dewpoint_f': 45.5, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.9, 'gust_kph': 14.4, 'uv': 1.0}, {'time_epoch': 1672117200, 'time': '2022-12-27 06:00', 'temp_c': 13.2, 'temp_f': 55.8, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 5.1, 'wind_kph': 8.3, 'wind_degree': 20, 'wind_dir': 'NNE', 'pressure_mb': 1029.0, 'pressure_in': 30.38, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 68, 'cloud': 3, 'feelslike_c': 12.7, 'feelslike_f': 54.9, 'windchill_c': 12.7, 'windchill_f': 54.9, 'heatindex_c': 13.2, 'heatindex_f': 55.8, 'dewpoint_c': 7.4, 'dewpoint_f': 45.3, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.9, 'gust_kph': 14.4, 'uv': 1.0}, {'time_epoch': 1672120800, 'time': '2022-12-27 07:00', 'temp_c': 13.1, 'temp_f': 55.6, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 4.9, 'wind_kph': 7.9, 'wind_degree': 16, 'wind_dir': 'NNE', 'pressure_mb': 1029.0, 'pressure_in': 30.38, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 68, 'cloud': 3, 'feelslike_c': 12.7, 'feelslike_f': 54.9, 'windchill_c': 12.7, 'windchill_f': 54.9, 'heatindex_c': 13.1, 'heatindex_f': 55.6, 'dewpoint_c': 7.3, 'dewpoint_f': 45.1, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.5, 'gust_kph': 13.7, 'uv': 1.0}, {'time_epoch': 1672124400, 'time': '2022-12-27 08:00', 'temp_c': 13.0, 'temp_f': 55.4, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 4.5, 'wind_kph': 7.2, 'wind_degree': 10, 'wind_dir': 'N', 'pressure_mb': 1029.0, 'pressure_in': 30.4, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 68, 'cloud': 4, 'feelslike_c': 12.7, 'feelslike_f': 54.9, 'windchill_c': 12.7, 'windchill_f': 54.9, 'heatindex_c': 13.0, 'heatindex_f': 55.4, 'dewpoint_c': 7.1, 'dewpoint_f': 44.8, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 7.6, 'gust_kph': 12.2, 'uv': 1.0}, {'time_epoch': 1672128000, 'time': '2022-12-27 09:00', 'temp_c': 13.0, 'temp_f': 55.4, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 4.3, 'wind_kph': 6.8, 'wind_degree': 1, 'wind_dir': 'N', 'pressure_mb': 1030.0, 'pressure_in': 30.41, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 67, 'cloud': 4, 'feelslike_c': 12.8, 'feelslike_f': 55.0, 'windchill_c': 12.8, 'windchill_f': 55.0, 'heatindex_c': 13.0, 'heatindex_f': 55.4, 'dewpoint_c': 7.0, 'dewpoint_f': 44.6, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 7.2, 'gust_kph': 11.5, 'uv': 4.0}, {'time_epoch': 1672131600, 'time': '2022-12-27 10:00', 'temp_c': 13.5, 'temp_f': 56.3, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 3.4, 'wind_kph': 5.4, 'wind_degree': 329, 'wind_dir': 'NNW', 'pressure_mb': 1031.0, 'pressure_in': 30.43, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 65, 'cloud': 5, 'feelslike_c': 13.6, 'feelslike_f': 56.5, 'windchill_c': 13.6, 'windchill_f': 56.5, 'heatindex_c': 13.5, 'heatindex_f': 56.3, 'dewpoint_c': 7.1, 'dewpoint_f': 44.8, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 5.4, 'gust_kph': 8.6, 'uv': 4.0}, {'time_epoch': 1672135200, 'time': '2022-12-27 11:00', 'temp_c': 15.7, 'temp_f': 60.3, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 2.9, 'wind_kph': 4.7, 'wind_degree': 321, 'wind_dir': 'NW', 'pressure_mb': 1031.0, 'pressure_in': 30.44, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 63, 'cloud': 4, 'feelslike_c': 15.7, 'feelslike_f': 60.3, 'windchill_c': 15.7, 'windchill_f': 60.3, 'heatindex_c': 15.7, 'heatindex_f': 60.3, 'dewpoint_c': 8.7, 'dewpoint_f': 47.7, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 3.4, 'gust_kph': 5.4, 'uv': 5.0}, {'time_epoch': 1672138800, 'time': '2022-12-27 12:00', 'temp_c': 16.5, 'temp_f': 61.7, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 2.0, 'wind_kph': 3.2, 'wind_degree': 298, 'wind_dir': 'WNW', 'pressure_mb': 1031.0, 'pressure_in': 30.43, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 62, 'cloud': 4, 'feelslike_c': 16.5, 'feelslike_f': 61.7, 'windchill_c': 16.5, 'windchill_f': 61.7, 'heatindex_c': 16.5, 'heatindex_f': 61.7, 'dewpoint_c': 9.3, 'dewpoint_f': 48.7, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 2.2, 'gust_kph': 3.6, 'uv': 5.0}, {'time_epoch': 1672142400, 'time': '2022-12-27 13:00', 'temp_c': 17.0, 'temp_f': 62.6, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 2.9, 'wind_kph': 4.7, 'wind_degree': 228, 'wind_dir': 'SW', 'pressure_mb': 1030.0, 'pressure_in': 30.41, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 60, 'cloud': 3, 'feelslike_c': 17.0, 'feelslike_f': 62.6, 'windchill_c': 17.0, 'windchill_f': 62.6, 'heatindex_c': 17.0, 'heatindex_f': 62.6, 'dewpoint_c': 9.2, 'dewpoint_f': 48.6, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 3.4, 'gust_kph': 5.4, 'uv': 5.0}, {'time_epoch': 1672146000, 'time': '2022-12-27 14:00', 'temp_c': 17.0, 'temp_f': 62.6, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 4.3, 'wind_kph': 6.8, 'wind_degree': 225, 'wind_dir': 'SW', 'pressure_mb': 1029.0, 'pressure_in': 30.39, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 60, 'cloud': 3, 'feelslike_c': 17.0, 'feelslike_f': 62.6, 'windchill_c': 17.0, 'windchill_f': 62.6, 'heatindex_c': 17.0, 'heatindex_f': 62.6, 'dewpoint_c': 9.2, 'dewpoint_f': 48.6, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 4.9, 'gust_kph': 7.9, 'uv': 5.0}, {'time_epoch': 1672149600, 'time': '2022-12-27 15:00', 'temp_c': 17.0, 'temp_f': 62.6, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 5.1, 'wind_kph': 8.3, 'wind_degree': 211, 'wind_dir': 'SSW', 'pressure_mb': 1028.0, 'pressure_in': 30.37, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 62, 'cloud': 3, 'feelslike_c': 17.0, 'feelslike_f': 62.6, 'windchill_c': 17.0, 'windchill_f': 62.6, 'heatindex_c': 17.0, 'heatindex_f': 62.6, 'dewpoint_c': 9.7, 'dewpoint_f': 49.5, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 5.8, 'gust_kph': 9.4, 'uv': 5.0}, {'time_epoch': 1672153200, 'time': '2022-12-27 16:00', 'temp_c': 17.7, 'temp_f': 63.9, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 5.6, 'wind_kph': 9.0, 'wind_degree': 206, 'wind_dir': 'SSW', 'pressure_mb': 1028.0, 'pressure_in': 30.36, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 64, 'cloud': 2, 'feelslike_c': 17.7, 'feelslike_f': 63.9, 'windchill_c': 17.7, 'windchill_f': 63.9, 'heatindex_c': 17.7, 'heatindex_f': 63.9, 'dewpoint_c': 10.9, 'dewpoint_f': 51.6, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 6.5, 'gust_kph': 10.4, 'uv': 5.0}, {'time_epoch': 1672156800, 'time': '2022-12-27 17:00', 'temp_c': 17.5, 'temp_f': 63.5, 'is_day': 1, 'condition': {'text': 'Sunny', 'icon': '//cdn.weatherapi.com/weather/64x64/day/113.png', 'code': 1000}, 'wind_mph': 5.6, 'wind_kph': 9.0, 'wind_degree': 208, 'wind_dir': 'SSW', 'pressure_mb': 1028.0, 'pressure_in': 30.36, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 68, 'cloud': 0, 'feelslike_c': 17.5, 'feelslike_f': 63.5, 'windchill_c': 17.5, 'windchill_f': 63.5, 'heatindex_c': 17.5, 'heatindex_f': 63.5, 'dewpoint_c': 11.5, 'dewpoint_f': 52.7, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 6.5, 'gust_kph': 10.4, 'uv': 5.0}, {'time_epoch': 1672160400, 'time': '2022-12-27 18:00', 'temp_c': 17.0, 'temp_f': 62.6, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 5.4, 'wind_kph': 8.6, 'wind_degree': 216, 'wind_dir': 'SW', 'pressure_mb': 1028.0, 'pressure_in': 30.36, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 71, 'cloud': 0, 'feelslike_c': 17.0, 'feelslike_f': 62.6, 'windchill_c': 17.0, 'windchill_f': 62.6, 'heatindex_c': 17.0, 'heatindex_f': 62.6, 'dewpoint_c': 11.6, 'dewpoint_f': 52.9, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 6.3, 'gust_kph': 10.1, 'uv': 1.0}, {'time_epoch': 1672164000, 'time': '2022-12-27 19:00', 'temp_c': 16.5, 'temp_f': 61.7, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 4.7, 'wind_kph': 7.6, 'wind_degree': 234, 'wind_dir': 'SW', 'pressure_mb': 1028.0, 'pressure_in': 30.37, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 72, 'cloud': 2, 'feelslike_c': 16.5, 'feelslike_f': 61.7, 'windchill_c': 16.5, 'windchill_f': 61.7, 'heatindex_c': 16.5, 'heatindex_f': 61.7, 'dewpoint_c': 11.5, 'dewpoint_f': 52.7, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 5.4, 'gust_kph': 8.6, 'uv': 1.0}, {'time_epoch': 1672167600, 'time': '2022-12-27 20:00', 'temp_c': 14.2, 'temp_f': 57.6, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 4.7, 'wind_kph': 7.6, 'wind_degree': 246, 'wind_dir': 'WSW', 'pressure_mb': 1029.0, 'pressure_in': 30.38, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 72, 'cloud': 0, 'feelslike_c': 14.0, 'feelslike_f': 57.2, 'windchill_c': 14.0, 'windchill_f': 57.2, 'heatindex_c': 14.2, 'heatindex_f': 57.6, 'dewpoint_c': 9.3, 'dewpoint_f': 48.7, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 7.6, 'gust_kph': 12.2, 'uv': 1.0}, {'time_epoch': 1672171200, 'time': '2022-12-27 21:00', 'temp_c': 14.1, 'temp_f': 57.4, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 4.9, 'wind_kph': 7.9, 'wind_degree': 253, 'wind_dir': 'WSW', 'pressure_mb': 1029.0, 'pressure_in': 30.38, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 72, 'cloud': 0, 'feelslike_c': 13.8, 'feelslike_f': 56.8, 'windchill_c': 13.8, 'windchill_f': 56.8, 'heatindex_c': 14.1, 'heatindex_f': 57.4, 'dewpoint_c': 9.2, 'dewpoint_f': 48.6, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.1, 'gust_kph': 13.0, 'uv': 1.0}, {'time_epoch': 1672174800, 'time': '2022-12-27 22:00', 'temp_c': 14.0, 'temp_f': 57.2, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 5.1, 'wind_kph': 8.3, 'wind_degree': 262, 'wind_dir': 'W', 'pressure_mb': 1029.0, 'pressure_in': 30.37, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 72, 'cloud': 5, 'feelslike_c': 13.7, 'feelslike_f': 56.7, 'windchill_c': 13.7, 'windchill_f': 56.7, 'heatindex_c': 14.0, 'heatindex_f': 57.2, 'dewpoint_c': 9.0, 'dewpoint_f': 48.2, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.5, 'gust_kph': 13.7, 'uv': 1.0}, {'time_epoch': 1672178400, 'time': '2022-12-27 23:00', 'temp_c': 13.9, 'temp_f': 57.0, 'is_day': 0, 'condition': {'text': 'Clear', 'icon': '//cdn.weatherapi.com/weather/64x64/night/113.png', 'code': 1000}, 'wind_mph': 5.4, 'wind_kph': 8.6, 'wind_degree': 270, 'wind_dir': 'W', 'pressure_mb': 1029.0, 'pressure_in': 30.37, 'precip_mm': 0.0, 'precip_in': 0.0, 'humidity': 71, 'cloud': 17, 'feelslike_c': 13.5, 'feelslike_f': 56.3, 'windchill_c': 13.5, 'windchill_f': 56.3, 'heatindex_c': 13.9, 'heatindex_f': 57.0, 'dewpoint_c': 8.8, 'dewpoint_f': 47.8, 'will_it_rain': 0, 'chance_of_rain': 0, 'will_it_snow': 0, 'chance_of_snow': 0, 'vis_km': 10.0, 'vis_miles': 6.0, 'gust_mph': 8.9, 'gust_kph': 14.4, 'uv': 1.0}]}]}}
response.keys()
response['location']
response['current']
response['forecast']
response['forecast']['forecastday'][0].keys()
#cantidad de horas
len(response['forecast']['forecastday'][0]['hour'])
# obtengo la fecha y hora
response['forecast']['forecastday'][0]['hour'][1]['time']
# divido el string obtenido y obtengo el primer elemento(fecha)
response['forecast']['forecastday'][0]['hour'][1]['time'].split()[0]
# divido el string obtenido y obtengo el segundo elemento(hora)
response['forecast']['forecastday'][0]['hour'][1]['time'].split()[1]
# formateo la hora y convierto a entero
int(response['forecast']['forecastday'][0]['hour'][1]['time'].split()[1].split(':')[0])
# Obtener condición
response['forecast']['forecastday'][0]['hour'][0]['condition']
response['forecast']['forecastday'][0]['hour'][0]['condition']['text']
# Obtener temperatura
response['forecast']['forecastday'][0]['hour'][0]['temp_c']
# Llovera? 0 = No, 1= Sí
response['forecast']['forecastday'][0]['hour'][0]['will_it_rain']
# Probabilidad de lluvia
response['forecast']['forecastday'][0]['hour'][0]['chance_of_rain']
Creando el Dataframe
# funcion que recoge los valores antes filtrados
def get_forecast(response, i):
fecha = response['forecast']['forecastday'][0]['hour'][i]['time'].split()[0]
hora = int(response['forecast']['forecastday'][0]['hour'][i]['time'].split()[1].split(':')[0])
condicion = response['forecast']['forecastday'][0]['hour'][i]['condition']['text']
temperatura = response['forecast']['forecastday'][0]['hour'][i]['temp_c']
lluvia = response['forecast']['forecastday'][0]['hour'][i]['will_it_rain']
prob_lluvia = response['forecast']['forecastday'][0]['hour'][i]['chance_of_rain']
return fecha, hora, condicion, temperatura, lluvia, prob_lluvia
datos = []
# tqdm es una libreria q permite hacer una barra de avance
# len(response['forecast']['forecastday'][0]['hour']) --> extrae la cant. de horas. (24)
for i in tqdm(range(len(response['forecast']['forecastday'][0]['hour'])), colour = 'green'):
datos.append(get_forecast(response, i))
100%|██████████| 24/24 [00:00<00:00, 248551.35it/s]
datos
datos[0]
# creando las columnas del df
col = ['Fecha', 'Hora', 'Condicion', 'Temperatura', 'Lluvia', 'Prob_lluvia']
df = pd.DataFrame(datos, columns=col )
print(df)
Fecha Hora Condicion Temperatura Lluvia Prob_lluvia
0 2022-12-27 0 Clear 14.1 0 0
1 2022-12-27 1 Clear 14.0 0 0
2 2022-12-27 2 Clear 13.8 0 0
3 2022-12-27 3 Clear 13.7 0 0
4 2022-12-27 4 Clear 13.5 0 0
5 2022-12-27 5 Clear 13.4 0 0
6 2022-12-27 6 Clear 13.2 0 0
7 2022-12-27 7 Clear 13.1 0 0
8 2022-12-27 8 Clear 13.0 0 0
9 2022-12-27 9 Sunny 13.0 0 0
10 2022-12-27 10 Sunny 13.5 0 0
11 2022-12-27 11 Sunny 15.7 0 0
12 2022-12-27 12 Sunny 16.5 0 0
13 2022-12-27 13 Sunny 17.0 0 0
14 2022-12-27 14 Sunny 17.0 0 0
15 2022-12-27 15 Sunny 17.0 0 0
16 2022-12-27 16 Sunny 17.7 0 0
17 2022-12-27 17 Sunny 17.5 0 0
18 2022-12-27 18 Clear 17.0 0 0
19 2022-12-27 19 Clear 16.5 0 0
20 2022-12-27 20 Clear 14.2 0 0
21 2022-12-27 21 Clear 14.1 0 0
22 2022-12-27 22 Clear 14.0 0 0
23 2022-12-27 23 Clear 13.9 0 0
Filtro
# Filtro el df con las horas que me importan. En este caso las de probabilidad de lluvia.
# df_rain = df[ (df['Lluvia']==1) & (df['Hora']>6) & (df['Hora']<22) ]
# df_rain
# Filtro el df con las horas que me importan. En este caso las de probabilidad de lluvia.
df_f = df[ (df['Hora']>7) & (df['Hora']<21) ]
print(df_f)
Fecha Hora Condicion Temperatura Lluvia Prob_lluvia
8 2022-12-27 8 Clear 13.0 0 0
9 2022-12-27 9 Sunny 13.0 0 0
10 2022-12-27 10 Sunny 13.5 0 0
11 2022-12-27 11 Sunny 15.7 0 0
12 2022-12-27 12 Sunny 16.5 0 0
13 2022-12-27 13 Sunny 17.0 0 0
14 2022-12-27 14 Sunny 17.0 0 0
15 2022-12-27 15 Sunny 17.0 0 0
16 2022-12-27 16 Sunny 17.7 0 0
17 2022-12-27 17 Sunny 17.5 0 0
18 2022-12-27 18 Clear 17.0 0 0
19 2022-12-27 19 Clear 16.5 0 0
20 2022-12-27 20 Clear 14.2 0 0
# Función que verifica si el dataset está vacío
def is_empty(dataset):
return len(dataset) == 0
#if (is_empty(df_f)):
# print("El DS se encuentra vacío!")
# break
# me quedo solo con las columnas que me importan
#df_rain = df_rain[['Hora','Condicion']]
#df_rain
# me quedo solo con las columnas que me importan
df_f = df_f[['Hora','Condicion']]
print(df_f)
Hora Condicion
8 8 Clear
9 9 Sunny
10 10 Sunny
11 11 Sunny
12 12 Sunny
13 13 Sunny
14 14 Sunny
15 15 Sunny
16 16 Sunny
17 17 Sunny
18 18 Clear
19 19 Clear
20 20 Clear
# cambio de índice
#df_rain.set_index('Hora', inplace=True)
#print(df_rain)
# cambio de índice
df_f.set_index('Hora', inplace=True)
print(df_f)
Condicion
Hora
8 Clear
9 Sunny
10 Sunny
11 Sunny
12 Sunny
13 Sunny
14 Sunny
15 Sunny
16 Sunny
17 Sunny
18 Clear
19 Clear
20 Clear
Mensaje - template a enviar
# template para luego enviar al celular
template = '\nHola Carlos! \n\n El pronostico del tiempo para hoy '+ df['Fecha'][0] + ' en '+ query + ' es : \n\n '+ str(df_f)
print(template)
Hola Carlos!
El pronostico del tiempo para hoy 2022-12-27 en Barcelona es :
Condicion
Hora
8 Clear
9 Sunny
10 Sunny
11 Sunny
12 Sunny
13 Sunny
14 Sunny
15 Sunny
16 Sunny
17 Sunny
18 Clear
19 Clear
20 Clear
# chequeando variable con el numero comprado en twilio
# PHONE_NUMBER
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 610 86 58 16'#your number
)
print('El mensaje fue enviado con éxito!' + message.sid)
El mensaje fue enviado con éxito!SM2d8fd47ceb19407ba6be425b216507d2