From a262679916c444956f6b86c3a4dcd0df65bf3271 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Mon, 11 Nov 2024 16:22:29 +0100 Subject: [PATCH] Split files into smaller classed. Add enable_bot.php file to enable bot daily. --- enable_bot.php | 11 +++++++ index.php | 56 ++++++++++-------------------------- src/FoodsiService.php | 52 +++++++++++++++++++++++++++++++++ src/HomeassistantService.php | 43 +++++++++++++++++++++++++++ 4 files changed, 121 insertions(+), 41 deletions(-) create mode 100644 enable_bot.php create mode 100644 src/FoodsiService.php create mode 100644 src/HomeassistantService.php diff --git a/enable_bot.php b/enable_bot.php new file mode 100644 index 0000000..f9207bf --- /dev/null +++ b/enable_bot.php @@ -0,0 +1,11 @@ +load(); +$client = new Client(); +$homeassistant = new HomeassistantService($client); +$homeassistant->updateEntity('input_boolean.foodsi_bot_enabled', 'on'); diff --git a/index.php b/index.php index 96893b9..c999106 100644 --- a/index.php +++ b/index.php @@ -1,51 +1,20 @@ load(); $client = new Client(); -function send_notifications(Client $client, string $receiver, string $title, string $body): void -{ - $client->post(sprintf('%s/api/services/notify/%s', $_ENV['HOMEASSISTANT_URL'], $receiver), [ - 'headers' => ['Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN']], - 'json' => ['message' => $body, 'title' => $title] - ]); +$homeassistant = new HomeassistantService($client); +if ($homeassistant->getEntityInfo('input_boolean.foodsi_bot_enabled')['state'] === 'off') { + return; } - -function update_homeassistent_entity(Client $client, array $package_info): void -{ - $client->post(sprintf('%s/api/states/sensor.foodsi_3kromki_large', $_ENV['HOMEASSISTANT_URL']), [ - 'headers' => ['Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN']], - 'json' => ['state' => $package_info['current_quantity']] - ]); -} - -function get_foodsie_package_info(Client $client): array -{ - $response = $client->post('https://api.foodsi.pl/api/v2/auth/sign_in', [ - 'json' => [ - 'email' => $_ENV['FOODSI_EMAIL'], - 'password' => $_ENV['FOODSI_PASSWORD'], - ], - ]); - $response = $client->get( - 'https://api.foodsi.pl/api/v3/user/offers?filter[venue_name][]=Trzy kromki chleba&filter[active]=true&filter[current_quantity][gt]=-1&filter[id]=6052730', - [ - 'headers' => [ - 'Access-Token' => $response->getHeader('Access-Token'), - 'Client' => $response->getHeader('Client'), - 'Uid' => $response->getHeader('Uid'), - ], - ], - ); - $data = json_decode($response->getBody(), true); - - return $data['data'][0]['attributes']; -} - -$packageInfo = get_foodsie_package_info($client); +$foodsi = new FoodsiService($client); +$packageInfo = $foodsi->getPackageInfo(); $receivers = explode(',', $_ENV['HOMEASSISTANT_NOTIFICATION_CLIENTS']); $notificationSent = false; foreach ($receivers as $receiver) { @@ -54,11 +23,16 @@ foreach ($receivers as $receiver) { } $lastNotificationDate = file_get_contents('last_notification_date.txt'); if ($packageInfo['current_quantity'] > 0 && $lastNotificationDate !== date('Y-m-d')) { - send_notifications($client, $receiver, 'Dostępna paczka w 3 kromki chleba', sprintf('W trzech kromkach dostępne są paczki. Ilość: %s', $packageInfo['current_quantity'])); + $homeassistant->sendNotification( + $receiver, + 'Dostępna paczka w 3 kromki chleba', + sprintf('W trzech kromkach dostępne są paczki. Ilość: %s', $packageInfo['current_quantity']), + ); $notificationSent = true; } } if ($notificationSent) { file_put_contents('last_notification_date.txt', date('Y-m-d')); + $homeassistant->updateEntity('input_boolean.foodsi_bot_enabled', 'off'); } -update_homeassistent_entity($client, $packageInfo); +$homeassistant->updateEntity('sensor.foodsi_3kromki_large', $packageInfo['current_quantity'], ['update' => date('Y-m-d H:i:s')]); diff --git a/src/FoodsiService.php b/src/FoodsiService.php new file mode 100644 index 0000000..26c631c --- /dev/null +++ b/src/FoodsiService.php @@ -0,0 +1,52 @@ +login(); + $response = $this->httpClient->get( + 'https://api.foodsi.pl/api/v3/user/offers?filter[venue_name][]=Trzy kromki chleba&filter[active]=true&filter[current_quantity][gt]=-1&filter[original_price]=35', + [ + 'headers' => [ + 'Access-Token' => $this->accessToken, + 'Client' => $this->client, + 'Uid' => $this->uid, + ], + ], + ); + $data = json_decode($response->getBody(), true); + if (isset($data['data'][0])) { + return $data['data'][0]['attributes']; + } + + return []; + } + + private function login(): void + { + if (empty($this->accessToken)) { + $response = $this->httpClient->post('https://api.foodsi.pl/api/v2/auth/sign_in', [ + 'json' => [ + 'email' => $_ENV['FOODSI_EMAIL'], + 'password' => $_ENV['FOODSI_PASSWORD'], + ], + ]); + $this->accessToken = $response->getHeaderLine('Access-Token'); + $this->client = $response->getHeaderLine('Client'); + $this->uid = $response->getHeaderLine('Uid'); + } + } +} \ No newline at end of file diff --git a/src/HomeassistantService.php b/src/HomeassistantService.php new file mode 100644 index 0000000..118d2bc --- /dev/null +++ b/src/HomeassistantService.php @@ -0,0 +1,43 @@ +client->get(sprintf('%s/api/states/%s', $_ENV['HOMEASSISTANT_URL'], $entityId), [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN'], + ], + ]); + + return json_decode($response->getBody()->getContents(), true); + } + + public function updateEntity(string $entityId, string $state, array $attributes = []): void + { + $this->client->post( + sprintf('%s/api/states/%s', $_ENV['HOMEASSISTANT_URL'], $entityId), [ + 'headers' => ['Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN']], + 'json' => ['state' => $state, 'attributes' => $attributes], + ] + ); + } + + function sendNotification(string $receiver, string $title, string $body): void + { + $this->client->post(sprintf('%s/api/services/notify/%s', $_ENV['HOMEASSISTANT_URL'], $receiver), [ + 'headers' => [ + 'Authorization' => 'Bearer ' . $_ENV['HOMEASSISTANT_TOKEN'], + ], + 'json' => ['message' => $body, 'title' => $title] + ]); + } +} \ No newline at end of file