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

Commit

Permalink
refactor!: add for multiple API
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 5, 2025
1 parent f0b895b commit 040fee2
Show file tree
Hide file tree
Showing 16 changed files with 218 additions and 74 deletions.
59 changes: 54 additions & 5 deletions schemas/config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
{
"$defs": {
"LogLevelDTO": {
"ApiEnum": {
"enum": [
"checkphatnguoi.vn",
"csgt.vn"
],
"title": "ApiEnum",
"type": "string"
},
"LogLevelEnum": {
"enum": [
"NOTSET",
"DEBUG",
Expand All @@ -9,7 +17,7 @@
"ERROR",
"CRITICAL"
],
"title": "LogLevelDTO",
"title": "LogLevelEnum",
"type": "string"
},
"PlateInfoDTO": {
Expand Down Expand Up @@ -41,6 +49,32 @@
"dad"
],
"title": "Ghi chú chủ sở hữu"
},
"type": {
"anyOf": [
{
"$ref": "#/$defs/VehicleTypeEnum"
},
{
"type": "null"
}
],
"default": null,
"description": "Loại phương tiện. Khi sử dụng API \"checkphatnguoi_vn\" không cần trường này",
"title": "Loại phương tiện"
},
"api": {
"anyOf": [
{
"$ref": "#/$defs/ApiEnum"
},
{
"type": "null"
}
],
"default": null,
"description": "Sử dụng API từ trang web nào (để trống sẽ sử dụng API define ở scope ngoài)",
"title": "API"
}
},
"required": [
Expand Down Expand Up @@ -92,15 +126,24 @@
],
"title": "TelegramNotifyDTO",
"type": "object"
},
"VehicleTypeEnum": {
"enum": [
1,
2,
3
],
"title": "VehicleTypeEnum",
"type": "integer"
}
},
"properties": {
"data": {
"plates": {
"description": "Danh sách các biển xe",
"items": {
"$ref": "#/$defs/PlateInfoDTO"
},
"title": "Data",
"title": "Plates",
"type": "array"
},
"notifications": {
Expand All @@ -111,6 +154,12 @@
"title": "Notifications",
"type": "array"
},
"api": {
"$ref": "#/$defs/ApiEnum",
"default": "checkphatnguoi.vn",
"description": "Sử dụng API từ trang web nào (mặc định sử dụng API từ trang checkphatnguoi.vn)",
"title": "API"
},
"unpaid_only": {
"default": true,
"description": "Chỉ hiển thị thông tin vi phạm chưa nộp phạt",
Expand All @@ -129,7 +178,7 @@
"type": "boolean"
},
"log_level": {
"$ref": "#/$defs/LogLevelDTO",
"$ref": "#/$defs/LogLevelEnum",
"default": "INFO"
}
},
Expand Down
7 changes: 3 additions & 4 deletions src/check_phat_nguoi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from check_phat_nguoi.config.dto.notify.base_notify import BaseNotifyDTO
from check_phat_nguoi.config.dto.notify.telegram_notify import TelegramNotifyDTO
from check_phat_nguoi.context import PlatesModel
from check_phat_nguoi.get_data.check_phat_nguoi import GetDataCheckPhatNguoi
from check_phat_nguoi.get_data import GetData
from check_phat_nguoi.notify.message import Message
from check_phat_nguoi.notify.noti_engine import NotificationEngine
from check_phat_nguoi.notify.telegram import Telegram
Expand All @@ -17,9 +17,8 @@

async def async_main() -> None:
setup_logger()
plates: PlatesModel = PlatesModel(
plates=await GetDataCheckPhatNguoi(plate_infos=config.data).get_data()
)
async with GetData(plates=config.plates) as get_data:
plates: PlatesModel = PlatesModel(plates=await get_data.get_data())
message_dict = Message(plates=plates).format_messages()
notifications: filter[BaseNotifyDTO] = filter(
lambda notify: notify.enabled, config.notifications
Expand Down
1 change: 0 additions & 1 deletion src/check_phat_nguoi/config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
"config",
"ConfigDTO",
"PlateInfoDTO",
"LogLevelDTO",
"TelegramNotifyDTO",
]
9 changes: 7 additions & 2 deletions src/check_phat_nguoi/config/dto/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
from .api import ApiEnum
from .config import ConfigDTO
from .log_level import LogLevelDTO
from .notify import *
from .plate_info import PlateInfoDTO

__all__ = ["ConfigDTO", "PlateInfoDTO", "LogLevelDTO", "TelegramNotifyDTO"]
__all__ = [
"ConfigDTO",
"PlateInfoDTO",
"TelegramNotifyDTO",
"ApiEnum",
]
9 changes: 9 additions & 0 deletions src/check_phat_nguoi/config/dto/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from enum import Enum


class ApiEnum(str, Enum):
checkphatnguoi_vn = "checkphatnguoi.vn"
csgt_vn = "csgt.vn"


__all__ = ["ApiEnum"]
28 changes: 24 additions & 4 deletions src/check_phat_nguoi/config/dto/config.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
from pydantic import BaseModel, ConfigDict, Field
from typing import Self

from .log_level import LogLevelDTO
from pydantic import BaseModel, ConfigDict, Field, model_validator

from check_phat_nguoi.enums import LogLevelEnum

from .api import ApiEnum
from .notify.telegram_notify import TelegramNotifyDTO
from .plate_info import PlateInfoDTO


class ConfigDTO(BaseModel):
model_config = ConfigDict(use_enum_values=True, validate_default=True)

data: tuple[PlateInfoDTO, ...] = Field(
plates: tuple[PlateInfoDTO, ...] = Field(
description="Danh sách các biển xe", default_factory=tuple
)
notifications: tuple[TelegramNotifyDTO, ...] = Field(
description="Danh sách các thiết lập để thông báo", default_factory=tuple
)
api: ApiEnum = Field(
description="Sử dụng API từ trang web nào (mặc định sử dụng API từ trang checkphatnguoi.vn)",
title="API",
default=ApiEnum.checkphatnguoi_vn,
)
unpaid_only: bool = Field(
description="Chỉ hiển thị thông tin vi phạm chưa nộp phạt", default=True
)
verbose: bool = Field(
description="Hiển thị tất cả thông tin có thể hiển thị", default=False
)
detail_log: bool = True
log_level: LogLevelDTO = LogLevelDTO.info
log_level: LogLevelEnum = LogLevelEnum.info

@model_validator(mode="after")
def validate_type(self) -> Self:
for plate in self.plates:
if plate.api is None:
if self.api not in (ApiEnum.checkphatnguoi_vn,):
raise ValueError(
f'Plate {plate}: API other than "checkphatnguoi_vn" must set "type"'
)
plate.api = self.api
return self


__all__ = ["ConfigDTO"]
14 changes: 14 additions & 0 deletions src/check_phat_nguoi/config/dto/plate_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

from pydantic import BaseModel, Field

from check_phat_nguoi.enums import VehicleTypeEnum

from .api import ApiEnum


class PlateInfoDTO(BaseModel):
plate: str = Field(
Expand All @@ -15,6 +19,16 @@ class PlateInfoDTO(BaseModel):
examples=["@kevinnitro", "dad"],
default=None,
)
type: VehicleTypeEnum | None = Field(
description='Loại phương tiện. Khi sử dụng API "checkphatnguoi_vn" không cần trường này',
title="Loại phương tiện",
default=None,
)
api: ApiEnum | None = Field(
description="Sử dụng API từ trang web nào (để trống sẽ sử dụng API define ở scope ngoài)",
title="API",
default=None,
)

@override
def __hash__(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
from pydantic import BaseModel, Field

from check_phat_nguoi.enums import VehicleTypeEnum

from .violation import ViolationModel


class PlateInfoModel(BaseModel):
plate: str
owner: str | None
type: VehicleTypeEnum | None
violation: tuple[ViolationModel, ...] = Field(
description="Danh sách các vi phạm của 1 biển xe", default_factory=tuple
)
Expand Down
4 changes: 4 additions & 0 deletions src/check_phat_nguoi/enums/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .log_level import LogLevelEnum
from .vehicle_type import VehicleTypeEnum

__all__ = ["VehicleTypeEnum", "LogLevelEnum"]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from enum import Enum


class LogLevelDTO(str, Enum):
class LogLevelEnum(str, Enum):
notset = "NOTSET"
debug = "DEBUG"
info = "INFO"
Expand All @@ -10,4 +10,4 @@ class LogLevelDTO(str, Enum):
critical = "CRITICAL"


__all__ = ["LogLevelDTO"]
__all__ = ["LogLevelEnum"]
10 changes: 10 additions & 0 deletions src/check_phat_nguoi/enums/vehicle_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from enum import IntEnum


class VehicleTypeEnum(IntEnum):
car = 1
motorbike = 2
electric_motorbike = 3


__all__ = ["VehicleTypeEnum"]
4 changes: 2 additions & 2 deletions src/check_phat_nguoi/get_data/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .check_phat_nguoi import *
from .get_data import GetData

__all__ = ["GetDataCheckPhatNguoi"]
__all__ = ["GetData"]
22 changes: 22 additions & 0 deletions src/check_phat_nguoi/get_data/engine_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from abc import abstractmethod
from typing import Self

from aiohttp import ClientSession

from check_phat_nguoi.config import PlateInfoDTO
from check_phat_nguoi.context import PlateInfoModel


class GetDataEngineBase:
def __init__(self) -> None:
self.session: ClientSession = ClientSession()

async def __aenter__(self) -> Self:
return self

@abstractmethod
async def __aexit__(self, exc_type, exc_value, exc_traceback) -> None:
await self.session.close()

@abstractmethod
async def get_data(self, plate: PlateInfoDTO) -> PlateInfoModel | None: ...
Loading

0 comments on commit 040fee2

Please sign in to comment.