Skip to content
Snippets Groups Projects
Commit c6280144 authored by Hotte's avatar Hotte
Browse files

anpassung v1 wegen neuer mensa seite

parent 9bd5f0c5
Branches
No related tags found
No related merge requests found
<?php
// Universal
$MENSA_URL = "http://www.stwdo.de/gastronomie/speiseplaene/hauptmensa/wochenansicht-hauptmensa/";
$PARSE_DAYLIST = array (
"montag",
"dienstag",
"mittwoch",
"donnerstag",
"freitag"
);
// JSON
$JSON_NAME = "../../tmp/mensaPlan.json";
$JSON_INC_NR = false;
$JSON_INC_ORIGINAL = false;
$JSON_INC_SHORT = true;
$JSON_INC_ART = true;
$JSON_INC_KIND = false;
$JSON_INC_COUNTER = true;
$JSON_INC_STOFFE = false;
$JSON_INC_DATE = true;
?>
<?php
class GERICHT {
private $nr = 0;
private $originalText = "";
private $shortText = "";
private $rind = false;
private $schwein = false;
private $gefluegel = false;
private $fisch = false;
private $vegetarisch = false;
private $vegan = false;
private $kinderteller = false;
private $counter = "";
private $zusatzStoffe = array (
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0
);
private $date = "";
public function getNr() {
return $this->nr;
}
public function getOriginalText() {
return $this->originalText;
}
public function getShortText() {
return $this->shortText;
}
public function isRind() {
return $this->rind;
}
public function isSchwein() {
return $this->schwein;
}
public function isGefluegel() {
return $this->gefluegel;
}
public function isFisch() {
return $this->fisch;
}
public function isVegetarisch() {
return $this->vegetarisch;
}
public function isVegan() {
return $this->vegan;
}
public function isKinderteller() {
return $this->kinderteller;
}
public function getArtAsString($seperator = " ") {
$art = "";
$first = true;
if ($this->rind) {
$art .= "R";
$first = false;
}
if ($this->schwein) {
if (! $first) {
$art .= $seperator;
}
$art .= "S";
$first = false;
}
if ($this->gefluegel) {
if (! $first) {
$art .= $seperator;
}
$art .= "G";
$first = false;
}
if ($this->fisch) {
if (! $first) {
$art .= $seperator;
}
$art .= "F";
$first = false;
}
if ($this->vegetarisch) {
if (! $first) {
$art .= $seperator;
}
$art .= "V";
$first = false;
}
if ($this->vegan) {
if (! $first) {
$art .= $seperator;
}
$art .= "N";
$first = false;
}
if ($this->kinderteller) {
if (! $first) {
$art .= $seperator;
}
$art .= "K";
$first = false;
}
return $art;
}
public function getCounter() {
return $this->counter;
}
public function isZusatzstoff($id) {
return $this->zusatzStoffe [$id];
}
public function getZusatzstoffe() {
return $this->zusatzStoffe;
}
public function getZusatzstoffeAsString($seperator = " ") {
$stoffe = "";
$first = true;
for($i = 0; $i < count ( $this->zusatzStoffe ); $i ++) {
if ($this->zusatzStoffe [$i]) {
if (! $first) {
$stoffe .= $seperator . $i;
} else {
$stoffe .= $i;
$first = false;
}
}
}
return $stoffe;
}
public function getDate() {
return $this->date;
}
public function setNr($nr) {
$this->nr = $nr;
}
public function setOriginalText($originalText) {
$this->originalText = $originalText;
}
public function setShortText($shortText) {
$this->shortText = $shortText;
}
public function setRind($rind) {
$this->rind = $rind;
}
public function setSchwein($schwein) {
$this->schwein = $schwein;
}
public function setGefluegel($gefluegel) {
$this->gefluegel = $gefluegel;
}
public function setFisch($fisch) {
$this->fisch = $fisch;
}
public function setVegetarisch($vegetarisch) {
$this->vegetarisch = $vegetarisch;
}
public function setVegan($vegan) {
$this->vegan = $vegan;
}
public function setKinderteller($kinderteller) {
$this->kinderteller = $kinderteller;
}
public function setArt($in, $val) {
switch ($in) {
case "R" :
$this->rind = $val;
break;
case "S" :
$this->schwein = $val;
break;
case "G" :
$this->gefluegel = $val;
break;
case "F" :
$this->fisch = $val;
break;
case "V" :
$this->vegetarisch = $val;
break;
case "N" :
$this->vegan = $val;
break;
case "K" :
$this->kinderteller = $val;
break;
default :
echo "Angegebene Art '" . $in . "' nicht bekannt.";
}
}
public function setCounter($counter) {
$this->counter = $counter;
}
public function setZusatzstoff($in, $val) {
$this->zusatzStoffe [$in] = $val;
}
public function setZusatzstoffe($zusatzStoffe) {
$this->zusatzStoffe = $zusatzStoffe;
}
public function setDate($date) {
$this->date = $date;
}
public function toggleRind() {
$this->rind = ! $this->rind;
}
public function toggleSchwein() {
$this->schwein = ! $this->schwein;
}
public function toggleGefluegel() {
$this->gefluegel = ! $this->gefluegel;
}
public function toggleFisch() {
$this->fisch = ! $this->fisch;
}
public function toggleVegetarisch() {
$this->vegetarisch = ! $this->vegetarisch;
}
public function toggleVegan() {
$this->vegan = ! $this->vegan;
}
public function toggleKinderteller() {
$this->kinderteller = ! $this->kinderteller;
}
public function toggleArt($in) {
switch ($in) {
case "R" :
$this->rind = ! $this->rind;
break;
case "S" :
$this->schwein = ! $this->schwein;
break;
case "G" :
$this->gefluegel = ! $this->gefluegel;
break;
case "F" :
$this->fisch = ! $this->fisch;
break;
case "V" :
$this->vegetarisch = ! $this->vegetarisch;
break;
case "N" :
$this->vegan = ! $this->vegan;
break;
case "K" :
$this->kinderteller = ! $this->kinderteller;
break;
default :
echo "Angegebene Art '" . $in . "' nicht bekannt.";
}
}
public function toggleZusatzstoff($in) {
$this->zusatzStoffe [$in] = ! $this->zusatzStoffe [$in];
}
public function toJson($inc_nr = true, $inc_original = true, $inc_short = true, $inc_art = true, $inc_kind = true, $inc_counter = true, $inc_stoffe = true, $inc_date = true) {
$json = "{";
if ($inc_nr) {
$json .= '"nr": "' . $this->nr . '"';
}
if ($inc_nr && ($inc_original || $inc_short || $inc_art || $inc_kind || $inc_counter || $inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_original) {
$json .= '"originalText": "' . $this->originalText . '"';
}
if ($inc_original && ($inc_short || $inc_art || $inc_kind || $inc_counter || $inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_short) {
$json .= '"shortText": "' . $this->shortText . '"';
}
if ($inc_short && ($inc_art || $inc_kind || $inc_counter || $inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_art) {
$json .= '"rind": ' . $this->rind . ',';
$json .= '"schwein": ' . $this->schwein . ',';
$json .= '"gefluegel": ' . $this->gefluegel . ',';
$json .= '"fisch": ' . $this->fisch . ',';
$json .= '"vegetarisch": ' . $this->vegetarisch . ',';
$json .= '"vegan": ' . $this->vegan . '';
}
if ($inc_art && ($inc_kind || $inc_counter || $inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_kind) {
$json .= '"kinderteller":' . $this->kinderteller . '';
}
if ($inc_kind && ($inc_counter || $inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_counter) {
if ($this->getCounter () == "") {
$json .= '"counter": ' . 0;
} else {
$json .= '"counter": ' . $this->getCounter ();
}
}
if ($inc_counter && ($inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_stoffe) {
$json .= '"zusatzStoffe": {' . '"0":' . $this->zusatzStoffe [0] . ',' . '"1":' . $this->zusatzStoffe [1] . ',' . '"2":' . $this->zusatzStoffe [2] . ',' . '"3":' . $this->zusatzStoffe [3] . ',' . '"4":' . $this->zusatzStoffe [4] . ',' . '"5":' . $this->zusatzStoffe [5] . ',' . '"6":' . $this->zusatzStoffe [6] . ',' . '"7":' . $this->zusatzStoffe [7] . ',' . '"8":' . $this->zusatzStoffe [8] . ',' . '"9":' . $this->zusatzStoffe [9] . ',' . '"10":' . $this->zusatzStoffe [10] . ',' . '"11":' . $this->zusatzStoffe [11] . ',' . '"12":' . $this->zusatzStoffe [12] . ',' . '"13":' . $this->zusatzStoffe [13] . ',' . '"14":' . $this->zusatzStoffe [14] . ',' . '"15":' . $this->zusatzStoffe [15] . ',' . '"16":' . $this->zusatzStoffe [16] . ',' . '"17":' . $this->zusatzStoffe [17] . ',' . '"18":' . $this->zusatzStoffe [18] . ',' . '"19":' . $this->zusatzStoffe [19] . ',' . '"20":' . $this->zusatzStoffe [20] . ',' . '"21":' . $this->zusatzStoffe [21] . ',' . '"22":' . $this->zusatzStoffe [22] . ',' . '"23":' . $this->zusatzStoffe [23] . ',' . '"24":' . $this->zusatzStoffe [24] . ',' . '"25":' . $this->zusatzStoffe [25] . ',' . '"26":' . $this->zusatzStoffe [26] . ',' . '"27":' . $this->zusatzStoffe [27] . ',' . '"28":' . $this->zusatzStoffe [28] . ',' . '"29":' . $this->zusatzStoffe [29] . ',' . '"30":' . $this->zusatzStoffe [30] . ',' . '"31":' . $this->zusatzStoffe [31] . ',' . '"32":' . $this->zusatzStoffe [32] . ',' . '"33":' . $this->zusatzStoffe [33] . '}';
}
if ($inc_stoffe && ($inc_date)) {
$json .= ',';
}
if ($inc_date) {
$json .= '"date": "' . $this->date . '"';
}
$json .= '}';
return $json;
}
public function __toString() {
for($i = 0; $i < count ( $this->zusatzStoffe ); $i ++) {
if ($this->zusatzStoffe [$i]) {
$stoffe = $stoffe . " " . $i;
}
}
return "Nr: " . $this->nr . ", Original Text: " . $this->originalText . ",\n" . "Short Text: " . $this->shortText . ",\n" . "Rind: " . booleanToString ( $this->rind ) . ",\n" . "Schwein: " . booleanToString ( $this->schwein ) . ",\n" . "Fisch: " . booleanToString ( $this->fisch ) . ",\n" . "Vegetarisch: " . booleanToString ( $this->vegetarisch ) . ",\n" . "Vegan: " . booleanToString ( $this->vegan ) . ",\n" . "Kinderteller: " . booleanToString ( $this->kinderteller ) . ",\n" . "Counter: " . $this->counter . ",\n" . "Zusatzstoffe: " . $stoffe . ",\n";
}
}
?>
\ No newline at end of file
<?php
function booleanToString($bool) {
if ($bool) {
return "True";
} else {
return "False";
}
}
function toJson($tage, $name, $inc_nr, $inc_original, $inc_short, $inc_art, $inc_kind, $inc_stoffe, $inc_counter, $inc_date) {
$kommata = false;
$json = '{';
foreach ( $tage as $tag ) {
if ($kommata) {
$json .= ',';
} else {
$kommata = true;
}
$json .= $tag->toJson ( $inc_nr, $inc_original, $inc_short, $inc_art, $inc_kind, $inc_counter, $inc_stoffe, $inc_date );
}
$json .= "}";
file_put_contents ( $name, $json );
}
function parsDay($html, $day) {
$tag = new TAG ( $day, substr ( $html->find ( 'a[href="#' . $day . '"]', 0 )->innertext, - 10, 10 ) );
$html = $html->find ( 'a.#' . $day, 0 );
$html = $html->first_child ()->children ( 1 );
$tr = $html->first_child ();
$nr = 0;
while ( $tr != null ) {
$nr ++;
$current = new GERICHT ();
$current->setNr ( $nr );
$td0 = $tr->find ( 'td', 0 );
$td1 = $tr->find ( 'td', 1 );
$td2 = $tr->find ( 'td', 2 );
// parse originalText shortText and Zusatzstoffe
if ($td0 != null) {
$current->setOriginalText ( $td0->innertext );
// prepare Zusatzstoffe
$zusatzstoffe = $td0->innertext;
preg_match_all ( '#\([\d+,]+\)#', $zusatzstoffe, $matches );
foreach ( $matches [0] as $match ) {
preg_match_all ( '#[\d+]+#', $match, $stoffe );
foreach ( $stoffe [0] as $stoff ) {
$current->setZusatzstoff ( $stoff, true );
}
}
// shorten Text
$current->setShortText ( shortenText ( $td0->innertext ) );
}
// parse Art
if ($td1 != null) {
$art = $td1->innertext;
$current->setRind ( substr_count ( $art, "R" ) );
$current->setSchwein ( substr_count ( $art, "S" ) );
$current->setGefluegel ( substr_count ( $art, "G" ) );
$current->setFisch ( substr_count ( $art, "F" ) );
$current->setVegetarisch ( substr_count ( $art, "V" ) );
$current->setVegan ( substr_count ( $art, "N" ) );
$current->setKinderteller ( substr_count ( $art, "K" ) );
}
// parse Counter
if ($td2 != null) {
$img = $td2->find ( 'img', 0 );
if ($img != null) {
$img = $img->src;
$img = array_pop ( explode ( "/", $img ) );
switch ($img) {
case "icon-menue-1.png" :
$current->setCounter ( 1 );
break;
case "icon-menue-2.png" :
$current->setCounter ( 2 );
break;
case "icon-tagesgericht.png" :
$current->setCounter ( 3 );
break;
case "icon-vegetarisch.png" :
$current->setCounter ( 4 );
break;
case "icon-aktionsteller.png" :
$current->setCounter ( 5 );
break;
case "icon-grillstation.png" :
$current->setCounter ( 6 );
break;
case "icon-fisch.png" :
$current->setCounter ( 7 );
break;
case "icon-vegan.png" :
$current->setCounter ( 8 );
break;
}
}
}
if ($current->getOriginalText () != "") { // remove empty Fields
$current->setDate ( $tag->getDate () );
$tag->addGericht ( $current );
}
$tr = $tr->next_sibling ();
}
return $tag;
}
function shortenText($text) {
$text = preg_replace ( '#\([^\)]*\)#', '', $text ); // remove zusatzstoffe
$text = preg_replace ( '#(\s,|,\s)#', ',', $text ); // fix Komma
$text = preg_replace ( '#,#', ', ', $text ); // fix Komma
$text = preg_replace ( '#\s+#', ' ', $text ); // fix double Whitespace
$text = preg_replace ( '#,\sdazu\s\d\sBeilagen\snach\sWahl#', '', $text ); // remove Beilagen
$text = preg_replace ( '#,\sdazu\s#', ' + ', $text ); // replace dazu
$text = preg_replace ( '#\sund\s#', ' & ', $text ); // replace und
$text = str_replace(' ;','; ',$text); //weniger hässlich
return $text;
}
?>
\ No newline at end of file
<?php
require 'config.php';
require 'simple_html_dom.php';
require 'helper.php';
require 'gericht.php';
require 'tag.php';
// Parse
$html = file_get_html ( $MENSA_URL );
if ($html != null) {
$tage = array ();
foreach ( $PARSE_DAYLIST as $DAYNAME ) {
array_push ( $tage, parsDay ( $html, $DAYNAME ) );
}
// CREATE JSON
toJson ( $tage, $JSON_NAME, $JSON_INC_NR, $JSON_INC_ORIGINAL, $JSON_INC_SHORT, $JSON_INC_ART, $JSON_INC_KIND, $JSON_INC_STOFFE, $JSON_INC_COUNTER, $JSON_INC_DATE );
} else {
echo "Webseite nicht erreichbar.";
}
?>
\ No newline at end of file
......@@ -32,6 +32,11 @@ this.hide = function() {
clearInterval(this.interval);
}
function padTwo(num) {
var s = "00" + num;
return s.substr(2);
}
this.loaded = function(panel, config) {
// reload data
setInterval(function() {
......@@ -39,7 +44,7 @@ this.loaded = function(panel, config) {
}, 30*60*1000);
function getMensaPlan() {
$.getJSON("tmp/mensaPlan.json?"+( new Date().getTime()) ).done(function(json) {
$.getJSON("https://mobil.itmc.tu-dortmund.de/canteen-menu/v1/canteens/341/"+( new Date().getFullYear() + "-" + padTwo(new Date().getMonth()) + "-" + padTwo(new Date().getDate()) ).done(function(json) {
fillTable(json);
}).fail(function(jqxhr, textStatus, error) {
var err = textStatus + ", " + error;
......@@ -48,6 +53,7 @@ this.loaded = function(panel, config) {
}
function fillTable(json) {
console.log(json)
var date = new Date();
var next = 0;
if (date.getHours() >= 15) {
......
This diff is collapsed.
<?php
class TAG {
private $nr = 0;
private $name = 0;
private $gerichte = array ();
private $date = "";
function __construct($name, $date) {
$this->name = $name;
$this->date = $date;
switch ($name) {
case "montag" :
$this->nr = 1;
break;
case "dienstag" :
$this->nr = 2;
break;
case "mittwoch" :
$this->nr = 3;
break;
case "donnertag" :
$this->nr = 4;
break;
case "freitag" :
$this->nr = 5;
break;
case "samstag" :
$this->nr = 6;
break;
case "sonntag" :
$this->nr = 7;
breakM;
}
}
public function getNr() {
return $this->nr;
}
public function getName() {
return $this->name;
}
public function getGerichte() {
return $this->gerichte;
}
public function getDate() {
return $this->date;
}
public function setNr($nr) {
$this->nr = $nr;
}
public function setName($name) {
$this->name = $name;
}
public function setGerichte($gerichte) {
$this->gerichte = $gerichte;
}
public function setDate() {
$this->date = $date;
}
public function addGericht($gericht) {
array_push ( $this->gerichte, $gericht );
}
public function addGerichte($gerichte) {
foreach ( $gerichte as $gericht ) {
array_push ( $this->gerichte, $gericht );
}
}
public function toJson($inc_nr = true, $inc_original = true, $inc_short = true, $inc_art = true, $inc_kind = true, $inc_counter = true, $inc_stoffe = true, $inc_date = true) {
$kommata = false;
$json = '"' . $this->name . '":[';
foreach ( $this->gerichte as $gericht ) {
if ($kommata) {
$json .= ',';
} else {
$kommata = true;
}
$json .= $gericht->toJson ( $inc_nr,$inc_original, $inc_short, $inc_art, $inc_kind, $inc_counter, $inc_stoffe, $inc_date );
}
$json .= ']';
return $json;
}
public function __toString() {
$string = "Nr: " . $this->nr . ", Name: " . $this->name . ", Gerichte: ";
foreach ( $this->gerichte as $gericht ) {
$string .= $gericht;
}
$string .= ", Date: " . $this->date;
return $string;
}
}
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment