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

Commit

Permalink
fix: hehe
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 17, 2025
1 parent ff0250b commit 7d25449
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 36 deletions.
55 changes: 21 additions & 34 deletions src/check_phat_nguoi/get_data/engines/zm_io.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from asyncio import TimeoutError
from datetime import datetime
from logging import getLogger
from typing import TypeAlias, TypedDict, cast, override
from typing import Literal, TypedDict, cast, override

from aiohttp import ClientError

Expand All @@ -11,7 +11,6 @@
from check_phat_nguoi.context import PlateDetail, ViolationDetail
from check_phat_nguoi.types import (
ApiEnum,
VehicleStrVieType,
VehicleTypeEnum,
get_vehicle_enum,
)
Expand All @@ -20,31 +19,23 @@
from .base import BaseGetDataEngine

logger = getLogger(__name__)
_DataResponse = TypedDict(
"_DataResponse",
{
"bienkiemsoat": str,
"maubien": str,
"loaiphuongtien": VehicleStrVieType,
"thoigianvipham": str,
"diadiemvipham": str,
"trangthai": str,
"donviphathienvipham": str,
"noigiaiquyetvuviec": str,
},
)
_FoundResponse = TypedDict(
"_FoundResponse",
{"json": tuple[_DataResponse, ...], "html": str, "css": str},
)


_NotFoundResponse = TypedDict(
"_NotFoundResponse",
{"json": None, "html": str, "css": str},
)
class _DataResponse(TypedDict):
bienkiemsoat: str
maubien: str
loaiphuongtien: Literal["Ô tô", "Xe máy", "Xe máy điện"]
thoigianvipham: str
diadiemvipham: str
trangthai: str
donviphathienvipham: str
noigiaiquyetvuviec: str

_Response: TypeAlias = _FoundResponse | _NotFoundResponse

class _Response(TypedDict):
json: tuple[_DataResponse, ...] | None
html: str
css: str


class _ZMIOGetDataParseEngine:
Expand All @@ -54,23 +45,19 @@ def __init__(self, plate_info: PlateInfo, plate_detail_dict: _Response) -> None:
self._violations_details_set: set[ViolationDetail] = set()

def _parse_violation(self, data: _DataResponse) -> None:
type: VehicleStrVieType | None = data["loaiphuongtien"]
# 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 = data["bienkiemsoat"]
date: str = data["thoigianvipham"]
type: Literal["Ô tô", "Xe máy", "Xe máy điện"] = data["loaiphuongtien"]
color: str = data["maubien"]
location: str = data["diadiemvipham"]
status: str = data["trangthai"]
enforcement_unit: str = data["donviphathienvipham"]
# NOTE: this api just responses 1 resolution_office
resolution_offices: tuple[str, ...] = tuple(data["noigiaiquyetvuviec"])
resolution_offices: tuple[str, ...] = (data["noigiaiquyetvuviec"],)
violation_detail: ViolationDetail = ViolationDetail(
plate=plate,
color=color,
type=parsed_type,
type=get_vehicle_enum(type),
date=datetime.strptime(str(date), DATETIME_FORMAT),
location=location,
status=status == "Đã xử phạt",
Expand All @@ -82,8 +69,8 @@ def _parse_violation(self, data: _DataResponse) -> None:
def parse(self) -> tuple[ViolationDetail, ...] | None:
if not self._plate_detail_typed["json"]:
return
for json in self._plate_detail_typed["json"]:
self._parse_violation(json)
for data in self._plate_detail_typed["json"]:
self._parse_violation(data)
return tuple(self._violations_details_set)


Expand Down Expand Up @@ -114,7 +101,7 @@ async def _request(self, plate_info: PlateInfo) -> dict | None:

@override
async def get_data(self, plate_info: PlateInfo) -> PlateDetail | None:
plate_detail_raw = await self._request(plate_info)
plate_detail_raw: dict | None = await self._request(plate_info)
if not plate_detail_raw:
return
plate_detail_typed: _Response = cast(_Response, plate_detail_raw)
Expand Down
5 changes: 3 additions & 2 deletions src/check_phat_nguoi/get_data/get_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self) -> None:
self._checkphatnguoi_engine: CheckPhatNguoiGetDataEngine
self._csgt_engine: CsgtGetDataEngine
self._phatnguoi_engine: PhatNguoiGetDataEngine
self._etraffic_engine: ZMIOGetDataEngine
self._zmio_engine: ZMIOGetDataEngine
self._plates_details: set[PlateDetail] = set()

async def _get_data_for_plate(self, plate_info: PlateInfo) -> None:
Expand All @@ -41,7 +41,7 @@ async def _get_data_for_plate(self, plate_info: PlateInfo) -> None:
case ApiEnum.phatnguoi_vn:
engine = self._phatnguoi_engine
case ApiEnum.zm_io_vn:
engine = self._etraffic_engine
engine = self._zmio_engine
logger.info(
f"Plate {plate_info.plate}: Getting data with API: {api.value}..."
)
Expand All @@ -63,6 +63,7 @@ async def get_data(self) -> None:
CheckPhatNguoiGetDataEngine() as self._checkphatnguoi_engine,
CsgtGetDataEngine() as self._csgt_engine,
PhatNguoiGetDataEngine() as self._phatnguoi_engine,
ZMIOGetDataEngine() as self._zmio_engine,
):
if config.asynchronous:
await gather(
Expand Down

0 comments on commit 7d25449

Please sign in to comment.