mirror of
https://github.com/kaaninchen/Gleiswechsel.git
synced 2026-09-17 16:52:47 +00:00
transitous rewrite: add check_stations() function
This commit is contained in:
+74
-5
@@ -16,6 +16,77 @@ headers = {
|
||||
|
||||
endpoint = "https://api.transitous.org"
|
||||
|
||||
def check_stations():
|
||||
all_stations_output = {}
|
||||
minimal_overview = {
|
||||
"stations": {}
|
||||
}
|
||||
|
||||
for station in stations:
|
||||
req = f"{endpoint}/api/v1/geocode"
|
||||
|
||||
try:
|
||||
response = requests.get(req, params={"text": station}, headers=headers)
|
||||
response.raise_for_status()
|
||||
data = response.json()
|
||||
except requests.RequestException as e:
|
||||
logger(f"An error occured while checking for station {station}: {e}")
|
||||
continue
|
||||
|
||||
stops_dict = {}
|
||||
aliases_list = []
|
||||
exact_match = False
|
||||
|
||||
for entry in data:
|
||||
if entry.get("type") != "STOP":
|
||||
continue
|
||||
|
||||
station_id = entry.get("id")
|
||||
stop_name = entry.get("name")
|
||||
coords = f"{entry.get("lat")}, {entry.get("lon")}"
|
||||
modes = entry.get("modes")
|
||||
|
||||
stop_details = {
|
||||
"tz": entry.get("tz"),
|
||||
"country": entry.get("country"),
|
||||
"coords": coords,
|
||||
"modes": modes,
|
||||
"id": station_id,
|
||||
}
|
||||
|
||||
if stop_name:
|
||||
stops_dict[stop_name] = stop_details
|
||||
aliases_list.append(stop_name)
|
||||
|
||||
if stop_name == station:
|
||||
if not exact_match:
|
||||
logger(f"Exact match found! {station} is an assigned station! Bot would use that station directly")
|
||||
exact_match = True
|
||||
|
||||
if not stops_dict:
|
||||
logger("Failed to grab ID from 'search_name'", "error")
|
||||
continue
|
||||
|
||||
all_stations_output[station] = {
|
||||
"associated": stops_dict
|
||||
}
|
||||
|
||||
minimal_overview["stations"][station] = aliases_list
|
||||
|
||||
logger(json.dumps(minimal_overview, indent=4, ensure_ascii=False))
|
||||
|
||||
|
||||
print(f"\nIf you want, I can save a more detailed version directly as a json file to disk.")
|
||||
print("The json would provide informations like coords, country and transport modes that are from every specific associated station.")
|
||||
prompt = input("This would help you to identify the associated stations more accurately (y/n): ")
|
||||
if prompt == "y" or prompt == "yes":
|
||||
with open('stations.json', 'w') as f:
|
||||
json.dump(all_stations_output, f, indent=4, ensure_ascii=False)
|
||||
logger("stations.json generated")
|
||||
else:
|
||||
logger("okay :(")
|
||||
|
||||
|
||||
def get_random_stop_id() -> str | None:
|
||||
assigned_station = random.choice(stations)
|
||||
req = f"{endpoint}/api/v1/geocode"
|
||||
@@ -42,14 +113,15 @@ def get_random_stop_id() -> str | None:
|
||||
stop_ids[stop_id] = stop_name
|
||||
break
|
||||
|
||||
|
||||
if stop_ids is None:
|
||||
logger(f"Failed to grab ID from '{assigned_station}'", "error")
|
||||
return None
|
||||
|
||||
stop_ids_list = list(stop_ids.keys())
|
||||
stops_string = ", ".join(stop_ids.values())
|
||||
logger(f"Availabe stations: {stops_string}")
|
||||
if len(stop_ids_list) > 1:
|
||||
logger(f"Station '{assigned_station}' not found, choosing random from available: {stops_string}")
|
||||
logger(f"No station associated as '{assigned_station}', choosing random from available")
|
||||
chosen_stop_id = random.choice(stop_ids_list)
|
||||
return chosen_stop_id
|
||||
|
||||
@@ -176,9 +248,6 @@ def get_trip_details(random_connection: dict | None) -> dict | None:
|
||||
if not valid:
|
||||
return None
|
||||
|
||||
with open('data.json', 'w') as f:
|
||||
json.dump(data, f, indent=4, ensure_ascii=False)
|
||||
|
||||
logger(json.dumps(trip_details, indent=4, ensure_ascii=False))
|
||||
return trip_details
|
||||
|
||||
|
||||
@@ -102,6 +102,10 @@ OPERATORS = {
|
||||
"DSB": {
|
||||
"logo": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/87/DSB_company_logo.svg/960px-DSB_company_logo.svg.png",
|
||||
"color": 0xB22B32
|
||||
},
|
||||
"Vr": {
|
||||
"logo": "https://upload.wikimedia.org/wikipedia/commons/thumb/1/18/Vr_Logo.png/330px-Vr_Logo.png",
|
||||
"color": 0x00B451
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+10
-4
@@ -35,16 +35,22 @@ def build_info_embed() -> discord.Embed:
|
||||
stops = trip["stops"]
|
||||
|
||||
next_stop = get_next_station(trip["stops"])
|
||||
next_stop_station = next_stop["name"]
|
||||
next_stop_text = ""
|
||||
next_stop_station = None
|
||||
if next_stop:
|
||||
print("next_stop")
|
||||
next_stop_station = next_stop.get("name")
|
||||
next_stop_text = f". Nächster Halt: **{next_stop_station}**"
|
||||
|
||||
via = format_via_list(stops)
|
||||
|
||||
if via:
|
||||
via += f". Nächster Halt: **{next_stop_station}**"
|
||||
via += next_stop_text
|
||||
embed.add_field(name="Über", value=via, inline=False)
|
||||
else:
|
||||
embed.add_field(name="Nächster Halt", value=next_stop_station, inline=False)
|
||||
|
||||
if next_stop:
|
||||
embed.add_field(name="Nächster Halt", value=next_stop_station, inline=False)
|
||||
|
||||
route_fields = format_stop_list(stops, next_stop_station)
|
||||
for field_name, field_value in route_fields:
|
||||
embed.add_field(name=field_name, value=field_value, inline=False)
|
||||
|
||||
+1
-1
@@ -182,7 +182,7 @@ def format_via_list(stops: dict) -> str:
|
||||
return via
|
||||
return None
|
||||
|
||||
def format_stop_list(stops: dict, next_stop: str) -> list[tuple[str, str]]:
|
||||
def format_stop_list(stops: dict, next_stop: str | None) -> list[tuple[str, str]]:
|
||||
fields = []
|
||||
field_lines, field_length, part = [], 0, 1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user