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

Commit

Permalink
Merge branch 'main' into feat/discord-engine
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinNitroG committed Jan 14, 2025
2 parents 6ecad8c + bcde04f commit 5856d51
Show file tree
Hide file tree
Showing 20 changed files with 152 additions and 139 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/deploy-website.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ on:
branches: [main]
paths:
- docs/**
- config.sample.json
- jsfh-conf.yaml
- mkdocs.yml
- schemas/**
- config.sample.json
- requests/**
workflow_dispatch:

permissions:
Expand Down
File renamed without changes.
6 changes: 2 additions & 4 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,8 @@ plugins:
- trimmer

nav:
- 'Trang chủ':
- index.md
- 'Sử dụng':
- usage/usage.md
- 'Trang chủ': index.md
- 'Sử dụng': usage/index.md
- usage/schedule-run.md
- 'Config schema': 'https://ntgnguyen.github.io/check-phat-nguoi/schemas/config.html'
- 'Development':
Expand Down
10 changes: 6 additions & 4 deletions requests/checkphatnguoi_vn.http
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
### Get for 6A64685
POST /phatnguoi
HOST: https://api.checkphatnguoi.vn
# Config
@plate = 30F88251

### Get for {{plate}}
POST https://api.checkphatnguoi.vn/phatnguoi
Content-Type: application/json

{"bienso": "60A64685"}
{"bienso": "{{plate}}"}
23 changes: 14 additions & 9 deletions requests/csgt_vn.http
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
### csgt
POST /?mod=contact&task=tracuu_post&ajax
HOST: www.csgt.vn
Cookie: PHPSESSID=jmg7m23ttkho70rap0fs6evgb0
# Config
@plate = 30F88251
@type = 1
@phpsessid = jmg7m23ttkho70rap0fs6evgb0
@captcha = rtz4rv

### Get data for {{plate}} with type {{type}}
POST www.csgt.vn/?mod=contact&task=tracuu_post&ajax
Cookie: PHPSESSID={{phpsessid}}
content-type: application/x-www-form-urlencoded

BienKS=60A64685 &
Xe=1 &
captcha=rtz4rv &
ipClient=9.9.9.91 &
cUrl=1
BienKS = {{plate}} &
Xe= {{type}} &
captcha = {{captcha}} &
ipClient = 9.9.9.91 &
cUrl = 1
9 changes: 6 additions & 3 deletions requests/phatnguoi_vn.http
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### phatnguoi.vn
GET /web/tra-cuu/30F88251/1
HOST: https://api.phatnguoi.vn
# Config
@plate = 30F88251
@type = 1

### Get data for {{plate}} with type {{type}}
GET https://api.phatnguoi.vn/web/tra-cuu/{{plate}}/{{type}}
5 changes: 2 additions & 3 deletions src/check_phat_nguoi/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
from logging import getLogger

from check_phat_nguoi.config.config_reader import config
from check_phat_nguoi.context.plates.context import plates_context
from check_phat_nguoi.context import plates_context
from check_phat_nguoi.get_data import GetData
from check_phat_nguoi.notify import SendNotifications

from .utils.setup_logger import setup_logger
from check_phat_nguoi.utils import setup_logger

logger = getLogger(__name__)

Expand Down
21 changes: 7 additions & 14 deletions src/check_phat_nguoi/config/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from pydantic import BaseModel, ConfigDict, Field

from check_phat_nguoi.config.models.notifications.base_notification import (
BaseNotificationConfig,
)
from check_phat_nguoi.types import ApiEnum
from check_phat_nguoi.types.log_level import LogLevelEnum

from .notifications import (
DiscordNotificationConfig,
TelegramNotificationConfig,
)
from .plate_info import PlateInfo


Expand All @@ -23,21 +24,13 @@ class Config(BaseModel):
min_length=1,
)
# NOTE: Do not put base class in here. Because it will be wrong in schema.
notifications: tuple[BaseNotificationConfig, ...] = Field(
notifications: tuple[
TelegramNotificationConfig | DiscordNotificationConfig, ...
] = Field(
title="Danh sách thông báo",
description="Danh sách các thiết lập để thông báo",
default_factory=tuple,
)
# notifications: list[
# Annotated[
# TelegramNotificationConfig | DiscordNotificationConfig,
# Field(
# title="Danh sách thông báo",
# description="Danh sách các thiết lập để thông báo",
# default_factory=list,
# ),
# ]
# ]
api: tuple[ApiEnum, ...] | ApiEnum = Field(
title="API",
description="Sử dụng API từ trang web nào. Mặc định sẽ là list các API và dừng khi 1 API lấy dữ liệu thành công. Có thể điền giá trị trùng để retry. Hoặc chỉ dùng 1 API. Hiện tại API từ cgst.vn không đảm bảo có thể chạy.",
Expand Down
10 changes: 5 additions & 5 deletions src/check_phat_nguoi/get_data/engines/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .base_engine import BaseGetDataEngine
from .check_phat_nguoi import GetDataEngineCheckPhatNguoi
from .csgt import GetDataEngineCsgt
from .base import BaseGetDataEngine
from .check_phat_nguoi import CheckPhatNguoiGetDataEngine
from .csgt import CsgtGetDataEngine

__all__ = [
"BaseGetDataEngine",
"GetDataEngineCheckPhatNguoi",
"GetDataEngineCsgt",
"CheckPhatNguoiGetDataEngine",
"CsgtGetDataEngine",
]
32 changes: 0 additions & 32 deletions src/check_phat_nguoi/get_data/engines/base_session.py

This file was deleted.

12 changes: 8 additions & 4 deletions src/check_phat_nguoi/get_data/engines/check_phat_nguoi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@
VehicleTypeEnum,
get_vehicle_enum,
)
from check_phat_nguoi.utils import HttpaioSession

from .base_engine import BaseGetDataEngine
from .base_session import BaseGetDataSession
from .base import BaseGetDataEngine

logger = getLogger(__name__)


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

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

@staticmethod
def get_violations(
Expand Down Expand Up @@ -112,3 +112,7 @@ async def get_data(self, plate_info: PlateInfo) -> PlateDetail | None:
type=type,
violations=self.get_violations(plate_detail_dict, type),
)

@override
async def __aexit__(self, exc_type, exc_value, exc_traceback) -> None:
return await HttpaioSession.__aexit__(self, exc_type, exc_value, exc_traceback)
16 changes: 10 additions & 6 deletions src/check_phat_nguoi/get_data/engines/csgt.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
from check_phat_nguoi.constants import DATETIME_FORMAT_CHECKPHATNGUOI as DATETIME_FORMAT
from check_phat_nguoi.context import PlateDetail, ViolationDetail
from check_phat_nguoi.types import ApiEnum, VehicleTypeEnum, get_vehicle_enum
from check_phat_nguoi.utils import HttpaioSession

from .base_engine import BaseGetDataEngine
from .base_session import BaseGetDataSession
from .base import BaseGetDataEngine

logger = getLogger(__name__)

Expand All @@ -31,12 +31,12 @@
SSL_CONTEXT.set_ciphers("DEFAULT@SECLEVEL=1")


class _GetDataLocalEngineCsgt(BaseGetDataSession):
class _GetDataCsgtCoreEngine(HttpaioSession):
api: ApiEnum = ApiEnum.csgt_vn

def __init__(self, plate_info: PlateInfo) -> None:
self._plate_info: PlateInfo = plate_info
super().__init__()
HttpaioSession.__init__(self)

@staticmethod
def _bypass_captcha(captcha_img: bytes) -> str:
Expand Down Expand Up @@ -178,10 +178,14 @@ async def get_data(self) -> PlateDetail | None:
f"Plate {self._plate_info.plate}: Error occurs while getting data (internally) {self.api.value}. {e}"
)

@override
async def __aexit__(self, exc_type, exc_value, exc_traceback) -> None:
return await HttpaioSession.__aexit__(self, exc_type, exc_value, exc_traceback)


class GetDataEngineCsgt(BaseGetDataEngine):
class CsgtGetDataEngine(BaseGetDataEngine):
@override
async def get_data(self, plate_info: PlateInfo) -> PlateDetail | None:
async with _GetDataLocalEngineCsgt(plate_info) as local_engine:
async with _GetDataCsgtCoreEngine(plate_info) as local_engine:
plate_detail: PlateDetail | None = await local_engine.get_data()
return plate_detail
24 changes: 11 additions & 13 deletions src/check_phat_nguoi/get_data/get_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
)
from check_phat_nguoi.types import ApiEnum

from .engines import BaseGetDataEngine, GetDataEngineCheckPhatNguoi, GetDataEngineCsgt
from .engines import BaseGetDataEngine, CheckPhatNguoiGetDataEngine, CsgtGetDataEngine

logger = getLogger(__name__)


class GetData:
def __init__(self) -> None:
self._checkphatnguoi_engine: GetDataEngineCheckPhatNguoi
self._csgt_engine: GetDataEngineCsgt
self._checkphatnguoi_engine: CheckPhatNguoiGetDataEngine
self._csgt_engine: CsgtGetDataEngine
self._plates_details: set[PlateDetail] = set()

async def _get_data_for_plate(self, plate_info: PlateInfo) -> None:
Expand All @@ -26,33 +26,31 @@ async def _get_data_for_plate(self, plate_info: PlateInfo) -> None:
plate_info.api
if isinstance(plate_info.api, tuple)
else (plate_info.api,)
if plate_info.api is not None
if plate_info.api
else config.api
if isinstance(config.api, tuple)
else (config.api,)
)
get_data_engine: BaseGetDataEngine
engine: BaseGetDataEngine
for api in apis:
match api:
case ApiEnum.checkphatnguoi_vn:
get_data_engine = self._checkphatnguoi_engine
engine = self._checkphatnguoi_engine
case ApiEnum.csgt_vn:
get_data_engine = self._csgt_engine
engine = self._csgt_engine
logger.info(
f"Plate {plate_info.plate}: Getting data with API: {api.value}..."
)

plate_detail: PlateDetail | None = await get_data_engine.get_data(
plate_info
)
if plate_detail is not None:
plate_detail: PlateDetail | None = await engine.get_data(plate_info)
if plate_detail:
self._plates_details.add(plate_detail)
return

async def get_data(self) -> None:
async with (
GetDataEngineCheckPhatNguoi() as self._checkphatnguoi_engine,
GetDataEngineCsgt() as self._csgt_engine,
CheckPhatNguoiGetDataEngine() as self._checkphatnguoi_engine,
CsgtGetDataEngine() as self._csgt_engine,
):
if config.asynchronous:
await gather(
Expand Down
7 changes: 2 additions & 5 deletions src/check_phat_nguoi/notify/engines/discord.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import asyncio
from logging import getLogger
from typing import override

from discord import Intents, NotFound, User
from discord.ext.commands import Bot
Expand Down Expand Up @@ -59,11 +60,7 @@ async def __aexit__(self, exc_type, exc_val, exc_tb):


class DiscordNotificationEngine(BaseNotificationEngine):
async def __aenter__(self):
return self

async def __aexit__(self, exc_type, exc_val, exc_tb): ...

@override
async def send(
self,
notification_config: BaseNotificationEngineConfig,
Expand Down
Loading

0 comments on commit 5856d51

Please sign in to comment.