-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather
executable file
·82 lines (74 loc) · 2.75 KB
/
weather
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
#!/usr/bin/python3.10
from typing import List
from urllib.request import urlopen
from datetime import datetime
from json import loads
import yaml
from yaml.loader import SafeLoader
from geopy.geocoders import Nominatim
with open('/home/antoniom/.config/polybar/scripts/polybar-weather/config.yaml', 'r') as configfile:
cfg = yaml.load(configfile, Loader=SafeLoader)
if type(cfg['config']['location']) == str:
loc = Nominatim(user_agent='GetLoc')
try:
getLoc = loc.geocode(cfg['config']['location'])
except:
print()
longitude = getLoc.longitude
latitude = getLoc.latitude
elif type(cfg['config']['location']) == dict:
longitude = cfg['config']['location']['longitude']
latitude = cfg['config']['location']['latitude']
else:
longitude = 0
latitude = 0
url = 'https://api.open-meteo.com/v1/forecast?daily=sunset&daily=sunrise¤t_weather=true'
url += '&latitude=' + str(latitude)
url += '&longitude=' + str(longitude)
url += '&timezone=' + str(cfg['config']['timezone'])
url += '&windspeed_unit=' + str(cfg['config']['units']['windspeed'])
url += '&precipitation_unit=' + str(cfg['config']['units']['precipitation'])
url += '&temperature_unit=' + str(cfg['config']['units']['temperature'])
current_time = datetime.now()
try:
data_json = loads(urlopen(url).read())
except:
print()
sunrise_time = datetime.strptime(data_json['daily']['sunrise'][0], '%Y-%m-%dT%H:%M')
sunset_time = datetime.strptime(data_json['daily']['sunset'][0], '%Y-%m-%dT%H:%M')
at_night = lambda sunset, sunrise, current : 1 if (current >= sunset or current <= sunrise) else 0
if cfg['config']['units']['temperature'] == 'celsius':
tmp_unit = '°C'
else:
tmp_unit = '°F'
wmo_to_icons = {
0: [u'\ue30d', u'\ue32b'],
1: [u'\ue30c', u'\ue379'],
2: [u'\ue30c', u'\ue379'],
3: [u'\ue30c', u'\ue379'],
45: [u'\ue313', u'\ue313'],
48: [u'\ue313', u'\ue313'],
51: [u'\ue316', u'\ue316'],
53: [u'\ue316', u'\ue316'],
55: [u'\ue316', u'\ue316'],
56: [u'\ue316', u'\ue316'],
57: [u'\ue316', u'\ue316'],
61: [u'\ue318', u'\ue318'],
63: [u'\ue318', u'\ue318'],
65: [u'\ue318', u'\ue318'],
66: [u'\ue318', u'\ue318'],
67: [u'\ue318', u'\ue318'],
71: [u'\ue31a', u'\ue31a'],
73: [u'\ue31a', u'\ue31a'],
75: [u'\ue31a', u'\ue31a'],
77: [u'\ue314', u'\ue314'],
80: [u'\ue319', u'\ue319'],
81: [u'\ue319', u'\ue319'],
82: [u'\ue319', u'\ue319'],
85: [u'\ue31a', u'\ue31a'],
86: [u'\ue31a', u'\ue31a'],
95: [u'\ue31d', u'\ue31d'],
96: [u'\ue31d', u'\ue31d'],
99: [u'\ue31d', u'\ue31d']
}
print(wmo_to_icons[data_json['current_weather']['weathercode']][at_night(sunset_time, sunrise_time, current_time)], '', str(data_json['current_weather']['temperature']) + tmp_unit)