From d99e62ed9907acf8e4317d508b82b681dc912a28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hauke=20Z=C3=BChl?= Date: Thu, 5 Mar 2020 19:30:03 +0100 Subject: [PATCH] Worker function, saving data into file --- php/muell.php | 68 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/php/muell.php b/php/muell.php index 242c7be..37f11e5 100755 --- a/php/muell.php +++ b/php/muell.php @@ -18,8 +18,39 @@ * tgBotOwner: */ +function worker(string $response) +{ + $tomorrow = (new DateTime('tomorrow'))->format('d.m.Y'); + $trashArr = str_getcsv($response, "\n"); + + foreach($trashArr as $row) { + $rowArr = str_getcsv($row, ';'); + $index = array_search($tomorrow, $rowArr); + if ($index !== false) { + $what = ''; + switch ($index) { + case 0: + case 1: + $what = "werden Biomüll und gelbe Tonne"; + break; + case 2: + $what = "wird Restmüll"; + break; + case 3: + $what = "wird Papier"; + break; + } + if ($what !== '') { + shell_exec(sprintf("echo Morgen %s abgeholt | %s -u %d", $what, $config['pathBot'], $config['tgReceiver'])); + } + } + } +} + +// Main part $currentYear = date('Y'); $dateRange = sprintf('%s0101-%s1231', $currentYear, $currentYear); +$muellFile = sprintf('%s/muell.csv', __DIR__); $key = 'e21758b9c711463552fb9c70ac7d4273'; @@ -59,37 +90,20 @@ curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla 70'); -$response = curl_exec($ch); +$response = curl_exec($ch)."\n"; curl_close($ch); if ($response !== false) { - $tomorrow = (new DateTime('tomorrow'))->format('d.m.Y'); - $trashArr = str_getcsv($response, "\n"); - - foreach($trashArr as $row) { - $rowArr = str_getcsv($row, ';'); - $index = array_search($tomorrow, $rowArr); - if ($index !== false) { - $what = ''; - switch ($index) { - case 0: - case 1: - $what = "werden Biomüll und gelbe Tonne"; - break; - case 2: - $what = "wird Restmüll"; - break; - case 3: - $what = "wird Papier"; - break; - } - if ($what !== '') { - shell_exec(sprintf("echo Morgen %s abgeholt | %s -u %d", $what, $config['pathBot'], $config['tgReceiver'])); - } - } - } + // If result file does not exist, write it to have a copy + file_put_contents($muellFile, $response); + // Go on with checking the result data + worker($response); } else { - shell_exec(sprintf("echo Müllbot konnte keine Daten empfangen | %s -u %d", $config['pathBot'], $config['tgBotOwner'])); + if (file_exists($muellFile)) { + worker(file_get_contents($muellFile)); + } else { + shell_exec(sprintf("echo Müllbot konnte keine Daten empfangen | %s -u %d", $config['pathBot'], $config['tgBotOwner'])); + } }