A simple python code accessing the XML interface of the menu plan from SWFR (Studierendenwerk Freiburg-Schwarzwald).
More informations you can find here.
The code is written in such a way that it can be used in both Python 2.7 and Python 3.x. The following must be installed via pip before use.
Python 2.7:
python -m pip install requests
python -m pip install xmltodict
Python 3:
python3 -m pip install requests
python3 -m pip install xmltodict
If you want to access the XML interace you need an API-Key which you can request at SWFR. The baseurl is:
https://www.swfr.de/apispeiseplan?&type=98&tx_speiseplan_pi1[apiKey]={YOUR_API_KEY}
If you want to retrieve data from a specific location you have to add following to the baseurl:
&tx_speiseplan_pi1[ort]={LOCATION_ID}
| Location | ID |
|---|---|
| Mensa RempartstraĂźe | 610 |
| Mensa Institutsviertel | 620 |
| Mensa Littenweiler | 630 |
| Mensa Furtwangen | 641 |
| Mensa Offenburg | 651 |
| Mensa Gengenbach | 652 |
| Mensa Kehl | 661 |
| Mensa Schwenningen | 671 |
| Mensa Lörrach | 677 |
| Mensa Flugplatz | 681 |
| MusiKantine | 722 |
| Haus zur Lieben Hand | 776 |
| Otto-Hahn-Gymnasium Furtwangen | 9012 |
If you do not specify how many days you would like to receive the information, you will receive the information for the next 6 days (including today). If you want to limit the days displayed, you can add the following to the url:
&tx_speiseplan_pi1[tage]={DAYS}
| Days | Meaning |
|---|---|
| today + the next 5 days | |
| None | today + the next 5 days |
| 0 | today + the next 5 days |
| 1 | today |
| 2 | today + tomorrow |
| 3 | today + the next 2 days |
| 4 | today + the next 3 days |
| 5 | today + the next 4 days |
| 6 | today + the next 5 days |
| n > 6 | today + the next 5 days |
Please notice:
- Sunday is fundamentally ignored.
- Today is always taken as the starting point. So 1 will only stand for Monday on a Monday and not always for Monday.
- A week break is made, minus Sundays.
- Days on which the canteen is closed are ignored. If, for example, there are public holidays and a long weekend within the next few days, fewer data records will be returned. During semester or school holidays, this can mean that no data records are returned at all.
A little fact in passing as to why we use i18n in computer science. In the English word internationalisation, there are 18 letters between the i and the n.
The interface offers the option of requesting the data in English. However, the interface only offers English or German. To access the English API, the baseurl changes as follows:
https://www.swfr.de/en/apispeiseplan?&type=98&tx_speiseplan_pi1[apiKey]={YOUR_API_KEY}
This Python code serves as a wrapper for the menu plan API of Studierendenwerke Freiburg-Schwarzwald (SWFR). It makes it possible to retrieve menus for various canteens and extract information on menus, prices, allergens and more.
Please replace <YOUR_API_KEY> with your own api key.
from menu_plan import MenuPlan
# create instance of MenuPlan
menu_plan_instance = MenuPlan(api_key="<YOUR_API_KEY>")
# Example tests
location_id = 641
location_name = "Mensa Furtwangen"
menu_type = "Essen 1"
schedule_date = "27.02.2024"
# function tests
menu_plan_instance.set_location_id(location_id)
print("Location Name by ID:", menu_plan_instance.get_location_name_by_id(location_id))
menu_plan_instance.set_location_id_by_location_name(location_name)
print("Location ID by Name:", menu_plan_instance.get_location_id())
xml_data = menu_plan_instance.get_menu_plan_from_xml_interface(location_id)
print(xml_data)
menu_data = menu_plan_instance.get_menu_from_menu_plan(location_id, menu_type, schedule_date)
print("\nMenu Data:")
print(menu_data)
# ... (further code)
You could create one or multiple instances like this:
menu_plan_instance = MenuPlan(api_key, location_id=None, days=None, i18n=False)
api_key: Your personal API key for accessing the menu plan API.location_id: The ID of the dining hall for which information is to be retrieved (optional).days: The number of days for which the menu plan is to be retrieved (optional).i18n: Indicates whether the internationalized version of the menu plan should be used (optional).
⚠️ The functions were documented ki-generated.: Because I was too lazy to write detailed documentation for such a simple programme, I had an AI do it for me. I cannot guarantee for possible errors!
Sets the location ID for the dining hall to be retrieved.
location_id: The ID of the dining hall.
None
This method sets the location ID for the dining hall to be retrieved.
Returns the currently set location ID.
None
location_id: The ID of the currently set dining hall.
This method returns the currently set location ID.
Sets the location ID based on the dining hall name.
location_name: The name of the dining hall.
None
This method sets the location ID based on the dining hall name.
Sets the number of days for the menu plan.
days: The number of days.
None
This method sets the number of days for the menu plan.
Returns the currently set number of days.
None
days: The number of days.
This method returns the currently set number of days.
Enables or disables the international version of the menu plan.
i18n: A boolean indicating whether the international version should be enabled.
None
This method enables or disables the international version of the menu plan.
Returns whether the international version of the menu plan is enabled.
None
i18n: A boolean indicating whether the international version is enabled.
This method returns whether the international version of the menu plan is enabled.
Sets the API key for the menu plan.
api_key: Your personal API key.
None
This method sets the API key for accessing the menu plan API.
Returns the set API key.
None
api_key: Your personal API key.
This method returns the set API key.
Retrieves the menu plan as XML from the API.
location_id: The ID of the dining hall.days: The number of days.i18n: A boolean indicating whether the international version should be used.
xml_data: The menu plan data in XML format.
This method retrieves the menu plan as XML from the API.
get_menu_from_menu_plan(location=None, menu_type="Essen 1", schedule_date=datetime.now().strftime("%d.%m.%Y"))
Retrieves information about a specific menu.
location: The location (ID or name) for which to retrieve the menu.menu_type: The type of menu (default is "Essen 1").schedule_date: The date for which to retrieve the menu (default is today).
menu_data: Information about the specified menu.
This method retrieves information about a specific menu.
There are many more methods in the MenuPlan class that retrieve information about prices, allergens, additives, etc.
Note: Please replace <YOUR_API_KEY> with your actual API key.
⚠️ The functions were documented ki-generated.: Because I was too lazy to write detailed documentation for such a simple programme, I had an AI do it for me. I cannot guarantee for possible errors!
Retrieves a dictionary of available menu types for each dining hall.
None
menu_types: A dictionary mapping dining hall IDs to a list of available menu types.
This method retrieves a dictionary of available menu types for each dining hall.
Retrieves the available menu types for a specific dining hall.
id_or_name: The ID or name of the dining hall.
menu_types: A list of available menu types for the specified dining hall.
This method retrieves the available menu types for a specific dining hall.
Retrieves the available menu types for a specific dining hall by ID.
id: The ID of the dining hall.
menu_types: A list of available menu types for the specified dining hall.
This method retrieves the available menu types for a specific dining hall by ID.
Retrieves a list of customer types.
None
customer_types: A list of customer types.
This method retrieves a list of customer types, such as "studierende," "angestellte," "gaeste," and "schueler."
Retrieves the available menu types for a specific dining hall by name.
name: The name of the dining hall.
menu_types: A list of available menu types for the specified dining hall.
This method retrieves the available menu types for a specific dining hall by name.
Sets the XML data for the menu plan.
xml_string: The XML data as a string.
None
This method sets the XML data for the menu plan.
Retrieves the currently set XML data.
None
xml_data: The currently set XML data.
This method retrieves the currently set XML data.
Retrieves the location ID for a dining hall by name.
location_name: The name of the dining hall.
location_id: The ID of the specified dining hall.
This method retrieves the location ID for a dining hall by name.
Retrieves the name of a dining hall by ID.
location_id: The ID of the dining hall.
location_name: The name of the specified dining hall.
This method retrieves the name of a dining hall by ID.
Retrieves a dictionary of available dining hall IDs and names.
None
locations: A dictionary mapping dining hall IDs to names.
This method retrieves a dictionary of available dining hall IDs and names.
Sets the location ID for a dining hall.
location_id: The ID of the dining hall.
None
This method sets the location ID for a dining hall.
Sets the location ID based on the dining hall name.
location_name: The name of the dining hall.
None
This method sets the location ID based on the dining hall name.
Retrieves the menu plan as XML data.
location: The location (ID or name) for which to retrieve the menu plan.
xml_data: The menu plan data in XML format.
This method retrieves the menu plan as XML data.
Retrieves the menu plan as XML data for a specific dining hall by ID.
location_id: The ID of the dining hall.
xml_data: The menu plan data in XML format.
This method retrieves the menu plan as XML data for a specific dining hall by ID.
Retrieves the menu plan as XML data for a specific dining hall by name.
location_name: The name of the dining hall.
xml_data: The menu plan data in XML format.
This method retrieves the menu plan as XML data for a specific dining hall by name.
get_menu_from_menu_plan_by_name(location_name, menu_type="Essen 1", schedule_date=datetime.now().strftime("%d.%m.%Y"))
Retrieves information about a specific menu by dining hall name.
location_name: The name of the dining hall.menu_type: The type of menu (default is "Essen 1").schedule_date: The date for which to retrieve the menu (default is today).
menu_data: Information about the specified menu.
This method retrieves information about a specific menu by dining hall name.
get_menu_from_menu_plan_by_location_id(location_id, menu_type="Essen 1", schedule_date=datetime.now().strftime("%d.%m.%Y"))
Retrieves information about a specific menu by dining hall ID.
location_id: The ID of the dining hall.menu_type: The type of menu (default is "Essen 1").schedule_date: The date for which to retrieve the menu (default is today).
menu_data: Information about the specified menu.
This method retrieves information about a specific menu by dining hall ID.
`get_menu_from_menu_plan_by_location(location, menu_type="Essen 1", schedule_date=datetime.now().strftime("%d.%
m.%Y"))`
Retrieves information about a specific menu by dining hall.
location: The location (ID or name) for which to retrieve the menu.menu_type: The type of menu (default is "Essen 1").schedule_date: The date for which to retrieve the menu (default is today).
menu_data: Information about the specified menu.
This method retrieves information about a specific menu by dining hall.
get_menus_from_menu_plan(location=None, menu_types=["Essen 1"], schedule_date=datetime.now().strftime("%d.%m.%Y"))
Retrieves information about multiple menus by dining hall.
location: The location (ID or name) for which to retrieve the menus.menu_types: A list of menu types to retrieve (default is ["Essen 1"]).schedule_date: The date for which to retrieve the menus (default is today).
result_menus: Information about the specified menus.
This method retrieves information about multiple menus by dining hall.
Retrieves the prices of menus.
menus: A list of menus.
prices: A dictionary mapping menu types to prices.
This method retrieves the prices of menus.
Retrieves the prices of menus for a specific customer type.
menus: A list of menus.customer_type: The customer type for which to retrieve prices (default is "studierende").
prices: A dictionary mapping menu types to prices.
This method retrieves the prices of menus for a specific customer type.
Retrieves the prices of menus for pupils.
menu: A menu.
prices: A dictionary mapping menu types to prices.
This method retrieves the prices of menus for pupils.
Retrieves the prices of menus for students.
menu: A menu.
prices: A dictionary mapping menu types to prices.
This method retrieves the prices of menus for students.
Retrieves the prices of menus for employees.
menu: A menu.
prices: A dictionary mapping menu types to prices.
This method retrieves the prices of menus for employees.
Retrieves the prices of menus for guests.
menu: A menu.
prices: A dictionary mapping menu types to prices.
This method retrieves the prices of menus for guests.
Retrieves the price of a specific menu.
menu: A menu.
price: The price of the specified menu.
This method retrieves the price of a specific menu.
Retrieves the price of a specific menu for a specific customer type.
menu: A menu.customer_type: The customer type for which to retrieve the price (default is "studierende").
price: The price of the specified menu for the specified customer type.
This method retrieves the price of a specific menu for a specific customer type.
Retrieves the price of a specific menu for pupils.
menu: A menu.
price: The price of the specified menu for pupils.
This method retrieves the price of a specific menu for pupils.
Retrieves the price of a specific menu for students.
menu: A menu.
price: The price of the specified menu for students.
This method retrieves the price of a specific menu for students.
Retrieves the price of a specific menu for employees.
menu: A menu.
price: The price of the specified menu for employees.
This method retrieves the price of a specific menu for employees.
Retrieves the price of a specific menu for guests.
menu: A menu.
price: The price of the specified menu for guests.
This method retrieves the price of a specific menu for guests.
Converts text (str) to a dictionary.
text(str): Text containing e.g. additives or allergens information.
dict: A dictionary containing e.g. additives or allergens information.
This method converts text to a dictionary by removing spaces, splitting the text into key-value pairs, and creating a dictionary from them.
Extracts keys from a dictionary and returns them as a comma-separated string.
dictionary(dict): The input dictionary.
str: A string containing keys separated by commas.
This method takes a dictionary as input and extracts its keys, returning them as a comma-separated string.
Extracts values from a dictionary and returns them as a comma-separated string.
dictionary(dict): The input dictionary.
str: A string containing values separated by commas.
This method takes a dictionary as input and extracts its values, returning them as a comma-separated string.
Retrieves additives information from a menu.
menu: A menu.
additives_dict: A dictionary containing additives information.
This method retrieves additives information from a menu.
Retrieves allergens information from a menu.
menu: A menu.
allergens_dict: A dictionary containing allergens information.
This method retrieves allergens information from a menu.
, schedule_date=datetime.now().strftime("%d.%m.%Y"))`
Retrieves the daily schedule for a specific dining hall.
location: The location (ID or name) for which to retrieve the daily schedule.schedule_date: The date for which to retrieve the daily schedule (default is today).
schedule_data: Information about the daily schedule.
This method retrieves the daily schedule for a specific dining hall.
Retrieves multiple daily schedules for a specific dining hall.
location: The location (ID or name) for which to retrieve the daily schedules.start_date: The starting date for retrieving schedules (default is today).num_days: The number of days for which to retrieve schedules (default is 6).
schedule_plans: Information about the daily schedules.
This method retrieves multiple daily schedules for a specific dining hall.