-
Notifications
You must be signed in to change notification settings - Fork 5
/
getfood.php
executable file
·494 lines (432 loc) · 17 KB
/
getfood.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
<?php
include("./libs/simple_html_dom.php");
include("./config.php");
$plans = array();
$plansXML = array();
$timestamp = time();
$week = date("W", $timestamp);
$weeksRegistered = array();
$daysRegistered = array();
$json = array();
$json["weeks"]=array();
/*
* These functions are used for the JSON-file to ensure that every week/day we
* find in the pdf sources gets a sequential index beginning with 0 while
* meals at the same day/in the same week are correctly assigned.
*/
/*
* returns index of the value, false if not present
*/
function isWeekRegistered($int){
global $weeksRegistered;
return array_search($int, $weeksRegistered);
}
function registerWeek($int){
global $weeksRegistered;
array_push($weeksRegistered,$int);
}
/*
* returns index of the value, false if not present
*/
function isDayRegistered($date,$week){
global $daysRegistered;
return array_search($date, $daysRegistered[$week]);
}
function registerDay($date,$week){
global $daysRegistered;
array_push($daysRegistered[$week],$date);
}
/*
* Returns, if a menu item is whitelisted. This needs to be done as otherwise
* the fine print would be accepted as menu item.
*/
function whitelisted($fooditem){
$whitelist = array ( 'lecker und fein', 'gut und g', 'pizza',
'pasta', 'schneller teller', 'wok und grill',
'buffet', 'vegetarisch', 'bio', 'eintopf',
'aktion', 'suppe', 'essen', 'woche', 'expedition','ranjid',
'gourmet', 'prima klima');
foreach ( $whitelist as $wlElement )
if ( strpos($fooditem, $wlElement) !== FALSE ) return true;
return false;
}
function filterMeals($meal) {
$meal = str_replace(array("/ Bed."," Gast", "Stud.", " .", " ,", "g =")," ",$meal);
$meal = str_replace(array("MONTAG","DIENSTAG","MITTWOCH","MITTWOC H","MITT WOCH","DONNERSTAG","FREITAG", "FRE ITAG")," ",$meal);
$meal = str_replace(array("Montag","Dienstag","Mittwoch","Mittwoc h","Mitt woch","Donnerstag","Freitag", "Fre itag")," ",$meal);
$meal = str_replace(array("1","2","3","4","5","6","7","8","9","0"," ,","€"," ")," ",$meal);
$meal = str_replace(array("KJ","Kcal","/ / / / /")," ",$meal);
// multiple spaces to one
$meal = preg_replace( '/\s+/', ' ', $meal );
$meal = str_replace(array("KJ","Kcal","/ / / / /")," ",$meal);
$meal = str_replace(array("( )","()"),"",$meal);
return trim(str_replace(" , ",", ",$meal));
}
function filterHTML($str){
$str = str_replace(array(" "," ")," ",$str);
$str = trim($str);
return $str;
}
function filterPrice($str){
$str = str_replace("/"," ",$str);
$str = preg_replace( '/\s+/', ' ', $str );
$str = trim($str);
return $str;
}
function getTextFromNode($xml){
$tmp = $xml->xpath(".//text()");
return (string) $tmp[0];
}
/*
* Gets $attribute of an elements tag.
*/
function getStyleAttribute($attribute, $tag){
$tmp = $tag->xpath("./@".$attribute);
return (string) $tmp[0];
}
/*
* Looks for links and returns array with the addresses for all plans we use
*/
function getPlansURLs(){
$urls = array();
$domain = "https://www.studierendenwerk-ulm.de/";
$url = $domain."/essen-trinken/speiseplaene/";
$site = new simple_html_dom();
$site->load_file($url);
$as = $site->find("a");
foreach ( $as as $a ) {
if ((strpos($a->href,"mensa-uni") !== false ||
strpos($a->href,"uni-bistro") !== false ||
strpos($a->href,"cafeteria-uni-west") !== false ||
strpos($a->href,"cafeteria-b-mensavital") !== false ||
strpos($a->href,"rittwitzstr") !== false ||
strpos($a->href,"UL") !== false ||
strpos($a->href,"Bistro") !== false ||
strpos($a->href,"CB") !== false ||
strpos($a->href,"West") !== false //||
//strpos($a->href,"HL") !== false ||
//strpos($a->href,"OE") !== false
) && (strpos($a->href,".pdf") !== false || strpos($a->href,".PDF") !== false)
)
array_push($urls,$a->href."\n");
}
return $urls;
}
/*
* Returns the position of an element in the table or false if it's outside
* the table.
*/
function getPositionOfElement($element, $posx, $posy, $maxposx, $maxposy,
$rows, $columns){
$top = getStyleAttribute("top",$element);
$left = getStyleAttribute("left",$element);
// if the element is within these borders, it's in the table
if ( $left > $posx && $top > $posy && $left < $maxposx && $top < $maxposy ) {
$position = array();
$position['x'] = 0;
$position['y'] = 0;
// get the table position of the element
for ( ; $position['x'] < sizeof($columns) ; $position['x']++ ){
if ( $left <= $columns[$position['x']] ) {
break;
}
}
for ( ; $position['y'] < sizeof($rows) ; $position['y']++ ){
if ( $top <= $rows[$position['y']] ) {
break;
}
}
$position['x']--; $position['y']--;
if ( $position['x'] < 0 ) $position['x'] = 0;
if ( $position['y'] < 0 ) $position['y'] = 0;
return $position;
} else {
return false;
}
}
/*
* Build the json array we need. If something is changed here, the XML output
* has to be changed, too.
*/
function jsonify($json, $rowsNames, $food, $mealPrice, $columns, $rows,
$place, $week){
$year = date("Y",time());
$timestamp = strtotime($year."W".str_pad($week, 2, '0', STR_PAD_LEFT));
//get the index for the week element
$weekIndex = isWeekRegistered($week);
if ($weekIndex === FALSE){
registerWeek($week);
$weekIndex = isWeekRegistered($week);
global $daysRegistered;
$daysRegistered[$weekIndex] = array();
}
$json["weeks"][$weekIndex]["weekNumber"] = (int) $week;
for ( $i = 0; $i < $columns; $i++ ){
//get index for the day element
$dayIndex = isDayRegistered(date("Y-m-d", $timestamp),$weekIndex);
if($dayIndex === FALSE){
registerDay(date("Y-m-d", $timestamp),$weekIndex);
$dayIndex = isDayRegistered(date("Y-m-d", $timestamp),$weekIndex);
}
$json["weeks"][$weekIndex]["days"][$dayIndex]["date"]=date("Y-m-d", $timestamp);
$k = 0;
$theresSomethingToEatToday=FALSE;
for ( $j = 0; $j < $rows; $j++){
if ( filterMeals($food[$i][$j]) != "" && $rowsNames[$j] != "Salatbuffet"){
$json["weeks"][$weekIndex]["days"][$dayIndex][$place]["meals"][$k] = array();
$json["weeks"][$weekIndex]["days"][$dayIndex][$place]["meals"][$k]["category"]= $rowsNames[$j];
$json["weeks"][$weekIndex]["days"][$dayIndex][$place]["meals"][$k]["meal"]= filterMeals($food[$i][$j]);
$json["weeks"][$weekIndex]["days"][$dayIndex][$place]["meals"][$k]["meal_raw"]=$food[$i][$j];
if ( $mealPrice[$i][$j] != "" ){
$json["weeks"][$weekIndex]["days"][$dayIndex][$place]["meals"][$k]["price"]= filterPrice($mealPrice[$i][$j]);
}
$theresSomethingToEatToday=TRUE;
$k++;
}
}
$json["weeks"][$weekIndex]["days"][$dayIndex][$place]["open"] = $theresSomethingToEatToday;
$timestamp = $timestamp + 24*60*60;
}
return $json;
}
/*
* The real parsing:
*
* ($posx,$posy) is the top left position of the table in pixels
* ($maxposx,$maxposy) is the bottom right position of the table in pixels
* these differ from plan to plan
*
* $timestamp, $week, $place: used for the JSON file
*
* $json: the json construct, new plan gets added at the end.
*
*/
function parsePlan ($json, $posx, $posy, $maxposx, $maxposy,
$week, $url, $place) {
/* some elements are further left / right than the headline element for this
* column or higher / lower than the headline element for this row. The
* buffer exists to account for this.
*/
$buffer = 15;
$days = array("montag","dienstag","mittwoch","donnerstag","freitag");
$elements = array();
$columns = array();
$rows = array();
$rowsNames = array();
$rowPrice = array();
$food = array(array());
$mealPrice = array(array());
$bold = array(array());
// load xml file
$site = simplexml_load_file($url);
if ( $site == "" ) {
echo "plan is broken, continue with next plan\ņ";
return $json;
}
/* get the elements that contain the needed data and ignore the ones that
* are empty
*/
$elements = $site->xpath("//text");
// get positions of rows and columns – building table
foreach ( $elements as $element ){
$top = getStyleAttribute("top",$element);
$left = getStyleAttribute("left",$element);
$text = filterHTML(getTextFromNode($element));
if ( $text == "" ) continue;
// column detection by day
$tmp = $left-$buffer;
// $textWOSpaces = $text w/o spaces because weekdays sometimes happen to
// contain random spaces (e.g. mittw och)
$textWOSpaces = preg_replace( '/\s+/', '', $text );
if (in_array(strtolower(trim($textWOSpaces)),$days)) {
array_push($columns, $tmp);
}
// row detection by whitelisted meal categories
// this doesn't work with Cafeteria B as the meal
// names are pictures. add exceptions for broken plans here
if ( $left < $posx && $top > $posy && $top < $maxposy && $place != "CB" ) {
$tmp = $top-$buffer;
if ( whitelisted(strtolower(filterHTML($text))) ){
array_push($rows, $tmp);
array_push($rowsNames, $text);
} else {
if ( strpos($text,"€") !==false){
// there's a price in the title of the row.
$rowPrice[sizeof($rowsNames)-1]=$text;
} else {
echo "$place: not found: (".$text.") <br/>\n";
}
}
}
// detect end of table, change $maxposy if necessary
if ((strpos(strtolower(filterHTML($text)),"wir verwenden") !== false ||
strpos(strtolower(filterHTML($text)),"glich: ") !== false ||
strpos(strtolower(filterHTML($text)),"=") !== false )
&& $top < $maxposy ) {
if ( $place != "CB" ) $maxposy = $top;
}
}
// guessing positions for Cafeteria B
if ( $place == "CB" ) {
array_push($rows, 0);
array_push($rowsNames, "Mensa Vital");
array_push($rows, 450); //315
array_push($rowsNames, "Aus Topf und Pfanne");
}
// in case of broken Bistro plan , needs to be changed at the top, too
/*if ( $place == "Bistro" && $week == 11) {
array_push($rows, 200);
array_push($rowsNames, "Pizza I");
array_push($rows, 300);
array_push($rowsNames, "Pizza II");
array_push($rows, 400);
array_push($rowsNames, "Pizza III");
array_push($rows, 480);
array_push($rowsNames, "Pasta I");
array_push($rows, 520);
array_push($rowsNames, "Pasta II");
}*/
// initialise arrays
for ( $i = 0; $i < sizeof($columns); $i++){
for ( $j = 0; $j < sizeof($rows); $j++){
$food[$i][$j]="";
$bold[$i][$j]=false;
$mealPrice[$i][$j]="";
}
}
echo $place."\n";
// get positions of elements and sort them to the right position
foreach ( $elements as $element ){
$position = getPositionOfElement($element, $posx, $posy, $maxposx, $maxposy,
$rows, $columns);
if ( $position === false ) {
continue;
} else {
$x = $position['x'];
$y = $position['y'];
}
/*
* insert " – " if text changes from bold to normal.
* by that, the ingredients list and the name of a pizza are seperated
* (Bistro)
*/
if ( $element->xpath("./b") != array()) {
$boldElement = true;
} else {
$boldElement = false;
}
/*
* filterMeals($food[$i][$j]) needed b/c the day that gets
* filtered out later is bold
*/
if ($bold[$x][$y] && !$boldElement && filterMeals($food[$x][$y]) != ""
&& strpos(getTextFromNode($element),"€") === false
&& filterMeals(getTextFromNode($element)) != ""){
$food[$x][$y] .= " – ";
}
$bold[$x][$y] = $boldElement;
/*
* Prices are either below the meal name or below the meal category
* if it's below the category, it's already saved as $rowPrice.
* Otherwise, we find it here. Prices are identified on the basis of the
* existence of a €. Luckily, prices are always in an extra line. Let's
* hope it stays that way.
*/
if ( isset($rowPrice[$y]) ){
$mealPrice[$x][$y]=$rowPrice[$y];
}
if ( strpos(filterHTML(getTextFromNode($element)),"€") !== false ){
$mealPrice[$x][$y].=" ".filterHTML(getTextFromNode($element));
} else {// if it's not a price, append it to the meal
if ( $place != "CB" || strpos(filterHTML(getTextFromNode($element)),"Kilojoule") === false )
$food[$x][$y] .= " " . filterHTML(getTextFromNode($element));
}
}
return jsonify($json, $rowsNames ,$food, $mealPrice,
sizeof($columns),sizeof($rows),$place,
$week);
}
// get the URLs of the plans we want to parse
$plans = getPlansURLs();
// download plans and reformat them to XML
$i = 0;
foreach ($plans as $plan ) {
exec("mkdir " . $pfad . "/plans");
exec("wget --output-document ".$pfad."/plans/plan$i.pdf ".$plan);
exec("pdftohtml -i -c -xml ".$pfad."/plans/plan$i.pdf");
array_push($plansXML,"plans/plan$i.xml");
$i++;
}
// parse plans
$t = 0;
foreach ( $plansXML as $planXML ) {
preg_match_all('/\d+/', $plans[$t], $matches);
$calendarWeek = array_pop($matches[0]);
$calendarWeek = sprintf("%d", $calendarWeek);
// cut out old weeks
if ($calendarWeek >= date("W",time()) && $calendarWeek != 52) {
// Mensa
if ( (strpos($plans[$t], "mensa-uni") !== false || strpos($plans[$t], "UL") !== false) && file_exists($planXML) ) {
$json=parsePlan($json,60,70,1500,2000,$calendarWeek,$planXML,"Mensa");
// Bistro
} else if ( (strpos($plans[$t], "uni-bistro") !== false || strpos($plans[$t], "Bistro") !== false) && file_exists($planXML) ){
$json=parsePlan($json,120,120,1500,750,$calendarWeek,$planXML,"Bistro");
// Cafeteria West
} else if ( (strpos($plans[$t], "cafeteria-uni-west") !== false || strpos($plans[$t], "West") !== false) && file_exists($planXML) ){
$json=parsePlan($json,120,120,1500,800,$calendarWeek,$planXML,"West");
// Prittwitzstrasse
} else if (strpos($plans[$t], "rittwitzstr") !== false && file_exists($planXML) ){
$json=parsePlan($json,120,120,1500,800,$calendarWeek,$planXML,"Prittwitzstr");
//Hochschulleitung
//} else if ( strpos($plans[$t], "HL") !== false && file_exists($planXML) ){
// $json=parsePlan($json,120,120,800,1500,$calendarWeek,$planXML,"Hochschulleitung",60);
// HS Oberer Eselsberg
//} else if ( strpos($plans[$t], "OE") !== false && file_exists($planXML) ){
// $json=parsePlan($json,120,120,800,1500,$calendarWeek,$planXML,"HSOE",60);
// Cafeteria B
} else if ( (strpos($plans[$t], "cafeteria-b-mensavital") !== false || strpos($plans[$t], "CB") !== false) && file_exists($planXML) ){
//$json=parsePlan($json,180,120,700,710,$calendarWeek,$planXML,"CB");//quick fix for CW 15
//$json=parsePlan($json,180,120,2000,450,$calendarWeek,$planXML,"CB");
if ( $calendarWeek < 53 ) // workaround
$json=parsePlan($json,120,120,2000,630,$calendarWeek,$planXML,"CB");
}
}
$t++;
}
// Save as JSON
$fp = fopen($outputDir.'mensaplan.json', 'w');
fwrite($fp, json_encode($json));
fclose($fp);
// Save also as XML for compatibility reasons
$xml = new SimpleXMLElement('<mensaplan/>');
//if (false){
foreach ( $json['weeks'] as $weekkey => $weekvalue ) {
//echo $weekvalue['weekNumber']."<br>";
// add weeks
$xmlweek = $xml->addChild("week");
$xmlweek->addAttribute('weekOfYear', $weekvalue['weekNumber']);
foreach ( $weekvalue['days'] as $daykey => $dayvalue ) {
//echo $dayvalue['date']."<br>";
if ($dayvalue['Mensa']['open']) {
//echo "open<br>";
$xmlday = $xmlweek->addChild("day");
$xmlday->addAttribute('date', $dayvalue['date']);
$xmlday->addAttribute('open', "1");
// mark today day as today b/c htmlifier needs this..
if ($dayvalue['date']==date("Y-m-d")) {
$xmlday->addAttribute('today', "today");
}
foreach ( $dayvalue['Mensa']['meals'] as $mealkey => $mealvalue ) {
//echo $mealvalue['category'].": ".$mealvalue['meal']."<br>";
$xmlmeal = $xmlday->addChild("meal");
$xmlmeal->addAttribute('type', $mealvalue['category']);
$xmlmeal->addChild('item', $mealvalue['meal']);
}
}
}
//}
}
$xml->asXML($outputDir."mensaplan.xml");
// clean up
exec("rm -rf ".$pfad."/plans");
?>