Last active
August 19, 2024 23:23
-
-
Save ElectrWeakHyprCharge/06411369070a3dfc7aeadd0713d4e5f8 to your computer and use it in GitHub Desktop.
Tiempo Actual de Inumet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
from urllib.request import urlopen | |
from collections import namedtuple | |
class Estación(namedtuple('Estación', 'nombre estación id')): | |
ARTIGAS = 16 | |
RIVERA = 235 | |
MELO = 160 | |
SALTO = 239 | |
YOUNG = 293 | |
PAYSANDU = 201 | |
TRINIDAD = 276 | |
FLORIDA = 110 | |
ROCHA = 236 | |
CARRASCO = 39 | |
TREINTA_Y_TRES = 272 | |
MERCEDES = 162 | |
DURAZNO = 96 | |
SAN_JOSE = 252 | |
COLONIA = 66 | |
PASO_DE_LOS_TOROS = 200 | |
MELILLA = 159 | |
PRADO = 211 | |
LAGUNA_DEL_SAUCE = 138 | |
PUNTA_DEL_ESTE = 219 | |
TACUAREMBO = 263 | |
#No incluí todos | |
class Variable(namedtuple('Variable', 'nombre variable unidad id')): | |
VISIBILIDAD_HORIZONTAL = 74 | |
DIRECCIÓN_DEL_VIENTO = 8 | |
INTENSIDAD_DEL_VIENTO = 29 | |
INTENSIDAD_DE_RÁFAGA = 28 | |
INTENSIDAD_DEL_VIENTO_MÁXIMA_HORARIA = 105 | |
TEMPERATURA_DEL_AIRE = 47 | |
HUMEDAD_RELATIVA = 25 | |
TEMPERATURA_DE_PUNTO_DE_ROCÍO = 59 | |
TENSIÓN_DEL_VAPOR_DE_AGUA = 62 | |
PRESIÓN_ATMOSFÉRICA_A_NIVEL_DE_ESTACIÓN = 43 | |
PRESIÓN_ATMOSFÉRICA_A_NIVEL_DEL_MAR = 45 | |
ESTADO_ACTUAL_DEL_TIEMPO = 123 # <- Este decide qué ícono poner | |
NUBES = 31 | |
NUBOSIDAD_DEL_CIELO = 3 | |
PRECIPITACIÓN_ACUMULADA_HORARIA = 94 | |
#Hasta donde yo sé, están todos | |
class TiempoActual: | |
def __init__(self): | |
data = urlopen('https://inumet.gub.uy/reportes/estadoActual/estadoActualDatosHorarios.mch').read() | |
self.json = json.loads(str(data, encoding = 'utf-8')) #Ponele que es utf 8, debe ser ascii, pero no importa | |
map_vars = {'nombre':'nombre', 'idInt':'id', 'unidad':'unidad', 'variable':'variable'} | |
map_ests = {'Estacion':'estación', 'NombreEstacion':'nombre', 'id':'id'} | |
self.variables = [Variable(**{map_vars[k]:v for k, v in elem.items()}) for elem in self.json['variables']] | |
self.estaciones = [Estación(**{map_ests[k]:v for k, v in elem.items()}) for elem in self.json['estaciones']] | |
self.variables_id = [elem['idInt'] for elem in self.json['variables']] | |
self.estaciones_id = [elem['id'] for elem in self.json['estaciones']] | |
self.observaciones = self.json['observaciones'] | |
def obtener_observación(self, estación, variable): | |
var = self.variables.index(variable) | |
est = self.estaciones.index(estación) | |
return self.observaciones[var]['datos'][est][0] | |
def obtener_observaciones(self, estación, *variables): | |
for variable in variables: | |
var = self.variables.index(variable) | |
est = self.estaciones.index(estación) | |
yield self.observaciones[var]['datos'][est][0] | |
def obtener_observación_por_id(self, estación, variable): | |
var = self.variables_id.index(variable) | |
est = self.estaciones_id.index(estación) | |
return self.observaciones[var]['datos'][est][0] | |
def obtener_observaciones_por_id(self, estación, *variables): | |
for variable in variables: | |
var = self.variables_id.index(variable) | |
est = self.estaciones_id.index(estación) | |
yield self.observaciones[var]['datos'][est][0] | |
def actualizar(self): self.__init__() | |
def día_o_noche(): #Es una función al pedo, pero ta | |
return json.loads(str(urlopen('https://inumet.gub.uy/services/dianoche').read(), encoding = 'utf-8'))['result'] | |
#Versión mejorada (en mi opinión) de lo que usa Inumet, que está en | |
#"https://inumet.gub.uy/views/tiempo/js/estadoActualDatosHorarios.js" | |
#Desde Ctrl+F "if (existeValor($dirViento)) {", en function getStrViento(...) | |
DIRECCIONES_CARDINALES = ['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW'] | |
def grados_a_cardinal(deg): return DIRECCIONES_CARDINALES[round(deg/22.5) % 16] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-------------------------------------------------------------------------------------------------------# | |
# NOTA: No todos los datos están siempre disponibles, da None en ese caso (lo mismo pasa en la tabla de # | |
# inumet.gub.uy/tiempo/estadoActual, no se muestran todos los datos de todas las estaciones siempre). # | |
#-------------------------------------------------------------------------------------------------------# | |
import inumet, pprint | |
ta = inumet.TiempoActual() | |
#Esto es equivalente a lo que está en https://inumet.gub.uy/tiempo/estadoActual | |
print(ta.obtener_observación_por_id( | |
inumet.Estación.CARRASCO, | |
inumet.Variable.INTENSIDAD_DEL_VIENTO | |
)) #Ejemplo: 7 | |
observaciones = ta.obtener_observaciones_por_id( | |
inumet.Estación.LAGUNA_DEL_SAUCE, | |
inumet.Variable.INTENSIDAD_DEL_VIENTO, | |
inumet.Variable.DIRECCIÓN_DEL_VIENTO, | |
inumet.Variable.INTENSIDAD_DEL_VIENTO_MÁXIMA_HORARIA | |
) | |
print(list(observaciones)) #Ejemplo [9, '170', None] | |
#9 la intensidad; | |
#170° la dirección del viento; pasándolo por grados_a_cardinal() dice 'S' (Sur) | |
#el None es porque no está disponible la máxima horaria de la intensidad del viento | |
#Muestra todas las estaciones, con nombre, id, y un como código de estación | |
pprint.pprint(ta.estaciones) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
excelente! muy útil y funciona perfecto. Muchas gracias