-
Notifications
You must be signed in to change notification settings - Fork 2
/
KAMensa.py
103 lines (93 loc) · 3.13 KB
/
KAMensa.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# -*- coding: utf-8 -*-
#!/usr/bin/python2ö
# coding: utf8
import urllib2
import base64
import json
from datetime import date
class mensaplan:
def __init__(self):
username = "jsonapi"
password = "AhVai6OoCh3Quoo6ji"
theurl = 'https://www.sw-ka.de/json_interface/canteen/'
req = urllib2.Request(theurl)
base64string = base64.encodestring('%s:%s' % (username, password))[:-1]
req.add_header("Authorization", "Basic %s" % base64string)
try:
handle = urllib2.urlopen(req)
self.json_response = json.loads(handle.read())
except IOError, e:
if hasattr(e, 'code'):
if e.code != 401:
print 'We got another error'
print e.code
else:
print e.headers
print e.headers['www-authenticate']
def meal(self, mensa, line, date = date.today()):
timestamp = date.strftime('%s')
if (mensa in self.json_response.keys()
and timestamp in self.json_response[mensa].keys()
and line in self.json_response[mensa][timestamp].keys()):
return self.json_response[mensa][timestamp][line]
else:
return None
def meal_string(self, mensa, line, date = date.today()):
if date.weekday() > 4 :
return "Wochenende"
meals = self.meal(mensa,line,date)
if meals != None :
result = '' + str(key_to_name(line)) + ':'
for item in meals:
if 'nodata' not in item.keys():
result += '\t' + item['meal'] + ' ' + item['dish'] + ' ' + str(item['price_1']) + ' ' + item['info']
else:
result += "\tNo Data"
return result
else:
return "No Data Available"
def keys(self, type):
mensen = { 'adenauerring': ['l1', 'l2', 'l3', 'l45', 'schnitzelbar', 'update', 'abend', 'aktion', 'heisstheke', 'nmtisch'],
'erzberger': ['wahl1', 'wahl2'],
'gottesaue': ['wahl1', 'wahl2'],
'holzgarten': ['gut','gut2'],
'moltke': ['wahl1', 'wahl2', 'aktion', 'gut', 'buffet', 'schnitzelbar'],
'tiefenbronner': ['wahl1', 'wahl2', 'gut', 'buffet']
}
meals = ['add', 'bio', 'cow', 'cow_aw', 'dish', 'fish', 'info', 'meal', 'pork', 'price_1', 'price_2', 'price_3', 'price_4', 'price_flag', 'veg', 'vegan']
if type == 'mensa':
return mensen.keys()
elif type in mensen.keys():
return mensen[type]
elif type == 'meal':
return meals
else:
return 'unknown type'
def available_dates(self, mensa):
return map(lambda x: date.fromtimestamp(int(x)).strftime('%d.%m.%Y'),sorted(self.json_response[mensa].keys()))
## Mapping keys to Names
def key_to_name(key):
name = {"adenauerring":"Mensa am Adenauerring",
"erzberger":"Mensa Erzbergstraße",
"gottesaue":"Mensa Schloss Gottesaue",
"holzgarten":"Mensa Holzgartenstraße",
"moltke":"Mensa Moltke",
"tiefenbronner":"Mensa Tiefenbronner Straße",
"l1":"Linie 1",
"l2":"Linie 2",
"l3":"Linie 3",
"l45":"Linie 4/5",
"schnitzelbar":"Schnitzelbar",
"update":"L6 Update",
"abend":"Abend",
"aktion":"Aktionstheke",
"heisstheke":"Cafeteria Heiße Theke",
"nmtisch":"Cafeteria ab 14:30",
"wahl1":"Wahlessen 1",
"wahl2":"Wahlessen 2",
"aktion":"Aktionstheke",
"gut":"Gut&Guenstig",
"gut2":"Gut&Guenstig 2",
"buffet":"Buffet",
"schnitzelbar":"Schnitzelbar"}.get(key, "unknown key")
return name