-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather.py
executable file
·133 lines (100 loc) · 3.84 KB
/
weather.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/usr/bin/env python
## By Davoud Arsalani
## https://github.com/davoudarsalani/scripts
## https://github.com/davoudarsalani/scripts/blob/master/weather.py
## https://raw.githubusercontent.com/davoudarsalani/scripts/master/weather.py
## https://davoudarsalani.ir
from json import loads
from os import getenv
from sys import argv
from jdatetime import datetime as jdt
from requests import Session
from gp import get_headers, msgc, msgn, set_widget, refresh_icon, last_file_exists, save_as_last, save_error, get_last
lang = 'en'
lat = 29.4519 ## zahedan latitude
lon = 60.8842 ## zahedan longitude
appid = getenv('api_weather')
url = f'https://api.openweathermap.org/data/2.5/onecall?lang={lang}&lat={lat}&lon={lon}&units=metric&exclude=hourly,minutely&appid={appid}'
Ses = Session()
hdrs = get_headers()
last_file = f'{getenv("HOME")}/main/scripts/.last/weather'
error_file = f'{getenv("HOME")}/main/scripts/.error/weather'
arg = argv[1]
def get_info() -> dict[str, str]: ## {{{
resp = Ses.get(url, headers=hdrs, timeout=20)
resp = loads(resp.text) ## dict
return resp
## }}}
def get_current() -> tuple[str, int, str]: ## {{{
current = resp['current'] ## dict
dt = current['dt'] ## int
weekday = jdt.fromtimestamp(dt).strftime('%A')[:3]
temp = int(current['temp']) ## int
weather = current['weather'] ## list
description = weather[0]['description']
return weekday, temp, description
## }}}
def get_forecast() -> tuple[str, int, int, str]: ## {{{
dt = day['dt'] ## int
weekday = jdt.fromtimestamp(dt).strftime('%A')[:3]
temp = day['temp'] ## dict
temp_min = int(temp['min'])
temp_max = int(temp['max'])
weather = day['weather'] ## list
description = weather[0]['description']
return weekday, temp_min, temp_max, description
## }}}
if arg == 'update':
set_widget('weather', 'fg', 'reset')
set_widget('weather', 'markup', refresh_icon())
try:
resp = get_info()
## get last temp
if last_file_exists(last_file):
temp_last = int(get_last(last_file))
## get new temp, description
_, temp, description = get_current()
## save temp as last to compare in our next read
save_as_last(last_file, temp)
## compare
if last_file_exists(last_file):
if temp == temp_last:
icon = ''
else:
if temp > temp_last:
diff = temp - temp_last
icon = f'+{diff} '
elif temp < temp_last:
diff = temp_last - temp
icon = f'-{diff} '
else:
icon = ''
text = f'{icon}{temp}° {description}'
except Exception as exc:
text = 'WE'
## save error
save_error(error_file, f'{exc!r}')
set_widget('weather', 'fg', getenv('red'))
finally:
set_widget('weather', 'markup', text)
elif arg == 'forecast':
try:
resp = get_info()
## current
message = f'<span color=\"{getenv("orange")}\">Current</span>\n'
weekday, temp, description = get_current()
message = f'{message}{weekday}\t{temp}°\t\t\t{description}'
## forecast
message = f'{message}\n\n<span color=\"{getenv("orange")}\">Forecast</span>\n'
days = resp['daily'] ## list
for day in days: ## day is a dict
weekday, temp_min, temp_max, description = get_forecast()
if weekday == 'Fri' or weekday == 'Sat':
tabs = '\t' * 2
else:
tabs = '\t'
message = f'{message}{weekday}{tabs}{temp_min}°-{temp_max}°\t{description}\n'
message = message.strip()
msgn(message, duration=20)
except Exception as exc:
msgc('ERROR', f'gettin forecast\n{exc!r}', f'{getenv("HOME")}/main/linux/themes/alert-w.png')