From 6672aad7101f3593fde1009c36d0dde92ebc6512 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20P=C3=A9rez-Cerezo?= Date: Wed, 4 Oct 2017 15:25:10 +0200 Subject: Fixed backends, make run script configurable --- backends/mathemensa.py | 29 +++++++++++++++++------------ backends/satyam.py | 2 +- backends/singh.py | 4 +++- backends/studentenwerk.py | 6 +++--- base.py | 8 +++++++- run.py | 38 ++++++++++++++++++++++++++++++++++++-- 6 files changed, 67 insertions(+), 20 deletions(-) diff --git a/backends/mathemensa.py b/backends/mathemensa.py index b98ab1d..ea0cf72 100644 --- a/backends/mathemensa.py +++ b/backends/mathemensa.py @@ -16,7 +16,8 @@ class Mathemensa(IPlugin) : r = Restaurant("Mathemensa", "Mathemensa", self, "dummy") register_restaurant(r) - def get_food_items(self) : + def get_food_items(self, **kwargs) : + weekdays = ["MONTAG", "DIENSTAG", "MITTWOCH", "DONNERSTAG", "FREITAG"] weekday = datetime.datetime.today().weekday() if weekday > 4 : print("Error: No food today") @@ -30,15 +31,19 @@ class Mathemensa(IPlugin) : document = html5lib.parse(the_page, treebuilder="lxml") sel = CSSSelector('.Menu__accordion') fl = [] - for k in sel(document)[0][weekday] : - if k.tag.endswith("ul") : - for j in k : - price = j[1].text - st = str(etree.tostring(j)).split("\\n")[2].split("<")[0] - name = j[0].text + ", " + etree.fromstring("

%s

"%st).text.strip() # really extremely dirty hack - veg = 0 - - if "(v)" in name or "Gemüseplatte" in name : - veg = 1 - fl.append(Food(name, price, "Menü", veg)) + for i in sel(document)[0] : + h2sel = CSSSelector('h2') + if not weekdays[weekday] in i[0].text.upper() : + continue + for k in i : + if k.tag.endswith("ul") : + for j in k : + price = j[1].text + st = str(etree.tostring(j)).split("\\n")[2].split("<")[0] + name = j[0].text + ", " + etree.fromstring("

%s

"%st).text.strip() # really extremely dirty hack + veg = 0 + + if "(v)" in name or "Gemüseplatte" in name : + veg = 1 + fl.append(Food(name, price, "Menü", veg)) return fl diff --git a/backends/satyam.py b/backends/satyam.py index a20d6bb..ec488f3 100644 --- a/backends/satyam.py +++ b/backends/satyam.py @@ -11,7 +11,7 @@ class Satyam(IPlugin): def register_restaurants(self) : r = Restaurant("Satyam", "Satyam", self, "dummy") register_restaurant(r) - def get_food_items(self) : + def get_food_items(self, **kwargs) : s = sys.stderr sys.stderr = open("/dev/null", "w") user_agent = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' diff --git a/backends/singh.py b/backends/singh.py index a3b3119..ae8b291 100644 --- a/backends/singh.py +++ b/backends/singh.py @@ -12,7 +12,8 @@ class Signh(IPlugin) : def register_restaurants(self) : r = Restaurant("Singh", "Mathe-Café", self, "dummy") register_restaurant(r) - def get_food_items(self) : + def get_food_items(self, **kwargs) : + s = sys.stderr sys.stderr = open("/dev/null", "w") weekday = datetime.datetime.today().weekday() if weekday > 4 : @@ -46,6 +47,7 @@ class Signh(IPlugin) : elif "VEGAN" in vegsel(k)[0].text : veg = 2 fl.append(Food(name, price, "Essen", veg, desc)) + sys.stderr = s return fl if __name__ == "__main__": diff --git a/backends/studentenwerk.py b/backends/studentenwerk.py index ce6d2fd..48b15d3 100644 --- a/backends/studentenwerk.py +++ b/backends/studentenwerk.py @@ -12,7 +12,7 @@ import multiprocessing from yapsy import NormalizePluginNameForModuleName as normalize -mensenliste = {"TU Hardenberg" : "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"} +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"} @@ -23,7 +23,7 @@ def pr_f(j) : class Studentenwerk(IPlugin) : def register_restaurants (self) : - mensenliste = {"TU Hardenberg" : "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"} + 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) @@ -48,7 +48,7 @@ class Studentenwerk(IPlugin) : for m in meals : namesel = CSSSelector('.bold') nm = namesel(m)[0].text - if ignore_nudelauswahl and "Nudelauswahl" in nm: + if ignore_nudelauswahl and "Nudelauswahl" in nm : continue pricesel = CSSSelector('.col-md-3') veg = 0 diff --git a/base.py b/base.py index 2660607..df7bd4a 100644 --- a/base.py +++ b/base.py @@ -11,6 +11,13 @@ class Food : class NoMenuError(Exception) : """ gets raised if there's no menu""" + +class Frontend(object) : + def __init__(self, name, human_name, description="", optional_args=[]) : + self.name = name + self.human_name = human_name + self.description = description + self.optional_args = [] def formt (food) : cat = [] @@ -40,7 +47,6 @@ class Restaurant(object): def get_food(self,**opt_args) : return self.module.get_food_items(*self.obligatory_args, **opt_args) - def register_restaurant(restaurant): global foodsources foodsources[restaurant.name] = (restaurant) diff --git a/run.py b/run.py index 7ea99fe..3ce971f 100755 --- a/run.py +++ b/run.py @@ -1,16 +1,50 @@ #!/usr/bin/python3 + +import base + +import argparse + +parser = argparse.ArgumentParser(description='Fetch menus from various sources') +# parser.add_argument('integers', metavar='N', type=int, nargs='+', +# help='an integer for the accumulator') +parser.add_argument('-r', '--restaurants', dest='rest', action='store', + metavar='LIST', + help='Comma-separated list of restaurants to fetch the menus from.') +parser.add_argument('-l', '--list-restaurants', dest='list', action='store_true', + help='get list of restaurants') + + +args = parser.parse_args() +## Load backends from yapsy.PluginManager import PluginManager backends = PluginManager() backends.setPluginPlaces(["./backends"]) backends.collectPlugins() -import base +## Load frontends (not yet implemented) +# from yapsy.PluginManager import PluginManager +# frontends = PluginManager() +# frontends.setPluginPlaces(["./frontends"]) +# frontends.collectPlugins() + for pluginInfo in backends.getAllPlugins(): backends.activatePluginByName(pluginInfo.name) pluginInfo.plugin_object.register_restaurants() +if args.list : + for k,i in base.foodsources.items(): + print(i.name, i.human_name) + exit() +restlist = None +if args.rest : + restlist = args.rest.split(",") + for k,i in base.foodsources.items() : + if restlist and not i.name in restlist : + continue try : - food = i.get_food() + food = i.get_food(ignore_nudelauswahl=True) 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)) -- cgit v1.2.3