86 lines
2.7 KiB
Python
86 lines
2.7 KiB
Python
from geopy import Nominatim
|
|
import requests
|
|
import json
|
|
import datetime
|
|
|
|
class Whether:
|
|
def __init__(self, city: str):
|
|
gn = Nominatim(user_agent = "kzntsvartem@gmail.com")
|
|
self.__location = gn.geocode(city)
|
|
|
|
def get_weather(self) -> str:
|
|
url = f'https://api.weather.yandex.ru/v2/forecast?lat={self.__location.latitude}&lon={self.__location.longitude}'
|
|
access_key = open("whether_token").read()
|
|
|
|
header = {
|
|
'X-Yandex-Weather-Key' : access_key
|
|
}
|
|
|
|
#response = requests.get(url, headers = header)
|
|
#with open('data.json', 'w', encoding='utf-8') as f:
|
|
# json.dump(response.json(), f, ensure_ascii=False, indent=4)
|
|
#print(json.dumps(r, indent = 4))
|
|
|
|
with open('data.json') as json_data:
|
|
d = json.load(json_data)
|
|
json_data.close()
|
|
|
|
fact = d["fact"]
|
|
|
|
print(type(fact["cloudness"]))
|
|
|
|
string = "Погода сейчас:\n"
|
|
string += self.__get_cloudness(fact["cloudness"])
|
|
string += self.__get_temp(fact["temp"], fact["feels_like"])
|
|
string += self.__get_humidity(fact["humidity"])
|
|
string += self.__get_wind(fact["wind_dir"], fact["wind_speed"], fact["wind_gust"])
|
|
string += self.__get_prec(fact["prec_type"], fact["prec_strength"])
|
|
|
|
return string
|
|
return "Что-то пошло не так :("
|
|
|
|
def __get_cloudness(self, cloudness: float) -> str:
|
|
text = ""
|
|
if cloudness >= 0.8:
|
|
text += "Пасмурно"
|
|
elif cloudness >= 0.4:
|
|
text += "Облачно с прояснениями"
|
|
elif cloudness >= 0.1:
|
|
text += "Малооблачно"
|
|
else:
|
|
text += "Ясно"
|
|
|
|
text += "\n"
|
|
return text
|
|
|
|
def __get_humidity(self, humidity: int) -> str:
|
|
text = f"Влажность: {humidity}% ("
|
|
|
|
if humidity == 100:
|
|
text += "Кто проживает на дне океана?)"
|
|
elif humidity >= 90:
|
|
text += "Очень высокая"
|
|
elif humidity >= 70:
|
|
text += "Высокая"
|
|
elif humidity >= 50:
|
|
text += "Средняя"
|
|
elif humidity >= 30:
|
|
text += "Низкая"
|
|
else:
|
|
text += "Сушь галимая"
|
|
|
|
text += ")\n"
|
|
|
|
return text
|
|
|
|
def __get_temp(self, temp: int, feels: int) -> str:
|
|
text = f"Температура: {temp}°C\n"
|
|
text += f"(Ощущается как: {feels}°C)\n"
|
|
|
|
return text
|
|
|
|
def __get_wind(self, dir: str, speed: float, gust: float) -> str:
|
|
return ""
|
|
|
|
def __get_prec(self, type: int, strength: float) -> str:
|
|
return "" |