-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
186 lines (145 loc) · 5.18 KB
/
main.py
File metadata and controls
186 lines (145 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import re
import httpx
from bs4 import BeautifulSoup
from pydantic import BaseModel, Field
from datetime import datetime, timezone
from fastapi import FastAPI, HTTPException, Path
from typing import Dict, Literal, Optional, Tuple
from scalar_fastapi import get_scalar_api_reference, Theme, Layout
SOURCE_URL = "https://www.automobile.tn/fr/guide/dernieres-immatriculations.html"
C1 = "mx-1 flex justify-around rounded-[7px] border-2 border-white py-3 text-[30px] text-white"
C2 = C1 + " flex-row-reverse"
PlateType = Literal["TUN", "RS", "IT", "TRAC", "MC", "REM", "AA", "ES"]
PLATE_META: Dict[PlateType, Dict[str, str]] = {
"TUN": {"label": "Immatriculation Normale", "prefix": "TU"},
"RS": {"label": "Régime Suspensif", "prefix": "RS"},
"IT": {"label": "Immatriculation Temporaire", "prefix": "IT"},
"TRAC": {"label": "Tracteur", "prefix": "TRAC"},
"MC": {"label": "Motocyclette", "prefix": "MOTO"},
"REM": {"label": "Véhicule Remorqué", "prefix": "REM"},
"AA": {"label": "Appareil Agricole", "prefix": "AA"},
"ES": {"label": "Engins Spéciaux", "prefix": "ES"},
}
class PlateItem(BaseModel):
label: str
prefix: str
series: Optional[int] = Field(None, description="Series number (only for TUN)")
number: int
full: str
class PlatesResponse(BaseModel):
updated_at: str
source: str = "automobile.tn"
data: Dict[PlateType, PlateItem]
class PlateResponse(BaseModel):
type: PlateType
plate: PlateItem
updated_at: str
source: str = "automobile.tn"
def extract_numbers(raw: str) -> Tuple[Optional[int], Optional[int]]:
nums = [int(n) for n in re.findall(r"\d+", raw)]
if len(nums) >= 2:
return nums[0], nums[-1]
if len(nums) == 1:
return None, nums[0]
return None, None
def format_plate(code: PlateType, raw: str) -> PlateItem:
series, number = extract_numbers(raw)
if number is None:
raise ValueError("Invalid plate format")
meta = PLATE_META[code]
prefix = meta["prefix"]
full = (
f"{prefix} {series}/{number}"
if code == "TUN" and series is not None
else f"{prefix} {number}"
)
return PlateItem(
label=meta["label"],
prefix=prefix,
series=series if code == "TUN" else None,
number=number,
full=full,
)
async def fetch_latest_raw() -> Dict[PlateType, str]:
headers = {"User-Agent": "Mozilla/5.0 (TunisiaPlatesAPI/1.0)"}
try:
async with httpx.AsyncClient(timeout=10, headers=headers) as client:
r = await client.get(SOURCE_URL)
r.raise_for_status()
except httpx.RequestError:
raise HTTPException(status_code=502, detail="Upstream source unavailable")
soup = BeautifulSoup(r.text, "html.parser")
container = soup.find("div", class_="cms-prose")
if not container:
raise HTTPException(status_code=500, detail="Parsing failed")
tun_el = container.find("span", class_=C1)
spans = container.find_all("span", class_=C2)
if not tun_el or len(spans) < 7:
raise HTTPException(status_code=500, detail="Unexpected page structure")
return {
"TUN": tun_el.get_text(strip=True),
"RS": spans[0].get_text(strip=True),
"IT": spans[1].get_text(strip=True),
"TRAC": spans[2].get_text(strip=True),
"MC": spans[3].get_text(strip=True),
"REM": spans[4].get_text(strip=True),
"AA": spans[5].get_text(strip=True),
"ES": spans[6].get_text(strip=True),
}
app = FastAPI(
title="Tunisia Vehicle Plates API",
description="Clean REST API providing latest Tunisian vehicle registration numbers",
version="1.1.0",
docs_url=None,
redoc_url=None,
)
@app.get("/docs", include_in_schema=False)
async def scalar_html():
return get_scalar_api_reference(
openapi_url=app.openapi_url,
title="API Documentation",
layout=Layout.CLASSIC,
theme=Theme.DEEP_SPACE,
hide_models=True,
hide_client_button=False,
show_sidebar=True,
hide_search=False,
hide_dark_mode_toggle=False,
with_default_fonts=True,
expand_all_model_sections=False,
expand_all_responses=False,
integration="fastapi"
)
@app.get("/api/v1/plates", response_model=PlatesResponse, tags=["plates"])
async def get_all_plates():
raw = await fetch_latest_raw()
now = datetime.now(timezone.utc).isoformat()
data = {
code: format_plate(code, raw[code])
for code in PLATE_META
}
return PlatesResponse(
updated_at=now,
data=data,
)
@app.get(
"/api/v1/plates/{plate_type}",
response_model=PlateResponse,
tags=["plates"],
)
async def get_plate(
plate_type: str = Path(..., description="TUN, RS, IT, TRAC, MC, REM, AA, ES")
):
plate_type = plate_type.upper()
if plate_type not in PLATE_META:
raise HTTPException(
status_code=400,
detail=f"Invalid plate type. Allowed: {', '.join(PLATE_META.keys())}",
)
raw = await fetch_latest_raw()
now = datetime.now(timezone.utc).isoformat()
return PlateResponse(
type=plate_type,
plate=format_plate(plate_type, raw[plate_type]),
updated_at=now,
)