72 lines
2.7 KiB
Python
Executable File
72 lines
2.7 KiB
Python
Executable File
from config import open_weather_API_token
|
||
from pprint import pprint
|
||
import datetime
|
||
import time
|
||
import requests
|
||
|
||
|
||
def get_weather(city, open_weather_API_token):
|
||
code_to_smile = {
|
||
"Clear": "Ясно \U00002600",
|
||
"Clouds": "Облачно \U00002601",
|
||
"Rain": "Дождь \U00002614",
|
||
"Drizzle": "Дождь \U00002614",
|
||
"Thunderstorm": "Гроза \U000026A1",
|
||
"Snow": "Снег \U0001F328",
|
||
"Mist": "Туман \U0001F32B"
|
||
}
|
||
|
||
try:
|
||
r = requests.get(
|
||
f"https://api.openweathermap.org/data/2.5/weather?q={city}&appid={open_weather_API_token}&units=metric"
|
||
)
|
||
|
||
# days_ago = int(time.time()) - (86400 * (n == 1))
|
||
# r_2 = requests.get(
|
||
# f"https://api.openweathermap.org/data/2.5/onecall/timemachine?lat=55.7522&lon=37.6156&dt={days_ago}&appid={open_weather_API_token}&units=metric&lang=ru"
|
||
# )
|
||
|
||
data = r.json()
|
||
# data_2 = r_2.json()
|
||
#pprint(data)
|
||
|
||
city = data["name"]
|
||
cur_weather = data["main"]["temp"]
|
||
|
||
weather_description = data["weather"][0]["main"]
|
||
if weather_description in code_to_smile:
|
||
wd = code_to_smile[weather_description]
|
||
else:
|
||
wd = "Я понятия не имею, что у тебя там творится, выгялни в окно и посмотри!"
|
||
|
||
humidity = data["main"]["humidity"]
|
||
pressure = data["main"]["pressure"]
|
||
wind = data["wind"]["speed"]
|
||
sunrise_timestamp = datetime.datetime.fromtimestamp(data["sys"]["sunrise"])
|
||
sunset_timestamp = datetime.datetime.fromtimestamp(data["sys"]["sunset"])
|
||
length_of_the_day = sunset_timestamp - sunrise_timestamp
|
||
print(f"***{datetime.datetime.now().strftime('%Y-%m-%d %H:%M')}***\n"
|
||
f"Погода в городе: {city}\nТемпература: {cur_weather}C° {wd}\n"
|
||
f"Влажность: {humidity}%\nДавление: {pressure} мм.рт.ст\n"
|
||
f"Ветер: {wind} м/c\nВосход солнца: {sunrise_timestamp}\n"
|
||
f"Закат солнца: {sunset_timestamp}\nПродолжительность дня: {length_of_the_day}\n"
|
||
f"Хорошего дня!")
|
||
|
||
except Exception as ex:
|
||
print(ex)
|
||
print("Проверьте название города")
|
||
|
||
|
||
# def print_weather(city, open_weather_API_token):
|
||
# weather = get_weather(city, open_weather_API_token):
|
||
# return weather
|
||
|
||
def main():
|
||
city = input('Введите город: ')
|
||
#n = int(input('Сколько дней назад: '))
|
||
get_weather(city, open_weather_API_token)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|