summaryrefslogtreecommitdiff
path: root/.local/bin/weather
blob: 388254bcaf6d213e1b7295fc72f4f50e9f398a4d (plain)
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#!/usr/bin/env python3

import sys ,requests ,json ,argparse
from datetime import datetime, timedelta

# Set variables
location_id = 2618425
colors      = False
show24h     = False


# Fun defs
def get_date(time):
    return datetime.strptime(time, '%Y%m%d%H%M%S')


def get_name_of_day(time):
    return datetime.strptime(time, '%Y%m%d').strftime('%A')[:3]


def min2time(minutes):
    minutes = int(minutes)
    return "{:02d}:{:02d}".format(int(minutes/100), minutes%100)


def blue(s):
    return u'\033[34m{}\033[0m'.format(s)


def green(s):
    return u'\033[32m{}\033[0m'.format(s)


def yello(s):
    return u'\033[33m{}\033[0m'.format(s)


def red(s):
    return u'\033[31m{}\033[0m'.format(s)


def colortemp(d):
    if isinstance(d, float):
        d = "{:5.1f}".format(d)
    if not colors:
        return d
    if int(float(d)) > 30:
        return red(d)
    elif int(float(d)) > 20:
        return yello(d)
    elif int(float(d)) > 10:
        return green(d)
    elif int(float(d)) < 0:
        return blue(d)
    return d


def direction(d):
    if str.startswith(d, 'NV'):
        return u'\uf542'
    elif str.startswith(d, 'NÃ'):
        return u'\uf541'
    elif str.startswith(d, 'N'):
        return u'\uf544'
    elif str.startswith(d, 'SÃ'):
        return u'\uf55a'
    elif str.startswith(d, 'SV'):
        return u'\uf55b'
    elif str.startswith(d, 'S'):
        return u'\uf55c'
    elif str.startswith(d, 'V'):
        return u'\uf553'
    elif str.startswith(d, 'Ã'): # "Ø", dmi hasn't got their shit together
        return u'\uf54c'
    else:
        return u'?'+d


def symbol(s: int) -> str:
    if s == 1: # sunny
        return u'\ue30d'
    elif s == 2: # sunny cloudy
        return u'\ue302'
    elif s == 3: # cloudy
        return u'\ue33d'
    elif s == 60: # cloudy
        return u'\ue332'
    elif s == 80: # sunny rainy
        return u'\ue308'
    elif s == 101: # night clear
        return u'\ue32b'
    elif s == 102: # night cloudy
        return u'\ue379'
    elif s == 103: # night cloudy
        return u'\ue37e'
    else:
        return u'?' + str(s)


# Parse CLI args
parser = argparse.ArgumentParser(description='Get weather data from DMI')
parser.add_argument(
    '-c',
    default=False,
    action='store_true',
    help='Color output',
)
parser.add_argument(
    '-d',
    default=False,
    action='store_true',
    help='List for the next 24h instead of the week',
)

args    = parser.parse_args()
colors  = args.c
show24h = args.d

# Get that sweet data
r = requests.get(
    'https://dmi.dk/NinJo2DmiDk/ninjo2dmidk?cmd=llj&id={}'.format(location_id)
)

r.close()

if r.status_code != 200:
    sys.exit(1)

# Get the stuff we need anyhow
data         = json.loads(r.text)
city         = data['city']
sun_schedule = (data['sunrise'], data['sunset'])

if colors:
    sun_schedule_str = "%s{} / %s{}\n" % (yello(u'\ue34c'), yello(u'\ue34d'))
else:
    sun_schedule_str = "%s{} / %s{}\n" % (u'\ue34c', u'\ue34d')

print(u'{: <10} '.format(data['city']), end='')
print(sun_schedule_str.format(
    min2time(sun_schedule[0]),
    min2time(sun_schedule[1]),
))


# 2 different types of forecast:
def week():
    # Load as JSON (since we use the `llj` cmd)
    forecast     = data['aggData']

    print("Day | min / max | UV")
    for f in forecast:
        print(u"{} | {: >2}° / {: >2}° | {:.1f}".format(
            get_name_of_day(f['time']),
            f['minTemp'],
            colortemp(f['maxTemp']),
            f['uvRadiation']))


def day():
    now = datetime.now()
    forecast = data['timeserie']
    for d in forecast:
        if d != {}:
            d['time'] = get_date(d['time'])
    forecast = [f for f in forecast if f != {} and f['time'] < now + timedelta(days=1)]

    print("Time  |  Temp  | Wind | Weather")
    for f in forecast:
        print(u"{:02d}:{:02d} | {}° | {}{:.1f} | {}".format(
            f['time'].hour, f['time'].minute,
            colortemp(f['temp']),
            direction(f['windDir']),
            f['windSpeed'],
            symbol(f['symbol'])))


if show24h:
    day()
else:
    week()