1
+ #!/usr/bin/env python
2
+
3
+ import json
4
+ import requests
5
+ from datetime import datetime
6
+
7
+ WEATHER_CODES = {
8
+ '113' : '☀️ ' ,
9
+ '116' : '⛅ ' ,
10
+ '119' : '☁️ ' ,
11
+ '122' : '☁️ ' ,
12
+ '143' : '☁️ ' ,
13
+ '176' : '🌧️' ,
14
+ '179' : '🌧️' ,
15
+ '182' : '🌧️' ,
16
+ '185' : '🌧️' ,
17
+ '200' : '⛈️ ' ,
18
+ '227' : '🌨️' ,
19
+ '230' : '🌨️' ,
20
+ '248' : '☁️ ' ,
21
+ '260' : '☁️ ' ,
22
+ '263' : '🌧️' ,
23
+ '266' : '🌧️' ,
24
+ '281' : '🌧️' ,
25
+ '284' : '🌧️' ,
26
+ '293' : '🌧️' ,
27
+ '296' : '🌧️' ,
28
+ '299' : '🌧️' ,
29
+ '302' : '🌧️' ,
30
+ '305' : '🌧️' ,
31
+ '308' : '🌧️' ,
32
+ '311' : '🌧️' ,
33
+ '314' : '🌧️' ,
34
+ '317' : '🌧️' ,
35
+ '320' : '🌨️' ,
36
+ '323' : '🌨️' ,
37
+ '326' : '🌨️' ,
38
+ '329' : '❄️ ' ,
39
+ '332' : '❄️ ' ,
40
+ '335' : '❄️ ' ,
41
+ '338' : '❄️ ' ,
42
+ '350' : '🌧️' ,
43
+ '353' : '🌧️' ,
44
+ '356' : '🌧️' ,
45
+ '359' : '🌧️' ,
46
+ '362' : '🌧️' ,
47
+ '365' : '🌧️' ,
48
+ '368' : '🌧️' ,
49
+ '371' : '❄️' ,
50
+ '374' : '🌨️' ,
51
+ '377' : '🌨️' ,
52
+ '386' : '🌨️' ,
53
+ '389' : '🌨️' ,
54
+ '392' : '🌧️' ,
55
+ '395' : '❄️ '
56
+ }
57
+
58
+ data = {}
59
+
60
+
61
+ weather = requests .get ("https://wttr.in/?format=j1" ).json ()
62
+
63
+
64
+ def format_time (time ):
65
+ return time .replace ("00" , "" ).zfill (2 )
66
+
67
+
68
+ def format_temp (temp ):
69
+ return (hour ['FeelsLikeC' ]+ "°" ).ljust (3 )
70
+
71
+ def format_chances (hour ):
72
+ chances = {
73
+ "chanceoffog" : "Fog" ,
74
+ "chanceoffrost" : "Frost" ,
75
+ "chanceofovercast" : "Overcast" ,
76
+ "chanceofrain" : "Rain" ,
77
+ "chanceofsnow" : "Snow" ,
78
+ "chanceofsunshine" : "Sunshine" ,
79
+ "chanceofthunder" : "Thunder" ,
80
+ "chanceofwindy" : "Wind"
81
+ }
82
+
83
+ conditions = []
84
+ for event in chances .keys ():
85
+ if int (hour [event ]) > 0 :
86
+ conditions .append (chances [event ]+ " " + hour [event ]+ "%" )
87
+ return ", " .join (conditions )
88
+
89
+ tempint = int (weather ['current_condition' ][0 ]['FeelsLikeC' ])
90
+ extrachar = ''
91
+ if tempint > 0 and tempint < 10 :
92
+ extrachar = '+'
93
+
94
+
95
+ data ['text' ] = ' ' + WEATHER_CODES [weather ['current_condition' ][0 ]['weatherCode' ]] + \
96
+ " " + extrachar + weather ['current_condition' ][0 ]['FeelsLikeC' ]+ "°" + " | " + weather ['nearest_area' ][0 ]['areaName' ][0 ]['value' ]+ \
97
+ ", " + weather ['nearest_area' ][0 ]['country' ][0 ]['value' ]
98
+
99
+ data ['tooltip' ] = f"<b>{ weather ['current_condition' ][0 ]['weatherDesc' ][0 ]['value' ]} { weather ['current_condition' ][0 ]['temp_C' ]} °</b>\n "
100
+ data ['tooltip' ] += f"Feels like: { weather ['current_condition' ][0 ]['FeelsLikeC' ]} °\n "
101
+ data ['tooltip' ] += f"Location: { weather ['nearest_area' ][0 ]['areaName' ][0 ]['value' ]} \n "
102
+ data ['tooltip' ] += f"Wind: { weather ['current_condition' ][0 ]['windspeedKmph' ]} Km/h\n "
103
+ data ['tooltip' ] += f"Humidity: { weather ['current_condition' ][0 ]['humidity' ]} %\n "
104
+ for i , day in enumerate (weather ['weather' ]):
105
+ data ['tooltip' ] += f"\n <b>"
106
+ if i == 0 :
107
+ data ['tooltip' ] += "Today, "
108
+ if i == 1 :
109
+ data ['tooltip' ] += "Tomorrow, "
110
+ data ['tooltip' ] += f"{ day ['date' ]} </b>\n "
111
+ data ['tooltip' ] += f"⬆️ { day ['maxtempC' ]} ° ⬇️ { day ['mintempC' ]} ° "
112
+ data ['tooltip' ] += f"🌅 { day ['astronomy' ][0 ]['sunrise' ]} 🌇 { day ['astronomy' ][0 ]['sunset' ]} \n "
113
+ for hour in day ['hourly' ]:
114
+ if i == 0 :
115
+ if int (format_time (hour ['time' ])) < datetime .now ().hour - 2 :
116
+ continue
117
+ data ['tooltip' ] += f"{ format_time (hour ['time' ])} { WEATHER_CODES [hour ['weatherCode' ]]} { format_temp (hour ['FeelsLikeC' ])} { hour ['weatherDesc' ][0 ]['value' ]} , { format_chances (hour )} \n "
118
+
119
+
120
+ print (json .dumps (data ))
0 commit comments