Skip to content

Commit

Permalink
add maintancer & alias
Browse files Browse the repository at this point in the history
  • Loading branch information
XieXiLin2 committed Jan 19, 2024
1 parent 864882a commit e3c2a76
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
36 changes: 34 additions & 2 deletions nonebot_plugin_cnrail/data_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
CHINA_RAIL_SEARCH_API = "https://search.12306.cn/search/v1/train/search"
CHINA_RAIL_DETAIL_API = "https://kyfw.12306.cn/otn/queryTrainInfo/query"

CNRAIL_DATA_BASE_URL = "https://cnrail-data.baka.pub/data/"

ACG_IMAGE_URL = "https://www.loliapi.com/acg/pe/"

TEMPLATE_PATH = Path(__file__).parent / "templates" / "template.html.jinja"
Expand Down Expand Up @@ -42,7 +44,28 @@ async def query_train_info(train_code: str, train_date: str) -> Optional[TrainIn

raw_data = resp.json()["data"]
if not raw_data:
return None
async with httpx.AsyncClient(
base_url=CNRAIL_DATA_BASE_URL,
follow_redirects=True,
) as client:
resp = await client.get("/alias.json")
resp.raise_for_status()

train_code = resp.json()[train_code]

async with httpx.AsyncClient(follow_redirects=True) as client:
resp = await client.get(
CHINA_RAIL_SEARCH_API,
params={
"keyword": train_code,
"date": train_date.replace("-", ""),
},
)
resp.raise_for_status()

raw_data = resp.json()["data"]
if not raw_data:
return None

data = parse_obj_as(List[TrainSummary], raw_data)
if len(data) > 1:
Expand Down Expand Up @@ -74,7 +97,16 @@ async def query_train_info(train_code: str, train_date: str) -> Optional[TrainIn
return None

stations = parse_obj_as(List[TrainStation], raw_data)
return TrainInfo(summary=summary, stations=stations)

async with httpx.AsyncClient(
base_url=CNRAIL_DATA_BASE_URL, follow_redirects=True
) as client:
resp = await client.get("/maintance.json")
resp.raise_for_status()

maintancer = resp.json()[train_code]

return TrainInfo(summary=summary, stations=stations, maintancer=maintancer)


async def render_train_info(info: TrainInfo) -> bytes:
Expand Down
1 change: 1 addition & 0 deletions nonebot_plugin_cnrail/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def stay_minutes(self) -> int:
class TrainInfo(BaseModel):
summary: TrainSummary
stations: List[TrainStation]
maintancer: str

_arrive_next_day: Optional[bool] = PrivateAttr(None)

Expand Down
6 changes: 3 additions & 3 deletions nonebot_plugin_cnrail/templates/template.html.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@
</div>
</div>
</div>
<!-- <div class="station-bottom">
<div class="admin">担当路局:郑州局</div>
</div> -->
<div class="station-bottom">
<div class="admin">担当路局:{{ info.maintancer }}</div>
</div>
</div>
</div>

Expand Down

0 comments on commit e3c2a76

Please sign in to comment.