mirror of
https://github.com/kaaninchen/Gleiswechsel.git
synced 2026-09-17 16:52:47 +00:00
transitous rewrite: code cleanup
This commit is contained in:
+12
-26
@@ -1,7 +1,7 @@
|
||||
import discord
|
||||
import random
|
||||
|
||||
from src.utils import get_operator_metadata, get_next_station
|
||||
from src.utils import get_operator_metadata, get_next_station, format_via_list, format_stop_list
|
||||
from src.dc.helpers import format_timestamp_to_dc
|
||||
|
||||
def build_embed_footer(mode: str, slogans):
|
||||
@@ -25,7 +25,6 @@ def build_info_embed() -> discord.Embed:
|
||||
metadata = get_operator_metadata(agency, trip["route_color"])
|
||||
departure = format_timestamp_to_dc(trip["departure"])
|
||||
arrival = format_timestamp_to_dc(trip["arrival"])
|
||||
next_stop = get_next_station(trip["stops"])
|
||||
|
||||
embed = discord.Embed(
|
||||
title = trip["long_name"],
|
||||
@@ -35,33 +34,20 @@ def build_info_embed() -> discord.Embed:
|
||||
|
||||
stops = trip["stops"]
|
||||
|
||||
if len(stops) > 2:
|
||||
count = min(3, len(stops))
|
||||
random_stops = random.sample(list(stops), k=count)
|
||||
next_stop = get_next_station(trip["stops"])
|
||||
next_stop_station = next_stop["name"]
|
||||
|
||||
via = ", ".join(random_stops[:-1]) + " und " + random_stops[-1] + ". Nächster Halt: " + next_stop["name"]
|
||||
via = format_via_list(stops)
|
||||
|
||||
if via:
|
||||
via += f". Nächster Halt: **{next_stop_station}**"
|
||||
embed.add_field(name="Über", value=via, inline=False)
|
||||
else:
|
||||
embed.add_field(name="Nächster Halt", value=next_stop_station, inline=False)
|
||||
|
||||
field_lines, field_length, part = [], 0, 1
|
||||
|
||||
for name, stop_arrival in stops.items():
|
||||
if name == next_stop["name"]:
|
||||
line = f"**• {name} ({stop_arrival} Uhr)**"
|
||||
else:
|
||||
line = f"• {name} ({stop_arrival} Uhr)"
|
||||
|
||||
if field_length + len(line) + 1 > 1024:
|
||||
route_page_name = "Route"
|
||||
if part != 1:
|
||||
route_page_name += " (Fortsetzung)"
|
||||
embed.add_field(name=route_page_name, value="\n".join(field_lines), inline=False)
|
||||
field_lines, field_length, part = [], 0, part + 1
|
||||
|
||||
field_lines.append(line)
|
||||
field_length += len(line) + 1
|
||||
|
||||
if field_lines:
|
||||
embed.add_field(name="Route" if part == 1 else "Route (Fortsetzung)", value="\n".join(field_lines), 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)
|
||||
|
||||
footer = build_embed_footer(trip["mode"], metadata["slogans"])
|
||||
embed.set_footer(text=footer["text"], icon_url=footer["icon"])
|
||||
|
||||
@@ -177,3 +177,34 @@ def get_next_station(stops: dict) -> dict | None:
|
||||
|
||||
return None
|
||||
|
||||
def format_via_list(stops: dict) -> str:
|
||||
if len(stops) > 2:
|
||||
count = min(3, len(stops))
|
||||
random_stops = random.sample(list(stops), k=count)
|
||||
via = f"{', '.join(random_stops[:-1])} und {random_stops[-1]}"
|
||||
return via
|
||||
return None
|
||||
|
||||
def format_stop_list(stops: dict, next_stop: str) -> list[tuple[str, str]]:
|
||||
fields = []
|
||||
field_lines, field_length, part = [], 0, 1
|
||||
|
||||
for name, stop_arrival in stops.items():
|
||||
if name == next_stop:
|
||||
line = f"**• {name} ({stop_arrival} Uhr)**"
|
||||
else:
|
||||
line = f"• {name} ({stop_arrival} Uhr)"
|
||||
|
||||
if field_length + len(line) + 1 > 1024:
|
||||
route_page_name = "Route" if part == 1 else "Route (Fortsetzung)"
|
||||
fields.append((route_page_name, "\n".join(field_lines)))
|
||||
field_lines, field_length, part = [], 0, part + 1
|
||||
|
||||
field_lines.append(line)
|
||||
field_length += len(line) + 1
|
||||
|
||||
if field_lines:
|
||||
route_page_name = "Route" if part == 1 else "Route (Fortsetzung)"
|
||||
fields.append((route_page_name, "\n".join(field_lines)))
|
||||
|
||||
return fields
|
||||
Reference in New Issue
Block a user