mgokg commited on
Commit
2387771
·
verified ·
1 Parent(s): 11977d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -10
app.py CHANGED
@@ -3,11 +3,6 @@ import requests
3
  from datetime import datetime, timedelta
4
  import xml.etree.ElementTree as ET
5
  import json
6
- import os
7
-
8
- client_id = os.getenv('client_id')
9
- api_key = os.getenv('api_key')
10
-
11
 
12
  def get_station_info(pattern, client_id, api_key):
13
  """Ermittelt EVA-Nummer und offiziellen Namen."""
@@ -46,6 +41,9 @@ def fetch_timetable_hour(eva, date_str, hour_str, client_id, api_key):
46
 
47
  def search_next_3_connections(departure, destination, client_id, api_key):
48
  """Hauptfunktion: Findet die nächsten 3 Verbindungen ab 'jetzt'."""
 
 
 
49
 
50
  # 1. IBNRs ermitteln
51
  dep_info = get_station_info(departure, client_id, api_key)
@@ -77,7 +75,7 @@ def search_next_3_connections(departure, destination, client_id, api_key):
77
  pt = dp.get('pt', '') # Geplante Zeit: YYMMDDHHMM
78
  if not pt: continue
79
 
80
- dep_dt = datetime.strptime(pt, "%H%M")
81
 
82
  # Nur Verbindungen in der Zukunft (ab Zeitstempel jetzt)
83
  if dep_dt >= now:
@@ -85,7 +83,7 @@ def search_next_3_connections(departure, destination, client_id, api_key):
85
  found_connections.append({
86
  "startort": dep_info['name'],
87
  "zielort": dest_info['name'],
88
- "abfahrtszeit": dep_dt.strftime("%H:%M"),
89
  "gleis": dp.get('pp', 'n/a'),
90
  "zug": f"{tl.get('c', '')} {tl.get('n', '')}",
91
  "_dt": dep_dt # Hilfsfeld zum Sortieren
@@ -105,13 +103,17 @@ def search_next_3_connections(departure, destination, client_id, api_key):
105
 
106
  # --- Gradio UI ---
107
 
108
- def ui_wrapper(dep, dest, client_id, api_key):
109
- results = search_next_3_connections(dep, dest, client_id, api_key)
110
  return json.dumps(results, indent=4, ensure_ascii=False)
111
 
112
  with gr.Blocks(title="DB JSON Fahrplan", theme=gr.themes.Soft()) as demo:
113
  gr.Markdown("# 🚆 DB Verbindungs-Suche (JSON)")
114
  gr.Markdown("Ermittelt die nächsten 3 Verbindungen ab dem aktuellen Zeitstempel.")
 
 
 
 
115
 
116
  with gr.Row():
117
  dep_input = gr.Textbox(label="Abfahrtsort", placeholder="z.B. Berlin")
@@ -120,7 +122,7 @@ with gr.Blocks(title="DB JSON Fahrplan", theme=gr.themes.Soft()) as demo:
120
  btn = gr.Button("Suchen", variant="primary")
121
  output = gr.JSON(label="JSON Ergebnis") # Nutzt Gradio JSON Komponente für schöneres Format
122
 
123
- btn.click(fn=ui_wrapper, inputs=[dep_input, dest_input], outputs=output)
124
 
125
  if __name__ == "__main__":
126
  demo.launch()
 
3
  from datetime import datetime, timedelta
4
  import xml.etree.ElementTree as ET
5
  import json
 
 
 
 
 
6
 
7
  def get_station_info(pattern, client_id, api_key):
8
  """Ermittelt EVA-Nummer und offiziellen Namen."""
 
41
 
42
  def search_next_3_connections(departure, destination, client_id, api_key):
43
  """Hauptfunktion: Findet die nächsten 3 Verbindungen ab 'jetzt'."""
44
+
45
+ if not all([departure, destination, client_id, api_key]):
46
+ return {"error": "Bitte alle Felder ausfüllen."}
47
 
48
  # 1. IBNRs ermitteln
49
  dep_info = get_station_info(departure, client_id, api_key)
 
75
  pt = dp.get('pt', '') # Geplante Zeit: YYMMDDHHMM
76
  if not pt: continue
77
 
78
+ dep_dt = datetime.strptime(pt, "%y%m%d%H%M")
79
 
80
  # Nur Verbindungen in der Zukunft (ab Zeitstempel jetzt)
81
  if dep_dt >= now:
 
83
  found_connections.append({
84
  "startort": dep_info['name'],
85
  "zielort": dest_info['name'],
86
+ "abfahrtszeit": dep_dt.strftime("%d.%m.%Y %H:%M"),
87
  "gleis": dp.get('pp', 'n/a'),
88
  "zug": f"{tl.get('c', '')} {tl.get('n', '')}",
89
  "_dt": dep_dt # Hilfsfeld zum Sortieren
 
103
 
104
  # --- Gradio UI ---
105
 
106
+ def ui_wrapper(dep, dest, cid, akey):
107
+ results = search_next_3_connections(dep, dest, cid, akey)
108
  return json.dumps(results, indent=4, ensure_ascii=False)
109
 
110
  with gr.Blocks(title="DB JSON Fahrplan", theme=gr.themes.Soft()) as demo:
111
  gr.Markdown("# 🚆 DB Verbindungs-Suche (JSON)")
112
  gr.Markdown("Ermittelt die nächsten 3 Verbindungen ab dem aktuellen Zeitstempel.")
113
+
114
+ with gr.Row():
115
+ cid_input = gr.Textbox(label="DB Client ID", type="password")
116
+ akey_input = gr.Textbox(label="DB API Key", type="password")
117
 
118
  with gr.Row():
119
  dep_input = gr.Textbox(label="Abfahrtsort", placeholder="z.B. Berlin")
 
122
  btn = gr.Button("Suchen", variant="primary")
123
  output = gr.JSON(label="JSON Ergebnis") # Nutzt Gradio JSON Komponente für schöneres Format
124
 
125
+ btn.click(fn=ui_wrapper, inputs=[dep_input, dest_input, cid_input, akey_input], outputs=output)
126
 
127
  if __name__ == "__main__":
128
  demo.launch()