Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # http://api.openweathermap.org/data/2.5/weather?q={city name}&appid={API key}
- import requests
- from pydantic import BaseModel
- s_city = input() # ввод города на английском
- appid = "7b2c7af1b22f90bac257d092cc195d0b" # полученный при регистрации на OpenWeatherMap.org.
- class Weather(BaseModel): # класс погоды
- id: int
- conditions: str
- temp: float
- temp_min: float
- temp_max: float
- list_forecast: list
- BASEURL = "http://api.openweathermap.org/data/2.5/"
- try:
- params = {'q': s_city, 'units': 'metric', 'lang': 'ru', 'APPID': appid}
- # запрос на получения температуры нашего города
- response = requests.get(BASEURL + "weather", params = params)
- data = response.json()
- external_data = {
- 'id': data['id'],
- 'conditions': data['weather'][0]['description'],
- 'temp': data['main']['temp'],
- 'temp_min': data['main']['temp_min'],
- 'temp_max': data['main']['temp_max'],
- 'list_forecast': [],
- }
- weather = Weather(**external_data)
- print("conditions:", weather.conditions)
- print("temp:", weather.temp)
- print("temp_min:", weather.temp_min)
- print("temp_max:", weather.temp_max)
- print()
- try:
- params = {'id': weather.id, 'units': 'metric', 'lang': 'ru', 'APPID': appid}
- # запрос на получения списка температур за 5 дней нашего города
- response = requests.get(BASEURL + "forecast", params=params)
- data = response.json()
- weather.list_forecast = data['list']
- for i in weather.list_forecast:
- print((i['dt_txt'])[:16], "|", i['main']['temp'], "|", str(i['wind']['speed']) + " м/с",
- "|", i['weather'][0]['description'])
- except Exception as e:
- print("Exception (forecast):", e)
- pass
- except Exception as e:
- print("Exception (weather):", e)
- pass
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement