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

Commit

Permalink
refactor: idk too much
Browse files Browse the repository at this point in the history
fix #40
  • Loading branch information
KevinNitroG committed Jan 15, 2025
1 parent 02551e2 commit b4419bd
Show file tree
Hide file tree
Showing 24 changed files with 245 additions and 1,835 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ authors = [
description = "Check phạt nguội"
name = "check-phat-nguoi"
version = "0.1.0-dev.1"
requires-python = ">=3.10"
requires-python = ">=3.13"
dependencies = [
"aiohttp[speedups]>=3.11.11",
"beautifulsoup4>=4.12.3",
Expand Down
163 changes: 5 additions & 158 deletions requirements/requirements-build-website.txt

Large diffs are not rendered by default.

499 changes: 11 additions & 488 deletions requirements/requirements-dev.txt

Large diffs are not rendered by default.

470 changes: 10 additions & 460 deletions requirements/requirements.txt

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/check_phat_nguoi/__main__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
from logging import getLogger

from check_phat_nguoi.config.config_reader import config
from check_phat_nguoi.config_reader import config
from check_phat_nguoi.context import plates_context
from check_phat_nguoi.get_data import GetData
from check_phat_nguoi.notify import SendNotifications
Expand Down
3 changes: 0 additions & 3 deletions src/check_phat_nguoi/config/exceptions/__init__.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

from pydantic import ValidationError

from check_phat_nguoi.constants.config import CONFIG_PATHS

from .models import Config
from check_phat_nguoi.config import Config
from check_phat_nguoi.constants import CONFIG_PATHS


def _config_reader() -> Config:
Expand Down
4 changes: 2 additions & 2 deletions src/check_phat_nguoi/context/plates/models/plate_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ def create_violation_str(violation: ViolationDetail, index: int) -> str:
+ "\n"
+ "\n".join(
resolution_office_detail.strip()
for resolution_office_detail in violation.resolution_offices_details
for resolution_office_detail in violation.resolution_offices
)
if violation.resolution_offices_details
if violation.resolution_offices
else None
)
return violation_str + (resolution_offices if resolution_offices else "")
Expand Down
18 changes: 16 additions & 2 deletions src/check_phat_nguoi/context/plates/models/violation_detail.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,29 @@
from datetime import datetime
from typing import override

from pydantic import BaseModel, Field
from pydantic import BaseModel, ConfigDict, Field

from check_phat_nguoi.types import VehicleTypeEnum


# NOTE: This class is used to store the get data's reponse. So it has None type in each field
class ViolationDetail(BaseModel):
model_config = ConfigDict(
frozen=True,
)

plate: str | None = Field(
description="Biển định danh được trả về từ API",
default=None,
)
color: str | None = Field(
description="Màu biển",
default=None,
)
type: VehicleTypeEnum | None = Field(
description="Màu biển",
default=None,
)
date: datetime | None = Field(
description="Thời điểm vi phạm",
default=None,
Expand All @@ -32,7 +46,7 @@ class ViolationDetail(BaseModel):
description="Đơn vị phát hiện vi phạm",
default=None,
)
resolution_offices_details: tuple[str, ...] | None = Field(
resolution_offices: tuple[str, ...] | None = Field(
description="Nơi giải quyết vụ việc",
default=None,
)
Expand Down
101 changes: 61 additions & 40 deletions src/check_phat_nguoi/get_data/engines/check_phat_nguoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,52 +30,71 @@
logger = getLogger(__name__)


class _CheckPhatNguoiGetDataParseEngine:
def __init__(self, plate_info: PlateInfo, plate_detail_dict: dict) -> None:
self._plate_info: PlateInfo = plate_info
self._plate_detail_dict: dict = plate_detail_dict
self._violations_details_set: set[ViolationDetail] = set()

def _parse_violation(self, violation_dict: dict) -> None:
type: VehicleStrVieType | None = violation_dict["Loại phương tiện"]
# NOTE: this is for filtering the vehicle that doesn't match the plate info type. Because checkphatnguoi.vn return all of the type of the plate
parsed_type: VehicleTypeEnum = get_vehicle_enum(type)
if parsed_type != self._plate_info.type:
return
plate: str | None = violation_dict["Biển kiểm soát"]
date: str | None = violation_dict["Thời gian vi phạm"]
color: str | None = violation_dict["Màu biển"]
location: str | None = violation_dict["Địa điểm vi phạm"]
violation: str | None = violation_dict["Hành vi vi phạm"]
status: str | None = violation_dict["Trạng thái"]
enforcement_unit: str | None = violation_dict["Đơn vị phát hiện vi phạm"]
resolution_offices: tuple[str, ...] | None = violation_dict[
"Nơi giải quyết vụ việc"
]
if any(
v is None
for v in (
plate,
color,
date,
location,
violation,
status,
enforcement_unit,
resolution_offices,
)
):
logger.error(f"Plate {self._plate_info.plate}: Cannot parse the data")
violation_detail: ViolationDetail = ViolationDetail(
plate=plate,
color=color,
type=parsed_type,
# Have to cast to string because lsp's warning
date=datetime.strptime(str(date), DATETIME_FORMAT),
location=location,
violation=violation,
status=status == "Đã xử phạt",
enforcement_unit=enforcement_unit,
resolution_offices=resolution_offices,
)
self._violations_details_set.add(violation_detail)

def parse_violations(self) -> tuple[ViolationDetail, ...] | None:
if not self._plate_detail_dict or not self._plate_detail_dict["data"]:
return
for violation_dict in self._plate_detail_dict["data"]:
self._parse_violation(violation_dict)
return tuple(self._violations_details_set)


class CheckPhatNguoiGetDataEngine(BaseGetDataEngine, HttpaioSession):
api: ApiEnum = ApiEnum.checkphatnguoi_vn
headers: Final[dict[str, str]] = {"Content-Type": "application/json"}

def __init__(self) -> None:
HttpaioSession.__init__(self, headers=self.headers)

@staticmethod
def get_violations(
plate_detail_dict: dict | None, filter_type: VehicleTypeEnum
) -> tuple[ViolationDetail, ...] | None:
violations_details_set: set[ViolationDetail] = set()
if not plate_detail_dict or not plate_detail_dict["data"]:
return

def _get_violation_detail(violation_dict: dict) -> None:
type: VehicleStrVieType = violation_dict["Loại phương tiện"]
# NOTE: this is for filtering the vehicle that doesn't match the plate info type. Because checkphatnguoi.vn return all of the type of the plate
if get_vehicle_enum(type) != filter_type:
return
date: str = violation_dict["Thời gian vi phạm"]
color: str = violation_dict["Màu biển"]
location: str = violation_dict["Địa điểm vi phạm"]
violation: str = violation_dict["Hành vi vi phạm"]
status: bool = (
False if violation_dict["Trạng thái"] == "Chưa xử phạt" else True
)
enforcement_unit: str = violation_dict["Đơn vị phát hiện vi phạm"]
resolution_office: tuple[str, ...] = violation_dict[
"Nơi giải quyết vụ việc"
]
violation_detail: ViolationDetail = ViolationDetail(
color=color,
date=datetime.strptime(date, DATETIME_FORMAT),
location=location,
violation=violation,
status=status,
enforcement_unit=enforcement_unit,
resolution_offices_details=resolution_office,
)
violations_details_set.add(violation_detail)

for violation_dict in plate_detail_dict["data"]:
_get_violation_detail(violation_dict)
return tuple(violations_details_set)

async def _request(self, plate_info: PlateInfo) -> dict | None:
payload: Final[dict[str, str]] = {"bienso": plate_info.plate}
try:
Expand Down Expand Up @@ -110,7 +129,9 @@ async def get_data(self, plate_info: PlateInfo) -> PlateDetail | None:
plate=plate_info.plate,
owner=plate_info.owner,
type=type,
violations=self.get_violations(plate_detail_dict, type),
violations=_CheckPhatNguoiGetDataParseEngine(
plate_info=plate_info, plate_detail_dict=plate_detail_dict
).parse_violations(),
)

@override
Expand Down
Loading

0 comments on commit b4419bd

Please sign in to comment.