diff options
Diffstat (limited to 'mensa')
-rw-r--r-- | mensa/backends/studentenwerk.py | 35 | ||||
-rw-r--r-- | mensa/frontends/html.py | 33 | ||||
-rw-r--r-- | mensa/frontends/html.yapsy-plugin | 3 | ||||
-rw-r--r-- | mensa/frontends/plain-text.py | 4 | ||||
-rwxr-xr-x | mensa/logic.py | 68 |
5 files changed, 110 insertions, 33 deletions
diff --git a/mensa/backends/studentenwerk.py b/mensa/backends/studentenwerk.py index a8f6c2b..f916096 100644 --- a/mensa/backends/studentenwerk.py +++ b/mensa/backends/studentenwerk.py @@ -8,33 +8,28 @@ import html5lib from mensa.base import * from yapsy.IPlugin import IPlugin import multiprocessing - +import datetime from yapsy import NormalizePluginNameForModuleName as normalize -mensenliste = {"TU Hardenbergstraße" : "mensa-tu-hardenbergstra%C3%9Fe", "TU Marchstraße": "cafeteria-tu-marchstra%C3%9Fe", "TU Skyline": "cafeteria-tu-skyline", "TU Architektur": "cafeteria-tu-architektur", "TU Ackerstraße": "cafeteria-tu-ackerstra%C3%9Fe"} - - - -def pr_f(j) : - i,k = j - food = get_food_items(k, ignore_nudelauswahl=True) - return (i,"*"*20+i+"*"*20+"\n"+formt(food)) - class Studentenwerk(IPlugin) : - def register_restaurants (self) : - mensenliste = {"TU Hardenbergstraße" : "mensa-tu-hardenbergstra%C3%9Fe", "TU Marchstraße": "cafeteria-tu-marchstra%C3%9Fe", "TU Skyline": "cafeteria-tu-skyline", "TU Architektur": "cafeteria-tu-architektur", "TU Ackerstraße": "cafeteria-tu-ackerstra%C3%9Fe"} - for h,n in mensenliste.items() : - r = Restaurant(normalize(h), h, self, "dummy", [n]) - register_restaurant(r) - def get_food_items(self, mensa="mensa-tu-hardenbergstra%C3%9Fe", ignore_nudelauswahl=False) : + def fetch_page(self, mensa) : user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' headers = {'User-Agent': user_agent} req = urllib.request.Request('https://www.stw.berlin/mensen/%s.html' % mensa, headers=headers) response = urllib.request.urlopen(req) the_page = response.read() + open("/tmp/the_page","w").write(str(the_page)) document = html5lib.parse(the_page, treebuilder="lxml") + return document + def register_restaurants (self) : + mensenliste = {"TU Hardenbergstraße" : "mensa-tu-hardenbergstra%C3%9Fe", "TU Marchstraße": "cafeteria-tu-marchstra%C3%9Fe", "TU Skyline": "cafeteria-tu-skyline", "TU Architektur": "cafeteria-tu-architektur", "TU Ackerstraße": "cafeteria-tu-ackerstra%C3%9Fe"} + for h,n in mensenliste.items() : + r = Restaurant(normalize(h), h, self, "dummy", [n]) + register_restaurant(r) + def get_food_items(self, mensa="mensa-tu-hardenbergstra%C3%9Fe", ignore_nudelauswahl=False) : + document = self.fetch_page(mensa) groupsel = CSSSelector('.splGroupWrapper') groups = [e for e in groupsel(document)] fl = [] @@ -60,7 +55,13 @@ class Studentenwerk(IPlugin) : price = pricesel(m)[-1].text.strip() fl.append(Food(nm, price, name, veg)) return fl - + def get_opening_hours(self, mensa) : + #### Rudiment of a function for getting opening hours. Does NOT work yet due to unknown issues. + doc = self.fetch_page(mensa) + groupsel = CSSSelector('div.col-xs-10') + groups = [e for e in groupsel(doc)] + print(groups) + return doc # format: diff --git a/mensa/frontends/html.py b/mensa/frontends/html.py new file mode 100644 index 0000000..8ffec37 --- /dev/null +++ b/mensa/frontends/html.py @@ -0,0 +1,33 @@ +from mensa import base +from yapsy.IPlugin import IPlugin +from xml.sax.saxutils import escape as esc + +class HTMLRenderer(IPlugin) : + def render (self, foods, **options) : + ## Expects list of tuples with (Restaurant, Foodlist) + r = "" + vegkeys = [ "", "Vegetarian", "Vegan" ] + for restaurant, food in foods : + cat = [] + if not food : + continue + r = r+"<div class=\"restaurant\"><h3>"+esc(restaurant.human_name)+"</h3>"+"\n"#+base.formt(food) + food.sort(key=lambda foo: foo.category) + for i in food: + if options["only_veggie"] and options["only_veggie"] > i.veggie : + continue + if not i.category in cat : + if cat : + r = r + "</ul>" + cat.append(i.category) + if not i.category == None : + r=r+ "<h4>"+esc(i.category)+"</h4><ul class=\"food-by-cat\">\n" + r=r+"<li class=\"fooditem\" ><span class=\"name\">" + esc(i.name) + "</span><span class=\"price\">"+ esc(i.price) + "</span><span class=\"veggie\">"+ esc(vegkeys[i.veggie])+"</span>\n" + if i.desc : + r = r+"<div class=\"description\">"+esc(i.desc)+"</div>\n" + r = r+"</div>" + r = r + print(r) + + def register_renderer(self) : + base.register_renderer(base.Renderer("html", "HTML Renderer", self)) diff --git a/mensa/frontends/html.yapsy-plugin b/mensa/frontends/html.yapsy-plugin new file mode 100644 index 0000000..135ad17 --- /dev/null +++ b/mensa/frontends/html.yapsy-plugin @@ -0,0 +1,3 @@ +[Core] +Name = HTML Output +Module = html
\ No newline at end of file diff --git a/mensa/frontends/plain-text.py b/mensa/frontends/plain-text.py index 501be54..7a31371 100644 --- a/mensa/frontends/plain-text.py +++ b/mensa/frontends/plain-text.py @@ -7,9 +7,13 @@ class TextRenderer(IPlugin) : vegkeys = [ "", "Vegetarian", "Vegan" ] for restaurant, food in foods : cat = [] + if not food : + continue r = r+"*"*20+restaurant.human_name+"*"*20+"\n"#+base.formt(food) food.sort(key=lambda foo: foo.category) for i in food: + if options["only_veggie"] and options["only_veggie"] > i.veggie : + continue if not i.category in cat : cat.append(i.category) if not i.category == None : diff --git a/mensa/logic.py b/mensa/logic.py index f14b89c..75b2465 100755 --- a/mensa/logic.py +++ b/mensa/logic.py @@ -3,7 +3,13 @@ from mensa import base import urllib.error import os +import sys from yapsy.PluginManager import PluginManager +try: + import multiprocessing + parallel=True +except: + parallel = False def init_foodsources(): backends = PluginManager() @@ -22,25 +28,55 @@ def init_renderers(): pluginInfo.plugin_object.register_renderer() -def get_food(restlist=False, options={}) : +def get_food(restlist=False, **options) : foodl = [] - for k,i in base.foodsources.items() : - if restlist and not i.name in restlist : - continue - try : - food = i.get_food(ignore_nudelauswahl=True) - foodl.append((i, food)) - # print("*"*20+i.human_name+"*"*20+"\n"+base.formt(food)) - except base.NoMenuError: - print(i.human_name + ": No menu found. This could be due to a holiday or due to an error in the script.") - except urllib.error.HTTPError as e : - print(i.human_name + ": Fetching menu failed: %s" % str(e)) - except : - print(i.human_name + ": Unknown error") + if parallel : + r = [] + for k,i in base.foodsources.items() : + if restlist and not i.name in restlist : + continue + r.append(i) + + foodl = get_food_parallel(r, ignore_nudelauswahl=True) + else : + for k,i in base.foodsources.items() : + if restlist and not i.name in restlist : + continue + try : + food = i.get_food(ignore_nudelauswahl=True) + foodl.append((i, food)) + # print("*"*20+i.human_name+"*"*20+"\n"+base.formt(food)) + except base.NoMenuError: + sys.stderr.write(i.human_name + ": No menu found. This could be due to a holiday or due to an error in the script.\n") + except urllib.error.HTTPError as e : + sys.stderr.write(i.human_name + ": Fetching menu failed: %s\n" % str(e)) + except : + sys.stderr.write(i.human_name + ": Unknown error\n") return foodl -def render(to_render, rendlist=False, options={}) : +# format: +# if __name__ == "__main__": +# pool = multiprocessing.Pool(4) +# k = pool.map(pr_f, list(mensenliste.items())) +# k.sort(key=lambda x: x[0]) +# for i in k : +# print(i[1]) +def get_food_parallel_helper(i) : + try : + return (i[0],i[0].get_food(**i[1])) + except: + sys.stderr.write(i[0].human_name + ": No menu found. This could be due to a holiday or due to an error in the script.\n") + return (i[0], []) + +def get_food_parallel(rl, **options) : + pool = multiprocessing.Pool(60) + k = [ (i, options) for i in rl ] + l = list(pool.map(get_food_parallel_helper, k)) + l.sort(key=lambda x: x[0].human_name) + return l + +def render(to_render, rendlist=False, **options) : for k,i in base.renderers.items() : if rendlist and not i.name in rendlist : continue - i.render(to_render) + i.render(to_render, **options) |