Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,34 +1,57 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
-
|
|
|
|
| 5 |
|
| 6 |
session = requests.Session()
|
| 7 |
session.headers.update({
|
| 8 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
|
| 9 |
"Accept": "application/json, text/plain, */*",
|
| 10 |
"Accept-Language": "de-DE,de;q=0.9,en;q=0.8",
|
| 11 |
-
"Referer": "https://
|
| 12 |
-
"Sec-Fetch-Dest": "empty",
|
| 13 |
-
"Sec-Fetch-Mode": "cors",
|
| 14 |
-
"Sec-Fetch-Site": "same-origin",
|
| 15 |
})
|
| 16 |
|
| 17 |
|
| 18 |
def _search_places(query: str) -> list:
|
| 19 |
r = session.get(
|
| 20 |
-
|
| 21 |
-
params={
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
timeout=15,
|
| 23 |
)
|
| 24 |
if r.status_code != 200:
|
| 25 |
raise RuntimeError(f"HTTP {r.status_code}: {r.text[:300]}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
try:
|
| 27 |
-
data = r.json()
|
| 28 |
except ValueError:
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
if isinstance(data, dict):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
data = next((v for v in data.values() if isinstance(v, list)), [])
|
|
|
|
| 32 |
return data if isinstance(data, list) else []
|
| 33 |
|
| 34 |
|
|
@@ -54,14 +77,16 @@ def find_station(query: str):
|
|
| 54 |
if not places:
|
| 55 |
continue
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 59 |
if not pool:
|
| 60 |
continue
|
| 61 |
|
| 62 |
ql = query.lower()
|
| 63 |
for p in pool:
|
| 64 |
-
|
|
|
|
| 65 |
return p, debug
|
| 66 |
return pool[0], debug
|
| 67 |
return None, debug
|
|
@@ -75,8 +100,8 @@ def search_ui(query: str):
|
|
| 75 |
return station, log_text
|
| 76 |
|
| 77 |
|
| 78 |
-
with gr.Blocks(title="DB Bahnhofssuche") as demo:
|
| 79 |
-
gr.Markdown("## Deutsche Bahn Bahnhofssuche")
|
| 80 |
with gr.Row():
|
| 81 |
query_input = gr.Textbox(
|
| 82 |
label="Bahnhof / Ort",
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
|
| 4 |
+
# Public HAFAS endpoint for station/location lookup
|
| 5 |
+
HAFAS_URL = "https://reiseauskunft.bahn.de/bin/ajax-getstop.exe/dn"
|
| 6 |
|
| 7 |
session = requests.Session()
|
| 8 |
session.headers.update({
|
| 9 |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
|
| 10 |
"Accept": "application/json, text/plain, */*",
|
| 11 |
"Accept-Language": "de-DE,de;q=0.9,en;q=0.8",
|
| 12 |
+
"Referer": "https://reiseauskunft.bahn.de/",
|
|
|
|
|
|
|
|
|
|
| 13 |
})
|
| 14 |
|
| 15 |
|
| 16 |
def _search_places(query: str) -> list:
|
| 17 |
r = session.get(
|
| 18 |
+
HAFAS_URL,
|
| 19 |
+
params={
|
| 20 |
+
"S": query,
|
| 21 |
+
"getstop": "1",
|
| 22 |
+
"type": "all",
|
| 23 |
+
"format": "json",
|
| 24 |
+
},
|
| 25 |
timeout=15,
|
| 26 |
)
|
| 27 |
if r.status_code != 200:
|
| 28 |
raise RuntimeError(f"HTTP {r.status_code}: {r.text[:300]}")
|
| 29 |
+
|
| 30 |
+
# HAFAS sometimes returns JSON wrapped in a callback function or raw JSON array/object.
|
| 31 |
+
# Let's clean it up safely:
|
| 32 |
+
text = r.text.strip()
|
| 33 |
+
if text.startswith("SLs.sls("):
|
| 34 |
+
text = text[8:-1] # Strip wrapper function if present
|
| 35 |
+
|
| 36 |
try:
|
| 37 |
+
data = r.json() if not text.startswith("SLs.sls") else requests.compat.json.loads(text)
|
| 38 |
except ValueError:
|
| 39 |
+
# Fallback to standard json parsing of response text
|
| 40 |
+
import json
|
| 41 |
+
try:
|
| 42 |
+
data = json.loads(text)
|
| 43 |
+
except Exception:
|
| 44 |
+
raise RuntimeError(f"Keine JSON-Antwort: {r.text[:300]}")
|
| 45 |
+
|
| 46 |
+
# HAFAS stopfinder typically returns a list or an object containing a list of stations
|
| 47 |
if isinstance(data, dict):
|
| 48 |
+
# Look for typical list keys in HAFas responses (e.g., 'suggestions' or 'stoplist')
|
| 49 |
+
for key in ["suggestions", "locations", "stoplist"]:
|
| 50 |
+
if key in data and isinstance(data[key], list):
|
| 51 |
+
return data[key]
|
| 52 |
+
# Or look for any value that is a list
|
| 53 |
data = next((v for v in data.values() if isinstance(v, list)), [])
|
| 54 |
+
|
| 55 |
return data if isinstance(data, list) else []
|
| 56 |
|
| 57 |
|
|
|
|
| 77 |
if not places:
|
| 78 |
continue
|
| 79 |
|
| 80 |
+
# HAFAS fields typically use 'extId' or 'id' for station IDs and 'name' for station names
|
| 81 |
+
stations = [p for p in places if p.get("type") in ("ST", "stop", "station") or p.get("id") or p.get("extId")]
|
| 82 |
+
pool = stations or places
|
| 83 |
if not pool:
|
| 84 |
continue
|
| 85 |
|
| 86 |
ql = query.lower()
|
| 87 |
for p in pool:
|
| 88 |
+
name = p.get("name") or p.get("value") or ""
|
| 89 |
+
if name.lower() == ql:
|
| 90 |
return p, debug
|
| 91 |
return pool[0], debug
|
| 92 |
return None, debug
|
|
|
|
| 100 |
return station, log_text
|
| 101 |
|
| 102 |
|
| 103 |
+
with gr.Blocks(title="DB Bahnhofssuche (HAFAS)") as demo:
|
| 104 |
+
gr.Markdown("## Deutsche Bahn Bahnhofssuche (HAFAS Endpoint)")
|
| 105 |
with gr.Row():
|
| 106 |
query_input = gr.Textbox(
|
| 107 |
label="Bahnhof / Ort",
|