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|
|
|
|
|
|
| 9 |
url = f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/station/{pattern}"
|
| 10 |
headers = {
|
| 11 |
'DB-Client-Id': client_id,
|
|
@@ -24,7 +175,7 @@ def get_station_info(pattern, client_id, api_key):
|
|
| 24 |
return None
|
| 25 |
|
| 26 |
def fetch_timetable_hour(eva, date_str, hour_str, client_id, api_key):
|
| 27 |
-
|
| 28 |
url = f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/{eva}/{date_str}/{hour_str}"
|
| 29 |
headers = {
|
| 30 |
'DB-Client-Id': client_id,
|
|
@@ -40,7 +191,7 @@ def fetch_timetable_hour(eva, date_str, hour_str, client_id, api_key):
|
|
| 40 |
return None
|
| 41 |
|
| 42 |
def search_next_3_connections(departure, destination, client_id, api_key):
|
| 43 |
-
|
| 44 |
|
| 45 |
if not all([departure, destination, client_id, api_key]):
|
| 46 |
return {"error": "Bitte alle Felder ausfüllen."}
|
|
@@ -127,7 +278,7 @@ with gr.Blocks(title="DB JSON Fahrplan", theme=gr.themes.Soft()) as demo:
|
|
| 127 |
if __name__ == "__main__":
|
| 128 |
demo.launch()
|
| 129 |
|
| 130 |
-
|
| 131 |
import gradio as gr
|
| 132 |
import requests
|
| 133 |
from datetime import datetime, timedelta
|
|
|
|
| 6 |
|
| 7 |
def get_station_info(pattern, client_id, api_key):
|
| 8 |
"""Ermittelt EVA-Nummer und offiziellen Namen."""
|
| 9 |
+
url = f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/station/{pattern}"
|
| 10 |
+
headers = {'DB-Client-Id': client_id, 'DB-Api-Key': api_key, 'accept': 'application/xml'}
|
| 11 |
+
try:
|
| 12 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 13 |
+
response.raise_for_status()
|
| 14 |
+
root = ET.fromstring(response.content)
|
| 15 |
+
station = root.find('station')
|
| 16 |
+
if station is not None:
|
| 17 |
+
return {'name': station.get('name'), 'eva': station.get('eva')}
|
| 18 |
+
except Exception:
|
| 19 |
+
return None
|
| 20 |
+
return None
|
| 21 |
+
|
| 22 |
+
def fetch_timetable_hour(eva, date_str, hour_str, client_id, api_key):
|
| 23 |
+
"""Ruft den Fahrplan für eine bestimmte Stunde ab."""
|
| 24 |
+
url = f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/{eva}/{date_str}/{hour_str}"
|
| 25 |
+
headers = {'DB-Client-Id': client_id, 'DB-Api-Key': api_key, 'accept': 'application/xml'}
|
| 26 |
+
try:
|
| 27 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 28 |
+
if response.status_code == 200:
|
| 29 |
+
return ET.fromstring(response.content)
|
| 30 |
+
except Exception:
|
| 31 |
+
pass
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
def get_connections(departure, destination, client_id, api_key):
|
| 35 |
+
"""Sucht die nächsten 3 Verbindungen ab jetzt."""
|
| 36 |
+
if not all([departure, destination, client_id, api_key]):
|
| 37 |
+
return None, "❌ Bitte alle Felder ausfüllen."
|
| 38 |
+
|
| 39 |
+
dep_info = get_station_info(departure, client_id, api_key)
|
| 40 |
+
dest_info = get_station_info(destination, client_id, api_key)
|
| 41 |
+
|
| 42 |
+
if not dep_info or not dest_info:
|
| 43 |
+
return None, "❌ Bahnhof nicht gefunden. Bitte Namen prüfen."
|
| 44 |
+
|
| 45 |
+
now = datetime.now()
|
| 46 |
+
found_connections = []
|
| 47 |
+
|
| 48 |
+
# Suche über 3 Stunden-Slots hinweg (aktuelle + nächste zwei)
|
| 49 |
+
for hour_offset in range(3):
|
| 50 |
+
search_time = now + timedelta(hours=hour_offset)
|
| 51 |
+
datum = search_time.strftime("%y%m%d")
|
| 52 |
+
stunde = search_time.strftime("%H")
|
| 53 |
+
|
| 54 |
+
root = fetch_timetable_hour(dep_info['eva'], datum, stunde, client_id, api_key)
|
| 55 |
+
if root is None: continue
|
| 56 |
+
|
| 57 |
+
for train in root.findall('.//s'):
|
| 58 |
+
dp = train.find('dp')
|
| 59 |
+
if dp is None: continue
|
| 60 |
+
|
| 61 |
+
path = dp.get('ppth', '')
|
| 62 |
+
if dest_info['name'].lower() in path.lower() or destination.lower() in path.lower():
|
| 63 |
+
pt = dp.get('pt', '')
|
| 64 |
+
if not pt: continue
|
| 65 |
+
|
| 66 |
+
dep_dt = datetime.strptime(pt, "%y%m%d%H%M")
|
| 67 |
+
|
| 68 |
+
if dep_dt >= now:
|
| 69 |
+
tl = train.find('tl')
|
| 70 |
+
found_connections.append({
|
| 71 |
+
"startort": dep_info['name'],
|
| 72 |
+
"zielort": dest_info['name'],
|
| 73 |
+
"abfahrtszeit": dep_dt.strftime("%H:%M"),
|
| 74 |
+
"datum": dep_dt.strftime("%d.%m.%Y"),
|
| 75 |
+
"gleis": dp.get('pp', '-'),
|
| 76 |
+
"zug": f"{tl.get('c', '')} {tl.get('n', '')}",
|
| 77 |
+
"_dt": dep_dt
|
| 78 |
+
})
|
| 79 |
+
|
| 80 |
+
found_connections.sort(key=lambda x: x['_dt'])
|
| 81 |
+
return found_connections[:3], None
|
| 82 |
+
|
| 83 |
+
def format_markdown(connections):
|
| 84 |
+
"""Erstellt eine Smartphone-optimierte Markdown-Ansicht."""
|
| 85 |
+
if not connections:
|
| 86 |
+
return "### ℹ️ Keine Verbindungen gefunden\nIn den nächsten 3 Stunden wurden keine Direktverbindungen gefunden."
|
| 87 |
+
|
| 88 |
+
md = f"## 🚆 {connections[0]['startort']} → {connections[0]['zielort']}\n"
|
| 89 |
+
md += f"*{datetime.now().strftime('%H:%M')} Uhr Stand*\n\n"
|
| 90 |
+
|
| 91 |
+
for i, c in enumerate(connections, 1):
|
| 92 |
+
md += f"### {i}. {c['abfahrtszeit']} Uhr\n"
|
| 93 |
+
md += f"**{c['zug']}** \n"
|
| 94 |
+
md += f"Gleis: **{c['gleis']}** | Datum: {c['datum']}\n"
|
| 95 |
+
md += "---\n" # Trennlinie für bessere Lesbarkeit auf dem Handy
|
| 96 |
+
|
| 97 |
+
return md
|
| 98 |
+
|
| 99 |
+
def main_interface(dep, dest, cid, akey):
|
| 100 |
+
conns, error = get_connections(dep, dest, cid, akey)
|
| 101 |
+
|
| 102 |
+
if error:
|
| 103 |
+
return error, json.dumps({"error": error}, indent=2)
|
| 104 |
+
|
| 105 |
+
# JSON-Bereinigung (internes Sortierfeld entfernen)
|
| 106 |
+
clean_json = []
|
| 107 |
+
for c in conns:
|
| 108 |
+
clean_json.append({k: v for k, v in c.items() if k != "_dt"})
|
| 109 |
+
|
| 110 |
+
md_output = format_markdown(clean_json)
|
| 111 |
+
json_output = json.dumps(clean_json, indent=4, ensure_ascii=False)
|
| 112 |
+
|
| 113 |
+
return md_output, json_output
|
| 114 |
+
|
| 115 |
+
# --- Gradio UI ---
|
| 116 |
+
with gr.Blocks(title="DB Mobile Fahrplan", theme=gr.themes.Soft()) as demo:
|
| 117 |
+
gr.Markdown("# 📱 DB Quick-Check")
|
| 118 |
+
|
| 119 |
+
with gr.Accordion("⚙️ API-Konfiguration (Hier klicken)", open=False):
|
| 120 |
+
cid_input = gr.Textbox(label="DB Client ID", type="password")
|
| 121 |
+
akey_input = gr.Textbox(label="DB API Key", type="password")
|
| 122 |
+
|
| 123 |
+
with gr.Row():
|
| 124 |
+
dep_input = gr.Textbox(label="Von", placeholder="z.B. Berlin", scale=2)
|
| 125 |
+
dest_input = gr.Textbox(label="Nach", placeholder="z.B. Hamburg", scale=2)
|
| 126 |
+
|
| 127 |
+
btn = gr.Button("🔍 Verbindungen suchen", variant="primary")
|
| 128 |
+
|
| 129 |
+
with gr.Tabs():
|
| 130 |
+
with gr.TabItem("📱 Ansicht"):
|
| 131 |
+
md_display = gr.Markdown("Geben Sie Start und Ziel ein und drücken Sie Suchen.")
|
| 132 |
+
with gr.TabItem("💻 JSON"):
|
| 133 |
+
json_display = gr.Code(label="API Response", language="json")
|
| 134 |
+
|
| 135 |
+
btn.click(
|
| 136 |
+
fn=main_interface,
|
| 137 |
+
inputs=[dep_input, dest_input, cid_input, akey_input],
|
| 138 |
+
outputs=[md_display, json_display]
|
| 139 |
+
)
|
| 140 |
+
|
| 141 |
+
if __name__ == "__main__":
|
| 142 |
+
demo.launch()
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
|
| 151 |
+
|
| 152 |
+
"""
|
| 153 |
+
import gradio as gr
|
| 154 |
+
import requests
|
| 155 |
+
from datetime import datetime, timedelta
|
| 156 |
+
import xml.etree.ElementTree as ET
|
| 157 |
+
import json
|
| 158 |
+
|
| 159 |
+
def
|
| 160 |
url = f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/station/{pattern}"
|
| 161 |
headers = {
|
| 162 |
'DB-Client-Id': client_id,
|
|
|
|
| 175 |
return None
|
| 176 |
|
| 177 |
def fetch_timetable_hour(eva, date_str, hour_str, client_id, api_key):
|
| 178 |
+
|
| 179 |
url = f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/plan/{eva}/{date_str}/{hour_str}"
|
| 180 |
headers = {
|
| 181 |
'DB-Client-Id': client_id,
|
|
|
|
| 191 |
return None
|
| 192 |
|
| 193 |
def search_next_3_connections(departure, destination, client_id, api_key):
|
| 194 |
+
|
| 195 |
|
| 196 |
if not all([departure, destination, client_id, api_key]):
|
| 197 |
return {"error": "Bitte alle Felder ausfüllen."}
|
|
|
|
| 278 |
if __name__ == "__main__":
|
| 279 |
demo.launch()
|
| 280 |
|
| 281 |
+
|
| 282 |
import gradio as gr
|
| 283 |
import requests
|
| 284 |
from datetime import datetime, timedelta
|