From a98934dfad9036ebef981581debe3355fab8a59a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Sun, 15 Dec 2024 11:10:30 +0100 Subject: [PATCH] Add --debug flag to index.php script, restrict foodsi bot query to get only today packages. --- index.php | 4 ++++ src/BotService.php | 21 +++++++++++++++++++-- src/FoodsiService.php | 8 +++++++- 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/index.php b/index.php index 72c41e0..354776f 100644 --- a/index.php +++ b/index.php @@ -9,6 +9,10 @@ require_once 'vendor/autoload.php'; date_default_timezone_set('Europe/Warsaw'); $dotenv = Dotenv\Dotenv::createImmutable(__DIR__); $dotenv->load(); +$_ENV['DEBUG'] = false; +if ($argc === 2 and isset($argv[1]) && $argv[1] === '--debug') { + $_ENV['DEBUG'] = true; +} $client = new Client(); $homeassistant = new HomeassistantService($client); $foodsi = new FoodsiService($client); diff --git a/src/BotService.php b/src/BotService.php index b2ee43e..b38f3c2 100644 --- a/src/BotService.php +++ b/src/BotService.php @@ -14,6 +14,12 @@ class BotService public function run(): void { + if ($this->isDebugEnabled()) { + $packageInfo = $this->foodsiService->getPackageInfo(); + print_r($packageInfo); + + return; + } if (!$this->isEnabled()) { return; } @@ -29,13 +35,19 @@ class BotService $this->notificationSent = true; } } - if ($this->notificationSent) { + if ($this->notificationSent && !$this->isDebugEnabled()) { $this->updateNotificationLog(); $this->homeassistantService->updateEntity('input_boolean.foodsi_bot_enabled', 'off'); } $this->homeassistantService->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]); } + public function updateHomeAssistant(): void + { + $packageInfo = $this->foodsiService->getPackageInfo(); + $this->homeassistantService->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]); + } + private function isEnabled(): bool { return $this->homeassistantService->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'on'; @@ -65,6 +77,11 @@ class BotService private function shouldSendNotification(array $packageInfo): bool { - return $packageInfo['current_quantity'] > 0 && $this->getNotificationLog() !== date('Y-m-d'); + return !$this->isDebugEnabled() && $packageInfo['current_quantity'] > 0 && $this->getNotificationLog() !== date('Y-m-d'); + } + + private function isDebugEnabled(): bool + { + return $_ENV['DEBUG']; } } diff --git a/src/FoodsiService.php b/src/FoodsiService.php index e21ac54..c6a301d 100644 --- a/src/FoodsiService.php +++ b/src/FoodsiService.php @@ -18,7 +18,12 @@ readonly class FoodsiService { $this->login(); $response = $this->httpClient->get( - 'https://api.foodsi.pl/api/v3/user/offers?filter[venue_name][]=Trzy kromki chleba&filter[current_quantity][gt]=-1&filter[original_price]=35', + 'https://api.foodsi.pl/api/v3/user/offers?filter[venue_name][]=Trzy kromki chleba&filter[current_quantity][gt]=-1'. + '&filter[pickup_from][gt]='.date('Y-m-dT00:00:00+01:00'). + '&filter[pickup_from][lt]='.date('Y-m-dT22:00:00+01:00'). + '&filter[pickup_to][gt]='.date('Y-m-dT00:00:00+01:00'). + '&filter[pickup_to][lt]='.date('Y-m-dT22:00:00+01:00'). + '&filter[original_price]=35', [ 'headers' => [ 'Access-Token' => $this->accessToken, @@ -27,6 +32,7 @@ readonly class FoodsiService ], ], ); + $data = json_decode($response->getBody(), true); if (isset($data['data'][0])) { return $data['data'][0]['attributes'];