Changed to be run all the time, on/off switch in homeassistant controls notification
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
/vendor/
|
/vendor/
|
||||||
.env
|
.env
|
||||||
.idea
|
.idea
|
||||||
last_notification_date.txt
|
last_notification_date.txt
|
||||||
|
login_data.json
|
||||||
9
README.md
Normal file
9
README.md
Normal file
@@ -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
|
||||||
|
```
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use GuzzleHttp\Client;
|
|
||||||
use Kplaczek\FoodsiBot\BotService;
|
use Kplaczek\FoodsiBot\BotService;
|
||||||
use Kplaczek\FoodsiBot\FoodsiService;
|
use Kplaczek\FoodsiBot\FoodsiService;
|
||||||
use Kplaczek\FoodsiBot\HomeassistantService;
|
use Kplaczek\FoodsiBot\HomeassistantService;
|
||||||
@@ -13,7 +12,7 @@ $_ENV['DEBUG'] = false;
|
|||||||
if ($argc === 2 and isset($argv[1]) && $argv[1] === '--debug') {
|
if ($argc === 2 and isset($argv[1]) && $argv[1] === '--debug') {
|
||||||
$_ENV['DEBUG'] = true;
|
$_ENV['DEBUG'] = true;
|
||||||
}
|
}
|
||||||
$client = new Client();
|
$client = new GuzzleHttp\Client();
|
||||||
$homeassistant = new HomeassistantService($client);
|
$homeassistant = new HomeassistantService($client);
|
||||||
$foodsi = new FoodsiService($client);
|
$foodsi = new FoodsiService($client);
|
||||||
$bot = new BotService($homeassistant, $foodsi);
|
$bot = new BotService($homeassistant, $foodsi);
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ class BotService
|
|||||||
{
|
{
|
||||||
private bool $notificationSent = false;
|
private bool $notificationSent = false;
|
||||||
|
|
||||||
|
private const string LAST_NOTIFICATION_DATE_FILE = 'last_notification_date.txt';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private HomeassistantService $homeassistantService,
|
private HomeassistantService $homeassistantService,
|
||||||
private FoodsiService $foodsiService,
|
private FoodsiService $foodsiService,
|
||||||
@@ -20,9 +22,6 @@ class BotService
|
|||||||
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (!$this->isEnabled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$packageInfo = $this->foodsiService->getPackageInfo();
|
$packageInfo = $this->foodsiService->getPackageInfo();
|
||||||
foreach ($this->getReceivers() as $receiver) {
|
foreach ($this->getReceivers() as $receiver) {
|
||||||
$this->initializeNotificationLog();
|
$this->initializeNotificationLog();
|
||||||
@@ -60,24 +59,34 @@ class BotService
|
|||||||
|
|
||||||
private function initializeNotificationLog(): void
|
private function initializeNotificationLog(): void
|
||||||
{
|
{
|
||||||
if (!file_exists('last_notification_date.txt')) {
|
if (!file_exists(self::LAST_NOTIFICATION_DATE_FILE)) {
|
||||||
touch('last_notification_date.txt');
|
touch(self::LAST_NOTIFICATION_DATE_FILE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private function updateNotificationLog(): void
|
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
|
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
|
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
|
private function isDebugEnabled(): bool
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ readonly class FoodsiService
|
|||||||
private string $accessToken;
|
private string $accessToken;
|
||||||
private string $client;
|
private string $client;
|
||||||
private string $uid;
|
private string $uid;
|
||||||
|
private const string LOGIN_DATA_FILE = 'login_data.json';
|
||||||
|
|
||||||
public function __construct(private Client $httpClient)
|
public function __construct(private Client $httpClient)
|
||||||
{
|
{
|
||||||
@@ -18,11 +19,11 @@ readonly class FoodsiService
|
|||||||
{
|
{
|
||||||
$this->login();
|
$this->login();
|
||||||
$response = $this->httpClient->get(
|
$response = $this->httpClient->get(
|
||||||
'https://api.foodsi.pl/api/v3/user/offers?filter[venue_name][]=Trzy kromki chleba&filter[current_quantity][gt]=-1'.
|
'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][gt]=' . date('Y-m-dT00:00:00+01:00') .
|
||||||
'&filter[pickup_from][lt]='.date('Y-m-dT22: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][gt]=' . date('Y-m-dT00:00:00+01:00') .
|
||||||
'&filter[pickup_to][lt]='.date('Y-m-dT22:00:00+01:00').
|
'&filter[pickup_to][lt]=' . date('Y-m-dT22:00:00+01:00') .
|
||||||
'&filter[original_price]=35',
|
'&filter[original_price]=35',
|
||||||
[
|
[
|
||||||
'headers' => [
|
'headers' => [
|
||||||
@@ -32,7 +33,6 @@ readonly class FoodsiService
|
|||||||
],
|
],
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
|
|
||||||
$data = json_decode($response->getBody(), true);
|
$data = json_decode($response->getBody(), true);
|
||||||
if (isset($data['data'][0])) {
|
if (isset($data['data'][0])) {
|
||||||
return $data['data'][0]['attributes'];
|
return $data['data'][0]['attributes'];
|
||||||
@@ -43,6 +43,7 @@ readonly class FoodsiService
|
|||||||
|
|
||||||
private function login(): void
|
private function login(): void
|
||||||
{
|
{
|
||||||
|
$this->restoreLoginData();
|
||||||
if (empty($this->accessToken)) {
|
if (empty($this->accessToken)) {
|
||||||
$response = $this->httpClient->post('https://api.foodsi.pl/api/v2/auth/sign_in', [
|
$response = $this->httpClient->post('https://api.foodsi.pl/api/v2/auth/sign_in', [
|
||||||
'json' => [
|
'json' => [
|
||||||
@@ -53,6 +54,30 @@ readonly class FoodsiService
|
|||||||
$this->accessToken = $response->getHeaderLine('Access-Token');
|
$this->accessToken = $response->getHeaderLine('Access-Token');
|
||||||
$this->client = $response->getHeaderLine('Client');
|
$this->client = $response->getHeaderLine('Client');
|
||||||
$this->uid = $response->getHeaderLine('Uid');
|
$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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user