-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwya.py
117 lines (98 loc) · 3.14 KB
/
wya.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
import requests
import json
API_URL = "https://api.weather.yandex.ru/graphql/query"
API_KEY = "demo_yandex_weather_api_key_ca6d09349ba0" # Замените на свой ключ
LATITUDE = 55.030199
LONGITUDE = 82.92043
WEATHER_ICONS = {
"CLEAR": "", # ☀️
"PARTLY_CLOUDY": "", # 🌤
"CLOUDY": "", # ☁️
"OVERCAST": "", # 🌥
"LIGHT_RAIN": "", # 🌦
"RAIN": "", # 🌧
"HEAVY_RAIN": "", # 🌩
"SHOWERS": "", # 🌧
"SLEET": "", # 🌨
"LIGHT_SNOW": "", # 🌨
"SNOW": "", # ❄️
"SNOWFALL": "", # 🌨
"HAIL": "", # 🌩
"THUNDERSTORM": "", # ⛈
"THUNDERSTORM_WITH_RAIN": "", # 🌩
"THUNDERSTORM_WITH_HAIL": "", # 🌩
}
WIND_ICONS = {
"NORTH": "",
"NORTH_EAST": "",
"EAST": "",
"SOUTH_EAST": "",
"SOUTH": "",
"SOUTH_WEST": "",
"WEST": "",
"NORTH_WEST": "",
"CALM": " ",
}
CONDITION_TRANSLATION = {
"CLEAR": "Ясно",
"PARTLY_CLOUDY": "Малооблачно",
"CLOUDY": "Облачно",
"OVERCAST": "Пасмурно",
"LIGHT_RAIN": "Небольшой дождь",
"RAIN": "Дождь",
"HEAVY_RAIN": "Сильный дождь",
"SHOWERS": "Ливень",
"SLEET": "Дождь со снегом",
"LIGHT_SNOW": "Небольшой снег",
"SNOW": "Снег",
"SNOWFALL": "Снегопад",
"HAIL": "Град",
"THUNDERSTORM": "Гроза",
"THUNDERSTORM_WITH_RAIN": "Гроза с дождем",
"THUNDERSTORM_WITH_HAIL": "Гроза с градом",
}
def fetch_weather():
query = {
"query": f'''
{{
weatherByPoint(request: {{ lat: {LATITUDE}, lon: {LONGITUDE} }}) {{
now {{
temperature
windSpeed
windDirection
condition
}}
}}
}}
'''
}
headers = {
"x-yandex-weather-key": API_KEY,
"Content-Type": "application/json",
}
response = requests.post(API_URL, headers=headers, json=query)
return response.json()
def get_weather_data():
data = fetch_weather()["data"]["weatherByPoint"]["now"]
temperature = data["temperature"]
condition = data["condition"]
wind_speed = data["windSpeed"]
wind_direction = data["windDirection"]
return temperature, condition, wind_speed, wind_direction
def format_weather():
temperature, condition, wind_speed, wind_direction = get_weather_data()
weather_icon = WEATHER_ICONS.get(condition, "?")
wind_icon = WIND_ICONS.get(wind_direction, "?")
condition_translate = CONDITION_TRANSLATION.get(condition, condition.capitalize())
text = f"{weather_icon} {temperature}°C"
tooltip = f"{condition_translate}, {temperature}°C\n" \
f"Ветер: {wind_speed} м/с {wind_icon}"
return {
"text": text,
"tooltip": tooltip,
"class": "normal"
}
if __name__ == "__main__":
weather = format_weather()
print(json.dumps(weather))