diff --git a/src/dc/embeds.py b/src/dc/embeds.py index 3cf5a9d..495d78b 100644 --- a/src/dc/embeds.py +++ b/src/dc/embeds.py @@ -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"]) diff --git a/src/utils.py b/src/utils.py index 46f0491..ea0989b 100644 --- a/src/utils.py +++ b/src/utils.py @@ -176,4 +176,35 @@ def get_next_station(stops: dict) -> dict | None: return {"name": name, "arrival": arrival_str} return None - \ No newline at end of file + +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 \ No newline at end of file