A small greeter for Telegram. It runs just before I wake up and tells me relevant information for my day
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.2 KiB

#!/usr/bin/env python3
import os
import math
import locale
import requests
from datetime import datetime
from dotenv import load_dotenv
load_dotenv()
locale.setlocale(locale.LC_TIME, ('es_ES', 'UTF-8'))
date_format = '%A, %d de %B del %Y'
TELEGRAM_CHAT_ID = os.environ.get('TELEGRAM_CHAT_ID')
TELEGRAM_BOT_ID = os.environ.get('TELEGRAM_BOT_ID')
CITY_NAME = os.environ.get('CITY_NAME')
COUNTRY_NAME = os.environ.get('COUNTRY_NAME')
OPEN_WEATHER_MAP_APPID = os.environ.get('OPEN_WEATHER_MAP_APPID')
# Weather information
r_weather = requests.get('https://api.openweathermap.org/data/2.5/weather'
f'?q={CITY_NAME}&units=metric'
f'&appid={OPEN_WEATHER_MAP_APPID}').json()
temp = math.floor(r_weather['main']['temp'])
temp_formatted = f'{temp}C. {"🥶" if temp < 20 else "🔥"}'
feels_like = math.floor(r_weather['main']['feels_like'])
feels_like_formatted = f'{feels_like}C. {"🥶" if temp < 20 else "🔥"}'
temp_min = math.floor(r_weather['main']['temp_min'])
temp_max = math.floor(r_weather['main']['temp_max'])
wind = math.ceil(r_weather['wind']['speed'] * 3.6)
clouds = int(r_weather['clouds']['all'])
clouds_formatted = ''
if clouds >= 75:
clouds_formatted = f'{clouds}%'
if 25 > clouds > 75:
clouds_formatted = f'{clouds}%'
if clouds <= 25:
clouds_formatted = f'{clouds}%'
# Coronavirus information
data = requests.get('https://api.covid19api.com/country/uruguay').json()
corona_date_format = '%Y-%m-%dT%H:%M:%SZ'
count = -1
higher_cases = 0
higher_cases_day = ''
higher_death = 0
higher_death_day = ''
higher_recovered = 0
higher_recovered_day = ''
for day in data:
confirmed = day['Confirmed']
confirmed_yesterday = data[count]['Confirmed']
death_yesterday = data[count]['Deaths']
recovered_yesterday = data[count]['Recovered']
death = day['Deaths']
recovered = day['Recovered']
active = day['Active']
date = day['Date']
confirmed_than_yesterday = confirmed - confirmed_yesterday
death_than_yesterday = death - death_yesterday
recovered_than_yesterday = recovered - recovered_yesterday
if confirmed_than_yesterday < 0:
confirmed_than_yesterday = 0
if death_than_yesterday < 0:
death_than_yesterday = 0
if confirmed_than_yesterday > higher_cases:
higher_cases = confirmed_than_yesterday
higher_cases_day = datetime.strptime(
date, corona_date_format).strftime(date_format)
if death_than_yesterday > higher_death:
higher_death = death_than_yesterday
higher_death_day = datetime.strptime(
date, corona_date_format).strftime(date_format)
if recovered_than_yesterday > higher_recovered:
higher_recovered = recovered_than_yesterday
higher_recovered_day = datetime.strptime(
date, corona_date_format).strftime(date_format)
count += 1
yesterday = data[-1]
before = data[-2]
active = yesterday['Confirmed'] - yesterday['Recovered'] - yesterday['Deaths']
confirmed = yesterday['Confirmed'] - before['Confirmed']
dead = yesterday['Deaths'] - before['Deaths']
recovered = yesterday['Recovered'] - before['Recovered']
infection_ratio = confirmed - recovered
total_dead = yesterday['Deaths']
message = f"""
¡Buen dia Roger!
Hoy es {datetime.today().strftime("%A, %d de %B del %Y")}
<b>⛅Clima:⛅</b>
Temperatura en Montevideo 🇺🇾:
<b> Actual:</b> {temp_formatted}
<b> Se siente como:</b> {feels_like_formatted}
<b> Minimo:</b> {temp_min}C.
<b> Maximo:</b> {temp_max}C.
<b> Viento:</b> {wind} km/h 💨
<b> Nubosidad:</b> {clouds_formatted}
<b>☣Coronavirus:☣</b>
<b> Ayer:</b>
🏥Activos: {active}
😷Confirmados: {confirmed}
⚰Muertos: {dead}
🎉Recuperados: {recovered}
〽Ratio de infecciones: {infection_ratio}
<b> Historico:</b>
⚰Muertes totales: {yesterday['Deaths']}
😷Dia con mas confirmados: {higher_cases_day} con {higher_cases}
⚰Dia con mas muertos: {higher_death_day} con {higher_death}
🏥Dia con mas recuperados: {higher_recovered_day} con {higher_recovered}
"""
telegram_data = {
"chat_id": TELEGRAM_CHAT_ID,
"parse_mode": "HTML",
"text": message
}
requests.post('https://api.telegram.org/bot'
f'{TELEGRAM_BOT_ID}/sendmessage', data=telegram_data)