From 8d23c68812cdbcd93064722ad111245ed35dc53b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krzysztof=20P=C5=82aczek?= Date: Sun, 26 Jan 2025 09:28:30 +0100 Subject: [PATCH] Changed to be run all the time, on/off switch in homeassistant controls notification --- .gitignore | 3 ++- README.md | 9 +++++++++ index.php | 3 +-- src/BotService.php | 25 +++++++++++++++++-------- src/FoodsiService.php | 37 +++++++++++++++++++++++++++++++------ 5 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 README.md diff --git a/.gitignore b/.gitignore index c47b43f..7e4a56a 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ /vendor/ .env .idea -last_notification_date.txt \ No newline at end of file +last_notification_date.txt +login_data.json \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..408ff5a --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +## FOODSI HOMEASSISTANT BOT + +### Run in debug mode + +`--debug` flag does not update Homeassistant, does not send notifications it just prints out current number of packages on the screen. + +```bash +php index.php --debug +``` diff --git a/index.php b/index.php index 354776f..3beb012 100644 --- a/index.php +++ b/index.php @@ -1,6 +1,5 @@ isEnabled()) { - return; - } $packageInfo = $this->foodsiService->getPackageInfo(); foreach ($this->getReceivers() as $receiver) { $this->initializeNotificationLog(); @@ -60,24 +59,34 @@ class BotService private function initializeNotificationLog(): void { - if (!file_exists('last_notification_date.txt')) { - touch('last_notification_date.txt'); + if (!file_exists(self::LAST_NOTIFICATION_DATE_FILE)) { + touch(self::LAST_NOTIFICATION_DATE_FILE); } } private function updateNotificationLog(): void { - file_put_contents('last_notification_date.txt', date('Y-m-d')); + file_put_contents(self::LAST_NOTIFICATION_DATE_FILE, date('Y-m-d')); } private function getNotificationLog(): string { - return file_get_contents('last_notification_date.txt'); + return file_get_contents(self::LAST_NOTIFICATION_DATE_FILE); } private function shouldSendNotification(array $packageInfo): bool { - return !$this->isDebugEnabled() && $packageInfo['current_quantity'] > 0 && $this->getNotificationLog() !== date('Y-m-d'); + return !$this->isDebugEnabled() && $this->isEnabled() && $this->isPackageAvailable($packageInfo) && !$this->isPackageNotificationSentToday(); + } + + private function isPackageAvailable(array $packageInfo): bool + { + return $packageInfo['current_quantity'] > 0; + } + + private function isPackageNotificationSentToday(): bool + { + return $this->getNotificationLog() !== date('Y-m-d'); } private function isDebugEnabled(): bool diff --git a/src/FoodsiService.php b/src/FoodsiService.php index 33c14a3..458155c 100644 --- a/src/FoodsiService.php +++ b/src/FoodsiService.php @@ -9,6 +9,7 @@ readonly class FoodsiService private string $accessToken; private string $client; private string $uid; + private const string LOGIN_DATA_FILE = 'login_data.json'; public function __construct(private Client $httpClient) { @@ -18,11 +19,11 @@ 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[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'). + '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' => [ @@ -32,7 +33,6 @@ readonly class FoodsiService ], ], ); - $data = json_decode($response->getBody(), true); if (isset($data['data'][0])) { return $data['data'][0]['attributes']; @@ -43,6 +43,7 @@ readonly class FoodsiService private function login(): void { + $this->restoreLoginData(); if (empty($this->accessToken)) { $response = $this->httpClient->post('https://api.foodsi.pl/api/v2/auth/sign_in', [ 'json' => [ @@ -53,6 +54,30 @@ readonly class FoodsiService $this->accessToken = $response->getHeaderLine('Access-Token'); $this->client = $response->getHeaderLine('Client'); $this->uid = $response->getHeaderLine('Uid'); + $this->cacheLoginData(); } } + + private function restoreLoginData(): void + { + if (file_exists(self::LOGIN_DATA_FILE)) { + $loginData = json_decode(file_get_contents(self::LOGIN_DATA_FILE), 1); + if ($loginData['time'] + 3600 > time()) { + $this->accessToken = $loginData['Access-Token']; + $this->client = $loginData['Client']; + $this->uid = $loginData['Uid']; + } + } + } + + private function cacheLoginData(): void + { + $loginData = [ + 'Access-Token' => $this->accessToken, + 'Client' => $this->client, + 'Uid' => $this->uid, + 'time' => time(), + ]; + file_put_contents(self::LOGIN_DATA_FILE, json_encode($loginData)); + } } \ No newline at end of file