#!/usr/bin/env python3 # Ruokalistaparseri # Copyright (c) 2016 Toni Fadjukoff # Based on food.pl by # Copyright (c) 2007-2010 Timo Sirainen # 2011-2016 Toni Fadjukoff # This is Public Domain import sys day_names = [ "Maanantai", "Tiistai", "Keskiviikko", "Torstai", "Perjantai", "Lauantai", "Sunnuntai" ] import amica import sodexo import juvenes import campusravita allergies = [ "M", "L", "VL", "G", "K", "Ve" ] allergy_descriptions = { "M": "Maidoton", "L": "Laktoositon","VL": "Vähälaktoosinen", "G": "Gluteiiniton", "K": "Kasvis", "Ve": "Vegaani" } import os import time import datetime import re global_prefix = ""; use_old = True; # 1 is good for testing, 0 for production system! unordered = [] l = time.localtime() this_week = datetime.datetime.now().isocalendar()[1] unordered += amica.get_restaurants(use_old, this_week) unordered += juvenes.get_restaurants(use_old, this_week) unordered += sodexo.get_restaurants(use_old, this_week) unordered += campusravita.get_restaurants(use_old, this_week) max_week = 0; for r in unordered: week = r[2] max_week = week if week > max_week or week == 1 else max_week if l[6] != 0 and this_week != max_week: # it's not sunday, don't force next week's menu yet max_week = this_week stamp = time.time() - 3600*24*7 max_week_daterange = "" if max_week >= 1 and max_week <= 52: # figure out the date range while True: stamp_week = int(time.strftime("%W", time.localtime(stamp))) if stamp_week == max_week: break stamp += 3600*24 l1 = time.localtime(stamp) l2 = time.localtime(stamp + 3600*24*6) if l1[1] == l2[1]: # same month max_week_daterange = "{monday}-{sunday}.{month}.".format(monday=l1[2], sunday=l2[2], month=l1[1]) else: # different months max_week_daterange = "{monday}.{month}.-{sunday}.{next_month}.".format(monday=l1[2], month=l1[1], sunday=l2[2], next_month=l2[1]) max_week_daterange = " (" + str(max_week_daterange) + ")" file_header = ''' Ruokalistat
PNA.fi on kolmannen osapuolen tarjoama palvelu. En voi taata ruokalistojen oikeellisuutta. Virallisen ruokalistan saat näkyviin siirtymällä ravintolan omille sivuille painamalla sen nimestä. Jos huomaat ruokalistassa virheen, nopeiten virhe saadaan pois näkyvistä kun lähetät minulle siitä sähköpostia: lamperi+pna@gmail.com
''' file_footer = """
""".format(stamp=time.strftime("%d.%m.%Y %H:%M:%S")) def find_last_day_with_foods(restaurants): last_day = 0 for r in restaurants: for day in range(7): if day in r[3]: last_day = day if day > last_day else last_day return last_day def write_days_header(fout, day, last_day): fout.write( " ") for i in range(last_day): if i == day: fout.write("{0} ".format(day_names[i])) else: fout.write("{1} ".format(i+1, day_names[i])) if day < 0: fout.write("Taulukko") else: fout.write("Taulukko") fout.write("\n") def write_prefix_header(fout, prefix, day, resources_prefix): day = "table" if day == 0 else day fout.write("") if prefix == "": fout.write("Kaikki ") else: fout.write("Kaikki ".format(resources_prefix=resources_prefix, day=day)) if prefix == "tay/": fout.write("TaY ") else: fout.write("TaY ".format(resources_prefix=resources_prefix, day=day)) if prefix == "tays/": fout.write("TAYS ") else: fout.write("TAYS ".format(resources_prefix=resources_prefix, day=day)) if prefix == "tty/": fout.write("TTY ") else: fout.write("TTY ".format(resources_prefix=resources_prefix, day=day)) fout.write("\n") def write_day(day, header, outfname, last_day, restaurants, prefix, resources_prefix): with open(outfname, "w", encoding="utf-8") as fout: fout.write(file_header.format(resources_prefix=resources_prefix)) fout.write("

{header}

\n".format(header=header)) # print weekday links fout.write("
\n") write_days_header(fout, day, last_day) fout.write(" Näytä: ") for a in allergies: fout.write("".format(a=a)) fout.write("{a}".format(allergy_description=allergy_descriptions[a], a=a)) fout.write("\n") write_prefix_header(fout, prefix, day+1, resources_prefix); fout.write("
\n") # print foods foodnum = 0 eatable_food_numbers = {} maybe_eatable_food_numbers = {} for a in allergies: eatable_food_numbers[a] = [] maybe_eatable_food_numbers[a] = [] css_class = "left" fout.write("
\n".format(css_class=css_class)) for r in restaurants: title, open_hours, week, week_foods, info = r title2, url, lazy_allergies, info_class = info[0:4] if day in week_foods or day < 5: if info_class != css_class: css_class = info_class fout.write("
\n".format(css_class=css_class)) url = url.replace("&", "&") fout.write("

{title}

\n".format(url=url, title=title)) if not day in week_foods: fout.write("

Ruokalistaa ei saatavilla.

") continue if week != "" and week != max_week: if week > max_week or (week == 1 and max_week == 52): # early.. fout.write("

Viikon {week} ruokalista:

".format(week=week)) else: fout.write("

Saatavilla vain viikon {week} ruokalista.

".format(week=week)) continue if len(week_foods[day]) == 0: fout.write("

Ei ruokatietoja päivälle.

") continue fout.write("\n") # write allergy scripts fout.write(''.format(resources_prefix=resources_prefix)) fout.write('\n") fout.write("
{file_footer}".format(file_footer=file_footer.format(resources_prefix=resources_prefix))) def write_all_days(restaurants, prefix, title, resources_prefix): try: os.mkdir(prefix) except OSError as err: pass # hope it already exists last_day = find_last_day_with_foods(restaurants); for day in range(7): outfname = "{prefix}{day}.html".format(prefix=prefix, day=day+1) if day > last_day: try: os.unlink(outfname) except OSError as err: pass # probably did not exist continue header = "{day_name} - {title} vko {max_week}{max_week_daterange}".format(day_name=day_names[day], title=title, max_week=max_week, max_week_daterange=max_week_daterange) write_day(day, header, outfname, last_day, restaurants, prefix, resources_prefix) def write_table(restaurants, prefix, title, resources_prefix): last_day = find_last_day_with_foods(restaurants); outfname = "{prefix}table.html".format(prefix=prefix) with open(outfname, "w", encoding="utf-8") as fout: header = "{title} vko {max_week}{max_week_daterange}".format(title=title, max_week=max_week, max_week_daterange=max_week_daterange) fout.write(file_header.format(resources_prefix=resources_prefix)) fout.write("

{header}

\n".format(header=header)) fout.write("
\n") write_days_header(fout, -1, last_day) write_prefix_header(fout, prefix, 0, resources_prefix) fout.write("
") for r in restaurants: (title, open_hours, week, week_foods, info) = r (title2, url) = info[0:2] url = re.sub("&", " ", url) fout.write("".format(url=url, title=title)) fout.write("\n") for day in range(last_day): fout.write("\n".format(day_name=day_names[day])) for r in restaurants: (title, open_hours, week, week_foods, info) = r if day in week_foods and (week == "" or week == max_week): fout.write("\n") else: fout.write("\n") fout.write("{file_footer}".format(file_footer=file_footer.format(resources_prefix=resources_prefix))) def get_restaurants_sorted(restaurants): # consider writing comparator out = [] for r in restaurants: if r[4][3] == "left": out.append(r) for r in restaurants: if r[4][3] == "right": out.append(r) for r in restaurants: if r[4][3] == "middle" and not re.search("TAMK", r[4][0]): out.append(r) for r in restaurants: if r[4][3] == "middle" and re.search("TAMK", r[4][0]): out.append(r) return out def get_restaurants_with_prefix(prefix, restaurants): out = [] for r in restaurants: if re.search(prefix, r[4][0]): out.append(r) return get_restaurants_sorted(out) tty_title = "TTY:n ruokalistat" tty = get_restaurants_with_prefix("TTY", unordered) write_all_days(tty, "tty/", tty_title, "../") write_table(tty, "tty/", tty_title, "../") tay_title = "Tampereen yliopiston ruokalistat" tay = get_restaurants_with_prefix("TaY", unordered) write_all_days(tay, "tay/", tay_title, "../") write_table(tay, "tay/", tay_title, "../") tays_title = "TAYS:n ruokalistat" tays = get_restaurants_with_prefix("TAYS", unordered) write_all_days(tays, "tays/", tays_title, "../") write_table(tays, "tays/", tays_title, "../") for r in unordered: if re.search(r"^\(TaY\)", r[0]): r[4][3] = "left" if re.search(r"^\(TTY\)", r[0]): r[4][3] = "right" if re.search(r"^\(TAYS\)", r[0]): r[4][3] = "middle" all_title = "Tampereen yliopistojen ruokalistat"; all_restaurants = get_restaurants_sorted(unordered); # move fusion kitchen last #fusion = splice(@all_restaurants, 1, 1); #splice(@all_restaurants, 4, 0, @fusion); write_all_days(all_restaurants, "", all_title, ""); write_table(all_restaurants, "", all_title, "");
Päivä{title}
{day_name}
    \n") for food in week_foods[day]: fout.write("
  • {food}
  • ".format(food=food)) fout.write("