mgokg commited on
Commit
37fd47e
·
verified ·
1 Parent(s): 015eda9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -13
app.py CHANGED
@@ -5,10 +5,43 @@ import xml.etree.ElementTree as ET
5
  import json
6
  import os
7
 
8
- # Load credentials from environment variables
9
  CLIENT_ID = os.environ.get('client_id')
10
  API_KEY = os.environ.get('api_key')
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  def get_station_info(pattern, client_id, api_key):
13
  url = f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/station/{pattern}"
14
  headers = {'DB-Client-Id': client_id, 'DB-Api-Key': api_key, 'accept': 'application/xml'}
@@ -59,7 +92,6 @@ def search_next_3_connections(departure, destination, client_id, api_key):
59
  tl = train.find('tl')
60
  if dp is None or tl is None: continue
61
 
62
- # Check if destination is in the path
63
  path = dp.get('ppth', '')
64
  if dest_info['name'].lower() in path.lower() or destination.lower() in path.lower():
65
  pt = dp.get('pt', '')
@@ -68,17 +100,12 @@ def search_next_3_connections(departure, destination, client_id, api_key):
68
  dep_dt = datetime.strptime(pt, "%y%m%d%H%M")
69
 
70
  if dep_dt >= now:
71
- # To get arrival time, we must check the destination station for the same train ID
72
- # For performance, we assume arrival is within +2 hours of departure
73
  arr_time_str = "N/A"
74
  duration_str = "N/A"
75
 
76
- # Search destination timetable for the arrival
77
- # We check the hour of departure and the next hour for arrival
78
  arrival_root = fetch_timetable_hour(dest_info['eva'], dep_dt.strftime("%y%m%d"), dep_dt.strftime("%H"), client_id, api_key)
79
 
80
  if arrival_root is not None:
81
- # Find the train by number (n) and category (c)
82
  for arr_train in arrival_root.findall('.//s'):
83
  arr_tl = arr_train.find('tl')
84
  arr_ar = arr_train.find('ar')
@@ -88,7 +115,6 @@ def search_next_3_connections(departure, destination, client_id, api_key):
88
  arr_dt = datetime.strptime(arr_pt, "%y%m%d%H%M")
89
  arr_time_str = arr_dt.strftime("%H:%M")
90
 
91
- # Calculate duration
92
  diff = arr_dt - dep_dt
93
  duration_str = f"{int(diff.total_seconds() // 60)} min"
94
  break
@@ -105,20 +131,19 @@ def search_next_3_connections(departure, destination, client_id, api_key):
105
 
106
  found_connections.sort(key=lambda x: x['_dt'])
107
 
108
- # Return list of first 3 matches formatted without internal sort key
109
  return [{k: v for k, v in c.items() if k != "_dt"} for c in found_connections[:3]]
110
 
111
  # --- Gradio UI ---
112
 
113
  def ui_wrapper(dep, dest):
114
  if not CLIENT_ID or not API_KEY:
115
- return {"error": "API credentials not configured."}
116
 
117
  results = search_next_3_connections(dep, dest, CLIENT_ID, API_KEY)
118
- return results # Return list directly for gr.JSON
119
 
120
  with gr.Blocks(title="DB JSON Fahrplan", theme=gr.themes.Soft()) as demo:
121
- gr.Markdown("# │ DB │ Deutsche Bahn train connections")
122
 
123
  with gr.Row():
124
  dep_input = gr.Textbox(label="Abfahrtsort", placeholder="z.B. Schweinfurt Hbf")
@@ -126,8 +151,9 @@ with gr.Blocks(title="DB JSON Fahrplan", theme=gr.themes.Soft()) as demo:
126
 
127
  btn = gr.Button("Suchen", variant="primary")
128
  output = gr.JSON(label="JSON Ergebnis")
 
129
 
130
  btn.click(fn=ui_wrapper, inputs=[dep_input, dest_input], outputs=output)
131
 
132
  if __name__ == "__main__":
133
- demo.launch()
 
5
  import json
6
  import os
7
 
8
+ # Lade Anmeldeinformationen aus Umgebungsvariablen
9
  CLIENT_ID = os.environ.get('client_id')
10
  API_KEY = os.environ.get('api_key')
11
 
12
+ def json_to_markdown_table(json_data):
13
+ """
14
+ Konvertiert JSON-Daten in eine professionell formatierte Markdown-Tabelle
15
+ """
16
+ if isinstance(json_data, str):
17
+ data = json.loads(json_data)
18
+ else:
19
+ data = json_data
20
+
21
+ if not data:
22
+ return "Keine Daten vorhanden"
23
+
24
+ header_mapping = {
25
+ "startort": "🚉 Startort",
26
+ "zielort": "🎯 Zielort",
27
+ "abfahrtszeit": "⏰ Abfahrt",
28
+ "ankunftszeitszeit": "⏱️ Ankunft",
29
+ "gleis": "🛤️ Gleis",
30
+ "duration": "⏳ Dauer"
31
+ }
32
+
33
+ columns = list(data[0].keys())
34
+ headers = [header_mapping.get(col, col) for col in columns]
35
+
36
+ markdown = "| " + " | ".join(headers) + " |\n"
37
+ markdown += "|" + "|".join(["---" for _ in headers]) + "|\n"
38
+
39
+ for row in data:
40
+ values = [str(row.get(col, "")) for col in columns]
41
+ markdown += "| " + " | ".join(values) + " |\n"
42
+
43
+ return markdown
44
+
45
  def get_station_info(pattern, client_id, api_key):
46
  url = f"https://apis.deutschebahn.com/db-api-marketplace/apis/timetables/v1/station/{pattern}"
47
  headers = {'DB-Client-Id': client_id, 'DB-Api-Key': api_key, 'accept': 'application/xml'}
 
92
  tl = train.find('tl')
93
  if dp is None or tl is None: continue
94
 
 
95
  path = dp.get('ppth', '')
96
  if dest_info['name'].lower() in path.lower() or destination.lower() in path.lower():
97
  pt = dp.get('pt', '')
 
100
  dep_dt = datetime.strptime(pt, "%y%m%d%H%M")
101
 
102
  if dep_dt >= now:
 
 
103
  arr_time_str = "N/A"
104
  duration_str = "N/A"
105
 
 
 
106
  arrival_root = fetch_timetable_hour(dest_info['eva'], dep_dt.strftime("%y%m%d"), dep_dt.strftime("%H"), client_id, api_key)
107
 
108
  if arrival_root is not None:
 
109
  for arr_train in arrival_root.findall('.//s'):
110
  arr_tl = arr_train.find('tl')
111
  arr_ar = arr_train.find('ar')
 
115
  arr_dt = datetime.strptime(arr_pt, "%y%m%d%H%M")
116
  arr_time_str = arr_dt.strftime("%H:%M")
117
 
 
118
  diff = arr_dt - dep_dt
119
  duration_str = f"{int(diff.total_seconds() // 60)} min"
120
  break
 
131
 
132
  found_connections.sort(key=lambda x: x['_dt'])
133
 
 
134
  return [{k: v for k, v in c.items() if k != "_dt"} for c in found_connections[:3]]
135
 
136
  # --- Gradio UI ---
137
 
138
  def ui_wrapper(dep, dest):
139
  if not CLIENT_ID or not API_KEY:
140
+ return {"error": "API-Anmeldeinformationen nicht konfiguriert."}
141
 
142
  results = search_next_3_connections(dep, dest, CLIENT_ID, API_KEY)
143
+ return results
144
 
145
  with gr.Blocks(title="DB JSON Fahrplan", theme=gr.themes.Soft()) as demo:
146
+ gr.Markdown("# │ DB │ Deutsche Bahn Zugverbindungen")
147
 
148
  with gr.Row():
149
  dep_input = gr.Textbox(label="Abfahrtsort", placeholder="z.B. Schweinfurt Hbf")
 
151
 
152
  btn = gr.Button("Suchen", variant="primary")
153
  output = gr.JSON(label="JSON Ergebnis")
154
+ md_output = gr.Markdown()
155
 
156
  btn.click(fn=ui_wrapper, inputs=[dep_input, dest_input], outputs=output)
157
 
158
  if __name__ == "__main__":
159
+ demo.launch()