Skip to content

Commit

Permalink
script: add script to download a timetable json from its ID
Browse files Browse the repository at this point in the history
Signed-off-by: Eloi Charpentier <[email protected]>
  • Loading branch information
eckter committed Feb 4, 2025
1 parent 005ff04 commit 2e7a17b
Showing 1 changed file with 44 additions and 0 deletions.
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}")

0 comments on commit 2e7a17b

Please sign in to comment.