#!/usr/bin/env python3

import json
import datetime
import sys
import urllib.request

weekdagen = ('ma', 'di', 'wo', 'do', 'vr')
deltas = {'morgen': 1,
          'overmorgen': 2}

d = datetime.date.today()
if len(sys.argv) == 2:
    d += datetime.timedelta(deltas.get(sys.argv[1], 0))

    if sys.argv[1][0:2] in weekdagen:
        while d.weekday() != weekdagen.index(sys.argv[1][0:2]):
            d += datetime.timedelta(1)

menu = json.loads(urllib.request.urlopen(
    "http://zeus.ugent.be/hydra/api/1.0/resto/week/{}.json".format(d.isocalendar()[1])).read().decode('utf-8'))

if str(d) not in menu:
    exit("Restaurant gesloten")

print("{}[{}]{}".format("=" * 19, d, "=" * 19))
print("{}[ SOEP ]{}".format("=" * 21, "=" * 21))
print("* {}".format(menu[str(d)]["soup"]["name"]))
print("{}[ HOOFDGERECHTEN ]{}".format("=" * 16, "=" * 16))
[print("* {}".format(g["name"])) for g in menu[str(d)]["meat"]]
print("{}[ GROENTEN ]{}".format("=" * 21, "=" * 21))
[print("* {}".format(v)) for v in menu[str(d)]["vegetables"]]

@@ -79,16 +79,301 @@
 equest%0A%0A
+def header(text, fillchar='-', width=40):%0A    tofill = max(width - 4 - len(text), 0)%0A    tofill1 = tofill // 2%0A    tofill2 = tofill1 + (1 if tofill %25 2 else 0)%0A    %0A    print('%7B%7D%5B %7B%7D %5D%7B%7D'.format(%0A        fillchar * tofill1,%0A        text,%0A        fillchar * tofill2,%0A    ))%0A%0A# What day%0A
 weekdage
@@ -706,16 +706,33 @@
 lta(1)%0A%0A
+# Fetch from API%0A
 menu = j
@@ -878,16 +878,49 @@
 f-8'))%0A%0A
+# Get wanted day in API response%0A
 if str(d
@@ -970,104 +970,86 @@
 n%22)%0A
-%0Aprint(%22%7B%7D%5B%7B%7D%5D%7B%7D%22.format(%22=%22 * 19, d, %22=%22 * 19))%0Aprint(%22%7B%7D%5B SOEP %5D%7B%7D%22.format(%22=%22 * 21, %22=%22 * 21)
+today = menu%5Bstr(d)%5D%0A%0A# Print menu%0Aheader(str(d), fillchar='=')%0A%0Aheader('SOEP'
 )%0Apr
@@ -1066,28 +1066,21 @@
 .format(
-menu%5Bstr(d)%5D
+today
 %5B%22soup%22%5D
@@ -1090,27 +1090,25 @@
 ame%22%5D))%0A
-print(%22%7B%7D%5B 
+%0Aheader('
 HOOFDGER
@@ -1117,217 +1117,145 @@
 HTEN
- %5D%7B%7D%22.format(%22=%22 * 16, %22=%22 * 16))%0A%5Bprint(%22* %7B%7D%22.format(g%5B%22name%22%5D)) for g in menu%5Bstr(d)%5D%5B%22meat%22%5D%5D%0Aprint(%22%7B%7D%5B GROENTEN %5D%7B%7D%22.format(%22=%22 * 21, %22=%22 * 21))%0A%5Bprint(%22* %7B%7D%22.format(v)) for v in menu%5Bstr(d)%5D%5B%22vegetables%22%5D%5D
+')%0Afor g in today%5B%22meat%22%5D:%0A    print(%22* %7B%7D%22.format(g%5B%22name%22%5D))%0A%0Aheader('GROENTEN')%0Afor v in today%5B%22vegetables%22%5D:%0A    print(%22* %7B%7D%22.format(v))
 %0A

Improve header generation; add comments