Skip to content
This repository has been archived by the owner on Jan 18, 2025. It is now read-only.

Commit

Permalink
refactor: modify log msg, todo for Nguyen to print plates
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 4, 2025
1 parent aeb9a2a commit f0b895b
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 13 deletions.
9 changes: 4 additions & 5 deletions src/check_phat_nguoi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
logger: Logger = getLogger(__name__)


async def _main():
async def async_main() -> None:
setup_logger()
logger.debug(config)
plates: PlatesModel = PlatesModel(
plates=await GetDataCheckPhatNguoi(config.data).get_data()
plates=await GetDataCheckPhatNguoi(plate_infos=config.data).get_data()
)
message_dict = Message(plates=plates).format_messages()
notifications: filter[BaseNotifyDTO] = filter(
Expand All @@ -34,8 +33,8 @@ async def _main():
await noti_engine.send_messages()


def main():
asyncio.run(_main())
def main() -> None:
asyncio.run(async_main())


__all__ = ["main"]
6 changes: 5 additions & 1 deletion src/check_phat_nguoi/config/dto/plate_info.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Any, override

from pydantic import BaseModel, Field


Expand All @@ -14,10 +16,12 @@ class PlateInfoDTO(BaseModel):
default=None,
)

@override
def __hash__(self):
return hash(self.plate)

def __eq__(self, other):
@override
def __eq__(self, other: Any):
if isinstance(other, PlateInfoDTO):
return self.plate == other.plate
return False
Expand Down
6 changes: 6 additions & 0 deletions src/check_phat_nguoi/context/plate_context/models/plate.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import override

from pydantic import BaseModel, Field

from .plate_info import PlateInfoModel
Expand All @@ -8,5 +10,9 @@ class PlatesModel(BaseModel):
description="Danh sách các biển xe", default_factory=tuple
)

# TODO: @NTGNguyen this is for print(plates: list[PlatesModel]), in order to print out as string to stdout in main
@override
def __str__(self) -> str: ...


__all__ = ["PlatesModel"]
6 changes: 3 additions & 3 deletions src/check_phat_nguoi/get_data/check_phat_nguoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ async def _get_data_request(self, plate_info_object: PlateInfoDTO) -> None:
response_data = await response.read()
response_data = json.loads(response_data)
self.data_dict[plate_info_object] = response_data
logger.debug(f"Successfully get data for plate: {plate_info_object.plate}")
logger.info(f"Plate {plate_info_object.plate}: Get data successfully")
except asyncio.TimeoutError:
logger.error(
f"Time out of {self.timeout} seconds from URL {API_URL} for plate: {plate_info_object.plate}"
f"Plate {plate_info_object.plate}: Time out ({self.timeout}s) getting data from API {API_URL}"
)
except ClientConnectionError:
logger.error(
f"Error occurs while connecting to {API_URL} for plate: {plate_info_object.plate}"
f"Plate {plate_info_object.plate}: Error occurs while getting data from API {API_URL}"
)

async def _get_data(self) -> None:
Expand Down
8 changes: 4 additions & 4 deletions src/check_phat_nguoi/notify/telegram.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ async def _send_message(self, message: str, plate: str) -> None:
url, json=payload, timeout=ClientTimeout(self.timeout)
) as response:
response.raise_for_status()
logger.debug(
f"Sending message completed for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token} with plate:{plate}"
logger.info(
f"Plate {plate}: Successfully sent to Telegram Chat ID: {self._telegram.chat_id}"
)
except asyncio.TimeoutError:
logger.error(
f"Time out of {self.timeout} seconds for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token} with plate{plate}"
f"Plate {plate}: Timeout ({self.timeout}s) sending to Telegram Chat ID: {self._telegram.chat_id}"
)
except ClientConnectionError:
logger.error(
f"Unable to sending message for chat_id:{self._telegram.chat_id} and bot_token:{self._telegram.bot_token} with plate{plate}"
f"Plate {plate}: Fail to sent to Telegram Chat ID: {self._telegram.chat_id}"
)
except Exception as e:
logger.error(e)
Expand Down

0 comments on commit f0b895b

Please sign in to comment.