Skip to content
Snippets Groups Projects
Commit 2da3a09a authored by Rico van Endern's avatar Rico van Endern
Browse files

magical new json creation -mysql for a little moment

parent 2e81f3b3
No related branches found
No related tags found
No related merge requests found
......@@ -11,40 +11,40 @@ class GERICHT {
private $kinderteller = false;
private $image = "";
private $zusatzStoffe = array (
0 => false,
1 => false,
2 => false,
3 => false,
4 => false,
5 => false,
6 => false,
7 => false,
8 => false,
9 => false,
10 => false,
11 => false,
12 => false,
13 => false,
14 => false,
15 => false,
16 => false,
17 => false,
18 => false,
19 => false,
20 => false,
21 => false,
22 => false,
23 => false,
24 => false,
25 => false,
26 => false,
27 => false,
28 => false,
29 => false,
30 => false,
31 => false,
32 => false,
33 => false
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 getShortText() {
......@@ -269,6 +269,9 @@ class GERICHT {
public function toggleZusatzstoff($in) {
$this->zusatzStoffe [$in] = ! $this->zusatzStoffe [$in];
}
public function toJson() {
return '{' . '"shortText": "' . $this->shortText . '",' . '"originalText": "' . $this->originalText . '",' . '"rind":' . $this->rind . ',' . '"schwein":' . $this->schwein . ',' . '"gefluegel":' . $this->gefluegel . ',' . '"fisch":' . $this->fisch . ',' . '"vegetarisch":' . $this->vegetarisch . ',' . '"vegan":' . $this->vegan . ',' . '"kinderteller":' . $this->kinderteller . ',' . '"image": "' . $this->image . '",' . '"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] . '}, ' . '"date": "' . $this->date . '"' . '}';
}
public function __toString() {
for($i = 0; $i < count ( $this->zusatzStoffe ); $i ++) {
if ($this->zusatzStoffe [$i]) {
......
......@@ -6,4 +6,81 @@ function booleanToString($bool) {
return "False";
}
}
function toJson($tage) {
$kommata = false;
$json = '{';
foreach ( $tage as $tag ) {
if ($kommata) {
$json .= ',';
} else {
$kommata = true;
}
$json .= $tag->toJson ();
}
$json .= "}";
file_put_contents("mensa.json", $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 ();
while ( $tr != null ) {
$current = new GERICHT ();
$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 Image
if ($td2 != null) {
$img = $td2->find ( 'img', 0 );
if ($img != null) {
$current->setImage ( $img->src );
}
}
if ($current->getOriginalText () != "") { // remove empty Fields
$current->setDate ( $tag->date );
array_push ( $tag->gerichte, $current );
}
$tr = $tr->next_sibling ();
}
return $tag;
}
function shortenText($text) {
$text = preg_replace ( '#\([\d+,]+\)#', '', $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
return $text;
}
?>
\ No newline at end of file
This diff is collapsed.
......@@ -2,100 +2,41 @@
include 'simple_html_dom.php';
include 'helper.php';
include 'gericht.php';
include 'tag.php';
$html = file_get_html ( 'http://www.stwdo.de/gastronomie/speiseplaene/hauptmensa/wochenansicht-hauptmensa/' );
function parsDay($html, $day, $gerichte) {
$date = substr ( $html->find ( 'a[href="#' . $day . '"]', 0 )->innertext, - 10, 10 );
$montag = parsDay ( $html, "montag" );
$dienstag = parsDay ( $html, "dienstag" );
$mittwoch = parsDay ( $html, "mittwoch" );
$donnerstag = parsDay ( $html, "donnerstag" );
$freitag = parsDay ( $html, "freitag" );
$tage = array (
$montag,
$dienstag,
$mittwoch,
$donnerstag,
$freitag
);
$html = $html->find ( 'a.#' . $day, 0 );
$html = $html->first_child ()->children ( 1 );
$tr = $html->first_child ();
while ( $tr != null ) {
$current = new GERICHT ();
$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
$shortText = $td0->innertext;
$shortText = preg_replace ( '#\([\d+,]+\)#', '', $shortText ); // remove zusatzstoffe
$shortText = preg_replace ( '#(\s,|,\s)#', ',', $shortText ); // fix Komma
$shortText = preg_replace ( '#,#', ', ', $shortText ); // fix Komma
$shortText = preg_replace ( '#\s+#', ' ', $shortText ); // fix double Whitespace
$shortText = preg_replace ( '#,\sdazu\s\d\sBeilagen\snach\sWahl#', '', $shortText ); // remove Beilagen
$shortText = preg_replace ( '#,\sdazu\s#', ' + ', $shortText ); // replace dazu
$shortText = preg_replace ( '#\sund\s#', ' & ', $shortText ); // replace und
$current->setShortText ( $shortText );
}
// 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 Image
if ($td2 != null) {
$img = $td2->find ( 'img', 0 );
if ($img != null) {
$current->setImage ( $img->src );
}
}
if ($current->getOriginalText () != "") { // remove empty Fields
$current->setDate( $date );
array_push ( $gerichte, $current );
}
$tr = $tr->next_sibling ();
}
return $gerichte;
}
$gerichte = array ();
$gerichte = parsDay ( $html, "montag", $gerichte );
$gerichte = parsDay ( $html, "dienstag", $gerichte );
$gerichte = parsDay ( $html, "mittwoch", $gerichte );
$gerichte = parsDay ( $html, "donnerstag", $gerichte );
$gerichte = parsDay ( $html, "freitag", $gerichte );
/*
* // SAVE
* $mysqli = new mysqli ( "ovanier.de", "info_writer", "XAHQTZeGbqsnt8K6", "infoscreen" );
*
* if ($mysqli->connect_errno) {
* printf ( "Connect failed: %s\n", $mysqli->connect_error );
* exit ();
* }
*
* $stmt = mysqli_prepare ( $mysqli, "INSERT INTO gerichte (originalText,shortText,rind,schwein,gefluegel,fisch,vegetarisch,vegan,kinderteller,image,date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
* foreach ( $gerichte as $gericht ) {
* mysqli_stmt_bind_param ( $stmt, "ssiiiiiiisi", $gericht->getOriginalText (), $gericht->getShortText (), $gericht->isRind (), $gericht->isSchwein (), $gericht->isGefluegel (), $gericht->isFisch (), $gericht->isVegetarisch (), $gericht->isVegan (), $gericht->isKinderteller (), $gericht->getImage (), $gericht->getDate () );
* $stmt->execute ();
* }
* $stmt->close ();
*
* $mysqli->close ();
*/
$mysqli = new mysqli ( "ovanier.de", "info_writer", "XAHQTZeGbqsnt8K6", "infoscreen" );
if ($mysqli->connect_errno) {
printf ( "Connect failed: %s\n", $mysqli->connect_error );
exit ();
}
$stmt = mysqli_prepare($mysqli, "INSERT INTO gerichte (originalText,shortText,rind,schwein,gefluegel,fisch,vegetarisch,vegan,kinderteller,image,date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
foreach ( $gerichte as $gericht ) {
printf ( mysqli_stmt_bind_param($stmt, "ssiiiiiiisi"
, $gericht->getOriginalText()
, $gericht->getShortText()
, $gericht->isRind()
, $gericht->isSchwein()
, $gericht->isGefluegel()
, $gericht->isFisch()
, $gericht->isVegetarisch()
, $gericht->isVegan()
, $gericht->isKinderteller()
, $gericht->getImage()
, $gericht->getDate()
));
$stmt->execute();
}
$stmt->close();
$mysqli->close ();
// CREATE JSON
toJson ( $tage );
?>
\ No newline at end of file
<?php
class TAG {
public $nr = 0;
public $name = 0;
public $gerichte = array ();
public $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 toJson() {
$kommata = false;
$json = '"' . $this->name . '":[';
foreach ( $this->gerichte as $gericht ) {
if ($kommata) {
$json .= ',';
} else {
$kommata = true;
}
$json .= $gericht->toJson ();
}
$json .= ']';
return $json;
}
}
?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment