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

parser shit mit mehr json und mehr mysql ohh yeah

parent defce51c
No related branches found
No related tags found
No related merge requests found
......@@ -18,11 +18,13 @@ $SQL_PW = "XAHQTZeGbqsnt8K6";
$SQL_DB = "infoscreen";
// JSON
$JSON_NAME = "mensaPlan.json";
$JSON_INC_NR = true;
$JSON_INC_ORIGINAL = false;
$JSON_INC_SHORT = true;
$JSON_INC_ART = true;
$JSON_INC_KIND = false;
$JSON_INC_IMG = true;
$JSON_INC_COUNTER = true;
$JSON_INC_STOFFE = false;
$JSON_INC_DATE = true;
?>
\ No newline at end of file
<?php
class GERICHT {
private $nr = 0;
private $originalText = "";
private $shortText = "";
private $rind = false;
......@@ -9,7 +10,7 @@ class GERICHT {
private $vegetarisch = false;
private $vegan = false;
private $kinderteller = false;
private $image = "";
private $counter = "";
private $zusatzStoffe = array (
0,
0,
......@@ -47,6 +48,9 @@ class GERICHT {
0
);
private $date = "";
public function getNr() {
return $this->nr;
}
public function getOriginalText() {
return $this->originalText;
}
......@@ -125,8 +129,8 @@ class GERICHT {
}
return $art;
}
public function getImage() {
return $this->image;
public function getCounter() {
return $this->counter;
}
public function isZusatzstoff($id) {
return $this->zusatzStoffe [$id];
......@@ -152,6 +156,9 @@ class GERICHT {
public function getDate() {
return $this->date;
}
public function setNr($nr) {
$this->nr = $nr;
}
public function setOriginalText($originalText) {
$this->originalText = $originalText;
}
......@@ -206,8 +213,8 @@ class GERICHT {
echo "Angegebene Art '" . $in . "' nicht bekannt.";
}
}
public function setImage($image) {
$this->image = $image;
public function setCounter($counter) {
$this->counter = $counter;
}
public function setZusatzstoff($in, $val) {
$this->zusatzStoffe [$in] = $val;
......@@ -269,46 +276,47 @@ class GERICHT {
public function toggleZusatzstoff($in) {
$this->zusatzStoffe [$in] = ! $this->zusatzStoffe [$in];
}
public function toJson($inc_original = true, $inc_short = true, $inc_art = true, $inc_kind = true, $inc_img = true, $inc_stoffe = true, $inc_date = true) {
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_img || $inc_stoffe || $inc_date)) {
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_img || $inc_stoffe || $inc_date)) {
if ($inc_short && ($inc_art || $inc_kind || $inc_counter || $inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_art) {
$json .= '"rind":' . $this->rind;
$json .= '",';
$json .= '"schwein":' . $this->schwein;
$json .= '",';
$json .= '"gefluegel":' . $this->gefluegel;
$json .= '",';
$json .= '"fisch":' . $this->fisch;
$json .= '",';
$json .= '"vegetarisch":' . $this->vegetarisch;
$json .= '",';
$json .= '"vegan":' . $this->vegan . '"';
$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_img || $inc_stoffe || $inc_date)) {
if ($inc_art && ($inc_kind || $inc_counter || $inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_kind) {
$json .= '"kinderteller":' . $this->kinderteller . '"';
}
if ($inc_kind && ($inc_img || $inc_stoffe || $inc_date)) {
if ($inc_kind && ($inc_counter || $inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_img) {
$json .= '"image": "' . $this->image . '"';
if ($inc_counter) {
$json .= '"counter": "' . $this->getCounter() . '"';
}
if ($inc_img && ($inc_stoffe || $inc_date)) {
if ($inc_counter && ($inc_stoffe || $inc_date)) {
$json .= ',';
}
if ($inc_stoffe) {
......@@ -330,7 +338,7 @@ class GERICHT {
}
}
return "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" . "Image: " . $this->image . ",\n" . "Zusatzstoffe: " . $stoffe . ",\n";
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
......@@ -6,7 +6,7 @@ function booleanToString($bool) {
return "False";
}
}
function toJson($tage, $inc_original, $inc_short, $inc_art, $inc_kind, $inc_stoffe, $inc_img, $inc_date) {
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 ) {
......@@ -15,12 +15,12 @@ function toJson($tage, $inc_original, $inc_short, $inc_art, $inc_kind, $inc_stof
} else {
$kommata = true;
}
$json .= $tag->toJson ( $inc_original, $inc_short, $inc_art, $inc_kind, $inc_img, $inc_stoffe, $inc_date );
$json .= $tag->toJson ( $inc_nr, $inc_original, $inc_short, $inc_art, $inc_kind, $inc_counter, $inc_stoffe, $inc_date );
}
$json .= "}";
file_put_contents ( "mensa.json", $json );
file_put_contents ( $name, $json );
}
function toSql($tage, $server, $user, $pw, $db) {
function toSql($tage, $server, $user, $pw, $db) { // TODO NR
$mysqli = new mysqli ( $server, $user, $pw, $db );
if ($mysqli->connect_errno) {
......@@ -28,11 +28,12 @@ function toSql($tage, $server, $user, $pw, $db) {
exit ();
}
$stmt = mysqli_prepare ( $mysqli, "INSERT INTO gerichte (originalText,shortText,rind,schwein,gefluegel,fisch,vegetarisch,vegan,kinderteller,image,date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE originalText = ?" );
$stmt = mysqli_prepare ( $mysqli, "INSERT INTO gerichte (nr,originalText,shortText,rind,schwein,gefluegel,fisch,vegetarisch,vegan,kinderteller,counter,date) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE originalText = ?,shortText = ?,rind = ?,schwein = ?,gefluegel = ?,fisch = ?,vegetarisch = ?,vegan = ?,kinderteller = ?, counter = ?" );
foreach ( $tage as $tag ) {
foreach ( $tag->gerichte as $gericht ) {
echo mysqli_stmt_bind_param ( $stmt, "ssiiiiiiisis", $gericht->getOriginalText (), $gericht->getShortText (), $gericht->isRind (), $gericht->isSchwein (), $gericht->isGefluegel (), $gericht->isFisch (), $gericht->isVegetarisch (), $gericht->isVegan (), $gericht->isKinderteller (), $gericht->getImage (), $gericht->getDate (), $gericht->getOriginalText () );
$stmt->execute (); // TODO
foreach ( $tag->getGerichte () as $gericht ) {
echo mysqli_stmt_bind_param ( $stmt, "issiiiiiiiisssiiiiiiii", $gericht->getNr (), $gericht->getOriginalText (), $gericht->getShortText (), $gericht->isRind (), $gericht->isSchwein (), $gericht->isGefluegel (), $gericht->isFisch (), $gericht->isVegetarisch (), $gericht->isVegan (), $gericht->isKinderteller (), $gericht->getCounter (), $gericht->getDate (), $gericht->getOriginalText (), $gericht->getShortText (), $gericht->isRind (), $gericht->isSchwein (), $gericht->isGefluegel (), $gericht->isFisch (), $gericht->isVegetarisch (), $gericht->isVegan (), $gericht->isKinderteller (), $gericht->getCounter () );
echo $gericht->getNr ();
$stmt->execute ();
}
}
$stmt->close ();
......@@ -45,8 +46,11 @@ function parsDay($html, $day) {
$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 );
......@@ -76,16 +80,43 @@ function parsDay($html, $day) {
$current->setVegan ( substr_count ( $art, "N" ) );
$current->setKinderteller ( substr_count ( $art, "K" ) );
}
// parse Image
// parse Counter
if ($td2 != null) {
$img = $td2->find ( 'img', 0 );
if ($img != null) {
$current->setImage ( $img->src );
$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->date );
array_push ( $tag->gerichte, $current );
$current->setDate ( $tag->getDate () );
$tag->addGericht ( $current );
}
$tr = $tr->next_sibling ();
}
......
{"montag":[],"dienstag":[],"mittwoch":[],"donnerstag":[],"freitag":[]}
\ No newline at end of file
......@@ -7,18 +7,22 @@ require 'tag.php';
// Parse
$html = file_get_html ( $MENSA_URL );
$tage = array ();
foreach ( $PARSE_DAYLIST as $DAYNAME ) {
array_push ( $tage, parsDay ( $html, $DAYNAME ) );
}
// CREATE JSON
if ($DO_JSON) {
toJson ( $tage, $JSON_INC_ORIGINAL, $JSON_INC_SHORT, $JSON_INC_ART, $JSON_INC_KIND, $JSON_INC_STOFFE, $JSON_INC_IMG, $JSON_INC_DATE );
}
// SAVE
if ($DO_SQL) {
toSql ( $tage, $SQL_SERVER, $SQL_USER, $SQL_PW, $SQL_DB );
if ($html != null) {
$tage = array ();
foreach ( $PARSE_DAYLIST as $DAYNAME ) {
array_push ( $tage, parsDay ( $html, $DAYNAME ) );
}
// CREATE JSON
if ($DO_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 );
}
// SAVE
if ($DO_SQL) {
toSql ( $tage, $SQL_SERVER, $SQL_USER, $SQL_PW, $SQL_DB );
}
} else {
echo "Webseite nicht erreichbar.";
}
?>
\ No newline at end of file
This diff is collapsed.
CREATE TABLE IF NOT EXISTS `gerichte` (
`id` int(255) NOT NULL,
`nr` int(11) NOT NULL,
`originalText` text NOT NULL,
`shortText` text NOT NULL,
`rind` tinyint(1) NOT NULL DEFAULT '0',
......@@ -9,6 +10,9 @@ CREATE TABLE IF NOT EXISTS `gerichte` (
`vegetarisch` tinyint(1) NOT NULL DEFAULT '0',
`vegan` tinyint(1) NOT NULL DEFAULT '0',
`kinderteller` tinyint(1) NOT NULL DEFAULT '0',
`image` text,
`date` varchar(10) NOT NULL PRIMARY KEY UNIQUE KEY
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
\ No newline at end of file
`counter` int(11) DEFAULT '0',
`date` varchar(10) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=162 DEFAULT CHARSET=latin1;
ALTER TABLE `gerichte`
ADD PRIMARY KEY (`nr`,`date`), ADD UNIQUE KEY (`nr`,`date`), ADD KEY `id` (`id`);
\ No newline at end of file
<?php
class TAG {
public $nr = 0;
public $name = 0;
public $gerichte = array ();
public $date = "";
private $nr = 0;
private $name = 0;
private $gerichte = array ();
private $date = "";
function __construct($name, $date) {
$this->name = $name;
$this->date = $date;
......@@ -31,7 +31,39 @@ class TAG {
breakM;
}
}
public function toJson($inc_original = true, $inc_short = true, $inc_art = true, $inc_kind = true, $inc_img = true, $inc_stoffe = true, $inc_date = true) {
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 ) {
......@@ -40,10 +72,18 @@ class TAG {
} else {
$kommata = true;
}
$json .= $gericht->toJson ( $inc_original, $inc_short, $inc_art, $inc_kind, $inc_img, $inc_stoffe, $inc_dat );
$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 register or to comment