Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

script: add script to download a timetable json from its ID #10419

Merged
merged 1 commit into from
Feb 4, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions scripts/download_timetable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# This script can be used to download the content of a timetable knowing only its ID,
# as a JSON file that can be re-imported in OSRD.
#

import requests
import json
from typing import Dict, List

EDITOAST_URL = "https://rec-osrd.reseau.sncf.fr/"
COOKIES = {
# Connect to the front-end and look through the "cookies" part of any sent request
"gateway" : ""
}
TIMETABLE_ID = 1
OUT_PATH = "timetable.json"


def download_timetable(timetable_id: int) -> List[Dict]:
response = requests.get(f"{EDITOAST_URL}api/timetable/{timetable_id}/", cookies=COOKIES, verify=False)
response.raise_for_status()
json_response = response.json()
train_ids = json_response["train_ids"]
return download_trains(train_ids)


def download_trains(train_ids: List[int]) -> List[Dict]:
payload = {
"ids": train_ids
}
response = requests.post(f"{EDITOAST_URL}api/train_schedule/", cookies=COOKIES, json=payload, verify=False)
response.raise_for_status()
json_response = response.json()
for path_item in json_response:
del path_item["id"]
del path_item["timetable_id"]
return json_response


if __name__ == "__main__":
trains = download_timetable(TIMETABLE_ID)
with open(OUT_PATH, "w", encoding="utf-8") as jsonfile:
json.dump(trains, jsonfile, ensure_ascii=False, indent=4)
print(f"dumped timetable {TIMETABLE_ID} ({len(trains)} trains) to {OUT_PATH}")
Loading