commit 920f6f44ff8c64a8456f6ee47f73ef782f311a40 Author: mxmehl Date: Fri May 5 12:39:03 2017 +0200 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2d20446 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.php +angebote.json* diff --git a/config.php.sample b/config.php.sample new file mode 100644 index 0000000..41ddbff --- /dev/null +++ b/config.php.sample @@ -0,0 +1,15 @@ +; diff --git a/index.php b/index.php new file mode 100644 index 0000000..d2dfdb0 --- /dev/null +++ b/index.php @@ -0,0 +1,386 @@ +query('//div[@id="pageSelection"]/select'); + $pages = $searchPages->item(0)->childNodes->length; + // create a dynamic url in which the current page number can be set in + $queryurldyn = str_replace("/P-1/", "/P-%page%/", $queryurl); + + // loop through available pages + for ($page = 1; $page <= $pages; $page++) { + $queryurlcur = str_replace("%page%", $page, $queryurldyn); + echo "Current search page: " . $queryurlcur . "
\n"; + + $site = xsite($queryurlcur); // load and transform URL of current page + // get all links to expose pages + $searchResult = $site->query('//a[@class="result-list-entry__brand-title-container"]/@href'); + + // loop through web search results + foreach($searchResult as $result){ + global $data; + $new = "y"; // is the search entry new? + // extract ID of link + preg_match("/\d+$/",$result->textContent, $matches); + $id = $matches[0]; + $id = (int)$id; // convert to int value + + $total = count($data); + for ($row = 0; $row < $total; $row++) { + if ($id === $data[$row]['id']) { + $new = "n"; // entry isn't new anymore + } + } + + if ($new === "y") { // entry is new + + // load and transform expose URL + $site = xsite("https://www.immobilienscout24.de/expose/" . $id); + + // description + $searchResult = $site->query('//h1[@id="expose-title"]'); + $desc = trim($searchResult->item(0)->nodeValue); + $desc = mb_convert_encoding($desc, 'UTF-8', 'UTF-8'); // remove/replace invalid characters + $descs = substr($desc, 0, 15); + $descs = mb_convert_encoding($descs, 'UTF-8', 'UTF-8'); + + // flat available from + $searchResult = $site->query('//dd[@class="is24qa-bezugsfrei-ab grid-item three-fifths"]'); + $bezug = trim($searchResult->item(0)->nodeValue); + + // warm rent + $searchResult = $site->query('//dd[@class="is24qa-gesamtmiete grid-item three-fifths font-bold"]'); + $mietew = trim($searchResult->item(0)->nodeValue); + + // cold rent + $searchResult = $site->query('//dd[@class="is24qa-kaltmiete grid-item three-fifths"]'); + $mietek = trim($searchResult->item(0)->nodeValue); + + // rooms + $searchResult = $site->query('//dd[@class="is24qa-zimmer grid-item three-fifths"]'); + $zimmer = trim($searchResult->item(0)->nodeValue); + + // size + $searchResult = $site->query('//dd[@class="is24qa-wohnflaeche-ca grid-item three-fifths"]'); + $qm = trim($searchResult->item(0)->nodeValue); + + // location + $searchResult = $site->query('//div[@class="address-block"]'); + $ort = trim($searchResult->item(0)->nodeValue); + $ort = str_replace("(zur Karte) ", "", $ort); + $ort = str_replace("Die vollständige Adresse der Immobilie erhalten Sie vom Anbieter.", "", $ort); + + // append new array entry + $data[] = array("id" => $id, + "date" => date('d.m. H:i'), + "desc" => $desc, + "descs" => $descs, + "bezug" => $bezug, + "mietew" => $mietew, + "mietek" => $mietek, + "ort" => $ort, + "zimmer" => $zimmer, + "qm" => $qm, + "rating" => 0, + "status" => "NEU", + "note" => ""); + + // send mail for new item + $mailcontent = "*" . $desc . "* \r\n\r\n" . + "Bezug: " . $bezug . "\r\n" . + "Miete: " . $mietew . "/" . $mietek . "\r\n" . + "Größe: " . $qm . "\r\n" . + "Zimmer: " . $zimmer . "\r\n" . + "Ort: " . $ort . "\r\n" . + "Link: https://www.immobilienscout24.de/expose/" . $id . "\r\n\r\n" . + "Übersicht: https://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"]; + mail($config['mail']['to'], "Neues Wohnungsinserat: " . $descs . "...", $mailcontent, "From: " . $config['mail']['from'] . "\r\nMIME-Version: 1.0\r\nContent-Type: text/plain; charset=UTF-8"); + + echo $id . " is new and has been downloaded.
\n"; + + } else { // entry is old + echo $id . " already exists.
\n"; + } + + } // END foreach &searchResult + } // END for loop through pages + + + echo "

Back to overview

\n"; + + write_db($db, $data); +} + +///////////////// +/// do = SHOW /// +///////////////// +function immo_show($db) { + global $data; + global $queryurl; + read_db($db); + + $hide = isset($_GET['hide']) ? $_GET['hide'] : "yes"; + ?> + + + + + Immobilienscout Search Helper + + + + +
+ +
+ + + + + + + + + + + + + + $row) { + $date[$key] = $row['date']; + $id[$key] = $row['id']; + } + array_multisort($date, SORT_DESC, $id, SORT_DESC, $data); + + $total = count($data); + for ($row = 0; $row < $total; $row++) { + $id = $data[$row]['id']; + $date = $data[$row]['date']; + $desc = $data[$row]['desc']; + $descs = $data[$row]['descs']; + $bezug = $data[$row]['bezug']; + $mietew = $data[$row]['mietew']; + $mietek = $data[$row]['mietek']; + $zimmer = $data[$row]['zimmer']; + $qm = $data[$row]['qm']; + $ort = $data[$row]['ort']; + $status = $data[$row]['status']; + $note = $data[$row]['note']; + $link = "https://www.immobilienscout24.de/expose/" . $id; + + if ($status !== "del" || $hide === "no") { + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + echo ""; + + echo "\n"; + } + } + + ?> +
DatumBeschreibungStatusBezugMiete warm (kalt)ZiGrößeOrtNotizStatus ändern
" . $date . "" . $descs . "..." . $status . "" . $bezug . "" . $mietew . " (" . $mietek . ")" . "" . $zimmer . "" . $qm . "" . $ort . ""; + ?> +
+ + + + +
+ "; + + echo "
"; + ?> +
+ + + + +
+ "; + + echo "
+ +

Letztes Update:

+

Gelöschte Inserate einblenden / ausblenden

+

Suchlink: Klick

+

Update

+ + + + "; + echo "
"; + echo "Back to overview\n"; + } else { + session_start(); // start Sessions + if ($status != "NONE") { + $data[$key]['status'] = $status; + save_return_page("Changed status of " . $id . " to \"". $status . "\"."); + } + if ($note != "NONE") { + $data[$key]['note'] = $note; + save_return_page("Changed note of " . $id . " to \"". $note . "\"."); + } + } +} + +//////////////////////// +/// HELPER FUNCTIONS /// +//////////////////////// +// load database from file and decode it to array $data +function read_db($db) { + global $data; // declare $data a global variable to access it outside this function + if (! file_exists($db)) { + touch($db); + } + $file = file_get_contents($db, true); + $data = json_decode($file, true); + unset($file); +} + +// Encode $data array and write database to file +function write_db($db, $data) { + // encode and write file + $encoded = json_encode($data, JSON_PRETTY_PRINT); + file_put_contents($db, $encoded, LOCK_EX); +} + +// load URL and transform it to a XPath-searchable document +function xsite($url) { + libxml_use_internal_errors(true); // suppress errors + $dl = file_get_contents($url); + $site = new DOMDocument(); + $site->loadHTML($dl); + $xpathvar = new Domxpath($site); + return $xpathvar; +} + +// save database, put message to session, and load refering page which shows the message +function save_return_page($output) { + global $db; + global $data; + write_db($db, $data); + $_SESSION['message'] = "Update: " . $output; + header('Location: ' . $_SERVER['HTTP_REFERER']); + exit; +} +?>