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
Branches
No related tags found
No related merge requests found
...@@ -11,40 +11,40 @@ class GERICHT { ...@@ -11,40 +11,40 @@ class GERICHT {
private $kinderteller = false; private $kinderteller = false;
private $image = ""; private $image = "";
private $zusatzStoffe = array ( private $zusatzStoffe = array (
0 => false, 0,
1 => false, 0,
2 => false, 0,
3 => false, 0,
4 => false, 0,
5 => false, 0,
6 => false, 0,
7 => false, 0,
8 => false, 0,
9 => false, 0,
10 => false, 0,
11 => false, 0,
12 => false, 0,
13 => false, 0,
14 => false, 0,
15 => false, 0,
16 => false, 0,
17 => false, 0,
18 => false, 0,
19 => false, 0,
20 => false, 0,
21 => false, 0,
22 => false, 0,
23 => false, 0,
24 => false, 0,
25 => false, 0,
26 => false, 0,
27 => false, 0,
28 => false, 0,
29 => false, 0,
30 => false, 0,
31 => false, 0,
32 => false, 0,
33 => false 0
); );
private $date = ""; private $date = "";
public function getShortText() { public function getShortText() {
...@@ -149,7 +149,7 @@ class GERICHT { ...@@ -149,7 +149,7 @@ class GERICHT {
} }
return $stoffe; return $stoffe;
} }
public function getDate(){ public function getDate() {
return $this->date; return $this->date;
} }
public function setShortText($shortText) { public function setShortText($shortText) {
...@@ -215,7 +215,7 @@ class GERICHT { ...@@ -215,7 +215,7 @@ class GERICHT {
public function setZusatzstoffe($zusatzStoffe) { public function setZusatzstoffe($zusatzStoffe) {
$this->zusatzStoffe = $zusatzStoffe; $this->zusatzStoffe = $zusatzStoffe;
} }
public function setDate($date){ public function setDate($date) {
$this->date = $date; $this->date = $date;
} }
public function toggleRind() { public function toggleRind() {
...@@ -269,6 +269,9 @@ class GERICHT { ...@@ -269,6 +269,9 @@ class GERICHT {
public function toggleZusatzstoff($in) { public function toggleZusatzstoff($in) {
$this->zusatzStoffe [$in] = ! $this->zusatzStoffe [$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() { public function __toString() {
for($i = 0; $i < count ( $this->zusatzStoffe ); $i ++) { for($i = 0; $i < count ( $this->zusatzStoffe ); $i ++) {
if ($this->zusatzStoffe [$i]) { if ($this->zusatzStoffe [$i]) {
......
...@@ -7,7 +7,7 @@ if ($mysqli->connect_errno) { ...@@ -7,7 +7,7 @@ if ($mysqli->connect_errno) {
} }
if ($result = $mysqli->query("SELECT * FROM City")) { if ($result = $mysqli->query("SELECT * FROM City")) {
$result->close(); $result->close();
} }
......
...@@ -6,4 +6,81 @@ function booleanToString($bool) { ...@@ -6,4 +6,81 @@ function booleanToString($bool) {
return "False"; 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 @@ ...@@ -2,100 +2,41 @@
include 'simple_html_dom.php'; include 'simple_html_dom.php';
include 'helper.php'; include 'helper.php';
include 'gericht.php'; include 'gericht.php';
include 'tag.php';
$html = file_get_html ( 'http://www.stwdo.de/gastronomie/speiseplaene/hauptmensa/wochenansicht-hauptmensa/' ); $html = file_get_html ( 'http://www.stwdo.de/gastronomie/speiseplaene/hauptmensa/wochenansicht-hauptmensa/' );
function parsDay($html, $day, $gerichte) { $montag = parsDay ( $html, "montag" );
$date = substr ( $html->find ( 'a[href="#' . $day . '"]', 0 )->innertext, - 10, 10 ); $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 ); * // SAVE
$tr = $html->first_child (); * $mysqli = new mysqli ( "ovanier.de", "info_writer", "XAHQTZeGbqsnt8K6", "infoscreen" );
while ( $tr != null ) { *
$current = new GERICHT (); * if ($mysqli->connect_errno) {
$td0 = $tr->find ( 'td', 0 ); * printf ( "Connect failed: %s\n", $mysqli->connect_error );
$td1 = $tr->find ( 'td', 1 ); * exit ();
$td2 = $tr->find ( 'td', 2 ); * }
// parse originalText shortText and Zusatzstoffe *
if ($td0 != null) { * $stmt = mysqli_prepare ( $mysqli, "INSERT INTO gerichte (originalText,shortText,rind,schwein,gefluegel,fisch,vegetarisch,vegan,kinderteller,image,date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)" );
$current->setOriginalText ( $td0->innertext ); * foreach ( $gerichte as $gericht ) {
// prepare Zusatzstoffe * 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 () );
$zusatzstoffe = $td0->innertext; * $stmt->execute ();
preg_match_all ( '#\([\d+,]+\)#', $zusatzstoffe, $matches ); * }
foreach ( $matches [0] as $match ) { * $stmt->close ();
preg_match_all ( '#[\d+]+#', $match, $stoffe ); *
foreach ( $stoffe [0] as $stoff ) { * $mysqli->close ();
$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 );
$mysqli = new mysqli ( "ovanier.de", "info_writer", "XAHQTZeGbqsnt8K6", "infoscreen" ); // CREATE JSON
toJson ( $tage );
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 ();
?> ?>
\ 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