Advertisement
teslariu

test_openweather

Apr 17th, 2021
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.70 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import requests
  5. from datetime import datetime
  6. r = requests.get('https://api.openweathermap.org/data/2.5/weather?q=Buenos%20Aires,AR&units=metric&lang=es&appid=6ce13bc6d5a965e991adaea8fce8676a')
  7. data = r.json()
  8. contador = 0
  9. # api_key = 6ce13bc6d5a965e991adaea8fce8676a
  10.  
  11. print("Datos crudos:")
  12. print(data)
  13. print("\nDatos con formato:")
  14. print("--------------------")
  15. print("coordenadas:")
  16.  
  17. # almacenamos la latitud y la longitud
  18. latitud = data['coord']['lat']
  19. longitud = data['coord']['lon']
  20.  
  21.  
  22. for clave, valor in list(data['coord'].items()):
  23.     print("{:>12}: {}°".format(clave, valor))
  24.    
  25. print("clima:")
  26. print("{:>12}: {}".format("Description", data['weather'][0]['description']))
  27. for clave, valor in list(data['main'].items()):
  28.     if contador < 4:
  29.         print("{:>12}: {}°C".format(clave, valor))
  30.     if contador == 4:
  31.         print("{:>12}: {} hPa".format(clave, valor))
  32.     if contador == 5:
  33.         print("{:>12}: {}%".format(clave, valor))
  34.     contador += 1
  35. print("{:>12}: {} mts".format('visibility', data['visibility']))
  36. for clave, valor in list(data['wind'].items()):
  37.     if clave == 'speed':
  38.         print("{:>12}: {} km/h".format(clave, valor))
  39.  
  40.  
  41.  
  42. r = requests.get('https://api.openweathermap.org/data/2.5/onecall?lat=-34.61&lon=-58.38&units=metric&lang=es&exclude={current,minutely,hourly}&appid=6ce13bc6d5a965e991adaea8fce8676a')
  43. data = r.json()
  44. print(data)
  45. print("\nPronostico:")
  46. for elemento in data['daily']:
  47.     print()
  48.     claves = list(elemento.keys())
  49.     for clave,valor in elemento.items():
  50.         if clave in ['dt','sunrise','sunset']:
  51.             valor = int(valor)
  52.             print(
  53.             datetime.fromtimestamp(valor).strftime('%Y-%m-%d %H:%M:%S'))
  54.         else:
  55.             print(clave, valor)
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement