mirror of
https://github.com/kaaninchen/Gleiswechsel.git
synced 2026-09-17 16:52:47 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
21a279839d | ||
|
|
1c006731a4 |
@@ -121,6 +121,30 @@ For every station inside of the `stations.json` you'll find an ID. You can add t
|
|||||||
|
|
||||||
This is especially useful if you want to add a station whose name isn't unique and also used by other stations. The bot would falsely use the first station with the same name and consider it an exact match, even if you wanted a different one. This won't happen with the ID, as every ID is uniquely assigned to only one station.
|
This is especially useful if you want to add a station whose name isn't unique and also used by other stations. The bot would falsely use the first station with the same name and consider it an exact match, even if you wanted a different one. This won't happen with the ID, as every ID is uniquely assigned to only one station.
|
||||||
|
|
||||||
|
##### priority
|
||||||
|
You can define priorities of transport modes while selecting a connection. Leave empty to disable
|
||||||
|
|
||||||
|
The list should be descending, with "1" as the most important transport mode. For example, let's say, the bot should always choose Trains and other longer distance public transport modes. If it can't find any, then it should resort to Metros/Subways/Suburbans. If these are also missing at that station, then it should just use Busses/Trams:
|
||||||
|
|
||||||
|
```json
|
||||||
|
"priority": {
|
||||||
|
"HIGHSPEED_RAIL": 1,
|
||||||
|
"LONG_DISTANCE": 1,
|
||||||
|
"NIGHT_RAIL": 1,
|
||||||
|
"REGIONAL_RAIL": 1,
|
||||||
|
"COACH": 1,
|
||||||
|
"FERRY": 1,
|
||||||
|
"METRO": 2,
|
||||||
|
"SUBURBAN": 2,
|
||||||
|
"SUBWAY": 2,
|
||||||
|
"BUS": 3,
|
||||||
|
"TRAM": 3
|
||||||
|
}
|
||||||
|
```
|
||||||
|
A transport mode that isn't in the priority list would immediately get assigned the value of "99", making it near impossible to get that connection. Except when there are no other connections available, of course.
|
||||||
|
|
||||||
|
You can find the MODE names either in the console log or the stations.json from [helper tool](#stations)
|
||||||
|
|
||||||
##### blacklist
|
##### blacklist
|
||||||
You can blacklist specific types of transport, the bot would then skip them while selecting a connection. You can get the type in your console (mode)
|
You can blacklist specific types of transport, the bot would then skip them while selecting a connection. You can get the type in your console (mode)
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
"Helsinki"
|
"Helsinki"
|
||||||
],
|
],
|
||||||
"IDs": [],
|
"IDs": [],
|
||||||
|
"priority: "{
|
||||||
|
}",
|
||||||
"blacklist": [
|
"blacklist": [
|
||||||
"OTHER",
|
"OTHER",
|
||||||
"RIDE_SHARING"
|
"RIDE_SHARING"
|
||||||
|
|||||||
+27
-39
@@ -142,10 +142,12 @@ def get_random_stop_id() -> str | None:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
stop_ids_list = list(stop_ids.keys())
|
stop_ids_list = list(stop_ids.keys())
|
||||||
|
chosen_stop_id = random.choice(stop_ids_list)
|
||||||
if len(stop_ids_list) > 1:
|
if len(stop_ids_list) > 1:
|
||||||
logger(f"No station associated as '{assigned_station}', choosing random from similar named stations")
|
logger(f"No station associated as '{assigned_station}', choosing random from similar named stations")
|
||||||
logger(f"Run `python run main.py stations` to get exact station names")
|
logger(f"Run `python run main.py stations` to get exact station names")
|
||||||
chosen_stop_id, chosen_station = random.choice(list(stop_ids.items()))
|
logger(f"Selected station: {stop_ids[chosen_stop_id]}")
|
||||||
|
|
||||||
return chosen_stop_id
|
return chosen_stop_id
|
||||||
|
|
||||||
def get_random_connection(stop_id: str) -> str | None:
|
def get_random_connection(stop_id: str) -> str | None:
|
||||||
@@ -153,20 +155,7 @@ def get_random_connection(stop_id: str) -> str | None:
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||||
cursor = None
|
params = {"stopId": stop_id, "n": 50, "time": now}
|
||||||
max_pages = 5
|
|
||||||
min_results = 5
|
|
||||||
trip_ids = []
|
|
||||||
all_stop_times = []
|
|
||||||
|
|
||||||
for _ in range(max_pages):
|
|
||||||
params = {
|
|
||||||
"stopId": stop_id,
|
|
||||||
"n": 20,
|
|
||||||
"time": now,
|
|
||||||
}
|
|
||||||
if cursor:
|
|
||||||
params["pageCursor"] = cursor
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
response = requests.get(f"{endpoint}/api/v5/stoptimes", params=params, headers=headers)
|
response = requests.get(f"{endpoint}/api/v5/stoptimes", params=params, headers=headers)
|
||||||
@@ -174,39 +163,38 @@ def get_random_connection(stop_id: str) -> str | None:
|
|||||||
data = response.json()
|
data = response.json()
|
||||||
except requests.RequestException as e:
|
except requests.RequestException as e:
|
||||||
logger(e, "Error")
|
logger(e, "Error")
|
||||||
break
|
return None
|
||||||
|
|
||||||
stop_times = data.get("stopTimes", [])
|
candidates = []
|
||||||
all_stop_times.extend(stop_times)
|
for entry in data.get("stopTimes", []):
|
||||||
|
|
||||||
for entry in stop_times:
|
|
||||||
trip_id = entry["tripId"]
|
|
||||||
if entry["mode"] in blacklist:
|
if entry["mode"] in blacklist:
|
||||||
continue
|
continue
|
||||||
else:
|
candidates.append(entry)
|
||||||
trip_ids.append(entry["tripId"])
|
|
||||||
|
|
||||||
if len(trip_ids) >= min_results:
|
if not candidates:
|
||||||
break
|
|
||||||
|
|
||||||
cursor = data.get("nextPageCursor")
|
|
||||||
if not cursor:
|
|
||||||
break
|
|
||||||
|
|
||||||
if not trip_ids:
|
|
||||||
logger("Couldn't find any connections", "error")
|
logger("Couldn't find any connections", "error")
|
||||||
return None
|
return None
|
||||||
trip_id = random.choice(trip_ids)
|
|
||||||
|
|
||||||
from_station = None
|
if config.connections.priority:
|
||||||
for entry in all_stop_times:
|
best_priority = None
|
||||||
if entry.get("tripId") == trip_id:
|
for entry in candidates:
|
||||||
from_station = entry.get("place", {}).get("name")
|
priority = config.connections.priority.get(entry["mode"], 99)
|
||||||
break
|
if best_priority is None or priority < best_priority:
|
||||||
|
best_priority = priority
|
||||||
|
|
||||||
|
best_candidates = []
|
||||||
|
for entry in candidates:
|
||||||
|
priority = config.connections.priority.get(entry["mode"], 99)
|
||||||
|
if priority == best_priority:
|
||||||
|
best_candidates.append(entry)
|
||||||
|
|
||||||
|
chosen = random.choice(best_candidates)
|
||||||
|
else:
|
||||||
|
chosen = random.choice(candidates)
|
||||||
|
|
||||||
return {
|
return {
|
||||||
"trip_id": trip_id,
|
"trip_id": chosen["tripId"],
|
||||||
"from_station": from_station
|
"from_station": chosen.get("place", {}).get("name"),
|
||||||
}
|
}
|
||||||
|
|
||||||
def get_trip_details(random_connection: dict | None) -> dict | None:
|
def get_trip_details(random_connection: dict | None) -> dict | None:
|
||||||
|
|||||||
+2
-1
@@ -1,5 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
from dataclasses import dataclass, field
|
from dataclasses import dataclass
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
@@ -15,6 +15,7 @@ class DiscordConfig:
|
|||||||
class ConnectionsConfig:
|
class ConnectionsConfig:
|
||||||
stations: list[str]
|
stations: list[str]
|
||||||
IDs: list[str]
|
IDs: list[str]
|
||||||
|
priority: dict
|
||||||
blacklist: list[str]
|
blacklist: list[str]
|
||||||
min_duration: int
|
min_duration: int
|
||||||
timezone: str
|
timezone: str
|
||||||
|
|||||||
Binary file not shown.
|
Before Width: | Height: | Size: 1.3 KiB |
@@ -1,2 +0,0 @@
|
|||||||
SPDX-FileCopyrightText: 2024 Mathis Brüchert <[email protected]>
|
|
||||||
SPDX-License-Identifier: CC-BY-SA-4.0
|
|
||||||
+1
-1
@@ -9,7 +9,7 @@ lang_embed = lang.embeds
|
|||||||
|
|
||||||
def build_embed_footer(slogans):
|
def build_embed_footer(slogans):
|
||||||
footer_notice = f"https://transitous.org/sources/ • Maps (C) CARTO (C) OpenStreetMap.org contributors"
|
footer_notice = f"https://transitous.org/sources/ • Maps (C) CARTO (C) OpenStreetMap.org contributors"
|
||||||
icon = "https://raw.githubusercontent.com/kaaninchen/Gleiswechsel/refs/heads/main/src/data/assets/transitous-logo.png"
|
icon = "https://api.transitous.org/favicon.png"
|
||||||
|
|
||||||
if slogans is not None:
|
if slogans is not None:
|
||||||
footer_text = f"{random.choice(slogans)} • {footer_notice}"
|
footer_text = f"{random.choice(slogans)} • {footer_notice}"
|
||||||
|
|||||||
Reference in New Issue
Block a user