Advertisement
PrincePepper

Проект с погодой

Nov 5th, 2021
938
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | None | 0 0
  1. # http://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
  2.  
  3. import requests
  4. from pydantic import BaseModel
  5.  
  6. s_city = input()  # ввод города на английском
  7. appid = "7b2c7af1b22f90bac257d092cc195d0b"  # полученный при регистрации на OpenWeatherMap.org.
  8.  
  9.  
  10. class Weather(BaseModel):  # класс погоды
  11.     id: int
  12.     conditions: str
  13.     temp: float
  14.     temp_min: float
  15.     temp_max: float
  16.     list_forecast: list
  17.  
  18.  
  19. BASEURL = "http://api.openweathermap.org/data/2.5/"
  20.  
  21. try:
  22.     params = {'q': s_city, 'units': 'metric', 'lang': 'ru', 'APPID': appid}
  23.     # запрос на получения температуры нашего города
  24.     response = requests.get(BASEURL + "weather", params = params)
  25.  
  26.     data = response.json()
  27.     external_data = {
  28.         'id': data['id'],
  29.         'conditions': data['weather'][0]['description'],
  30.         'temp': data['main']['temp'],
  31.         'temp_min': data['main']['temp_min'],
  32.         'temp_max': data['main']['temp_max'],
  33.         'list_forecast': [],
  34.     }
  35.     weather = Weather(**external_data)
  36.  
  37.     print("conditions:", weather.conditions)
  38.     print("temp:", weather.temp)
  39.     print("temp_min:", weather.temp_min)
  40.     print("temp_max:", weather.temp_max)
  41.     print()
  42.  
  43.     try:
  44.         params = {'id': weather.id, 'units': 'metric', 'lang': 'ru', 'APPID': appid}
  45.         # запрос на получения списка температур за 5 дней нашего города
  46.         response = requests.get(BASEURL + "forecast", params=params)
  47.         data = response.json()
  48.         weather.list_forecast = data['list']
  49.  
  50.         for i in weather.list_forecast:
  51.             print((i['dt_txt'])[:16], "|", i['main']['temp'], "|", str(i['wind']['speed']) + " м/с",
  52.                   "|", i['weather'][0]['description'])
  53.     except Exception as e:
  54.         print("Exception (forecast):", e)
  55.         pass
  56. except Exception as e:
  57.     print("Exception (weather):", e)
  58.     pass
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement