Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,70 +1,79 @@
|
|
1 |
import gradio as gr
|
2 |
import requests
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
6 |
|
7 |
-
def
|
8 |
"""
|
9 |
-
|
10 |
-
Expects:
|
11 |
-
- origin: starting location as a string.
|
12 |
-
- destination: ending location as a string.
|
13 |
-
- date: journey date in YYYY-MM-DD format.
|
14 |
-
- time: journey time in HH:MM 24-hour format.
|
15 |
|
16 |
-
|
|
|
17 |
"""
|
18 |
-
payload = {
|
19 |
-
"origin": origin,
|
20 |
-
"destination": destination,
|
21 |
-
"date": date,
|
22 |
-
"time": time
|
23 |
-
}
|
24 |
-
headers = {
|
25 |
-
"Content-Type": "application/json",
|
26 |
-
"User-Agent": "Mozilla/5.0"
|
27 |
-
}
|
28 |
-
|
29 |
try:
|
30 |
-
response = requests.
|
31 |
response.raise_for_status()
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
|
34 |
-
|
35 |
-
if not
|
36 |
-
return "No
|
37 |
|
38 |
-
|
39 |
-
for
|
40 |
-
|
41 |
-
|
42 |
-
route
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
result_str += f"**Arrival:** {arr_time}\n"
|
49 |
-
result_str += f"**Stops:** {stops_str}\n"
|
50 |
-
result_str += "--------------------------\n"
|
51 |
|
52 |
-
|
|
|
|
|
53 |
except Exception as e:
|
54 |
-
return f"Error
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
56 |
-
# Define the Gradio interface.
|
57 |
iface = gr.Interface(
|
58 |
-
fn=
|
59 |
-
inputs=
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
gr.Textbox(label="Time (HH:MM, 24-hour format)")
|
64 |
-
],
|
65 |
-
outputs=gr.Textbox(label="Journey Schedule"),
|
66 |
-
title="Algoa Bus Journey Planner",
|
67 |
-
description="Enter your journey details to retrieve the latest bus schedule for Algoa Bus Company in Port Elizabeth."
|
68 |
)
|
69 |
|
70 |
if __name__ == "__main__":
|
|
|
1 |
import gradio as gr
|
2 |
import requests
|
3 |
+
from bs4 import BeautifulSoup
|
4 |
|
5 |
+
# URL for the schedule page (old schedule link provided)
|
6 |
+
SCHEDULE_URL = "http://www.algoabus.co.za/port-elizabeth-bus-routes/route_list/schedule.aspx"
|
7 |
|
8 |
+
def fetch_schedule():
|
9 |
"""
|
10 |
+
Scrapes the schedule page and extracts bus routes and their schedules.
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
This function assumes the schedule is contained in a table with a certain structure.
|
13 |
+
Adjust the selectors if Algoa updates their page design.
|
14 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
try:
|
16 |
+
response = requests.get(SCHEDULE_URL, timeout=10)
|
17 |
response.raise_for_status()
|
18 |
+
html = response.text
|
19 |
+
|
20 |
+
soup = BeautifulSoup(html, "html.parser")
|
21 |
+
schedule_data = {}
|
22 |
+
|
23 |
+
# Assume the schedule is in a table with class "scheduleTable".
|
24 |
+
# (This is based on a guess—inspect the page source to confirm the actual class or id.)
|
25 |
+
table = soup.find("table", {"class": "scheduleTable"})
|
26 |
+
if not table:
|
27 |
+
return None, "Could not locate the schedule table on the page."
|
28 |
|
29 |
+
rows = table.find_all("tr")
|
30 |
+
if not rows or len(rows) < 2:
|
31 |
+
return None, "No schedule rows found in the table."
|
32 |
|
33 |
+
# Assuming the first row is header, start parsing from the second row.
|
34 |
+
for row in rows[1:]:
|
35 |
+
cols = row.find_all("td")
|
36 |
+
# We assume that at least two columns exist:
|
37 |
+
# Column 0: Bus route name; Column 1: Schedule details.
|
38 |
+
if len(cols) >= 2:
|
39 |
+
route_name = cols[0].get_text(strip=True)
|
40 |
+
# The schedule details might include multiple departure times separated by newlines.
|
41 |
+
schedule_info = cols[1].get_text(separator="\n", strip=True)
|
42 |
+
schedule_data[route_name] = schedule_info
|
|
|
|
|
|
|
43 |
|
44 |
+
if not schedule_data:
|
45 |
+
return None, "No schedule data could be extracted from the table."
|
46 |
+
return schedule_data, None
|
47 |
except Exception as e:
|
48 |
+
return None, f"Error fetching schedule: {str(e)}"
|
49 |
+
|
50 |
+
# Fetch schedule data at startup.
|
51 |
+
schedule_data, error = fetch_schedule()
|
52 |
+
|
53 |
+
# If scraping failed, we fallback to an empty dictionary.
|
54 |
+
if error:
|
55 |
+
print("Scraping error:", error)
|
56 |
+
schedule_data = {}
|
57 |
+
|
58 |
+
def get_route_schedule(route):
|
59 |
+
"""
|
60 |
+
Returns the schedule details for a selected bus route.
|
61 |
+
"""
|
62 |
+
if not schedule_data:
|
63 |
+
return "Schedule data is not available at the moment."
|
64 |
+
if route not in schedule_data:
|
65 |
+
return "Selected route not found in the schedule data."
|
66 |
+
return f"**Route:** {route}\n\n**Schedule:**\n{schedule_data[route]}"
|
67 |
+
|
68 |
+
# Create a dropdown list from the scraped routes (or a placeholder if no data was retrieved).
|
69 |
+
route_choices = list(schedule_data.keys()) if schedule_data else ["No Data Available"]
|
70 |
|
|
|
71 |
iface = gr.Interface(
|
72 |
+
fn=get_route_schedule,
|
73 |
+
inputs=gr.Dropdown(choices=route_choices, label="Select Bus Route"),
|
74 |
+
outputs=gr.Textbox(label="Bus Schedule"),
|
75 |
+
title="Algoa Bus Schedule",
|
76 |
+
description="Select a bus route to view its schedule for Algoa Bus Company in Port Elizabeth."
|
|
|
|
|
|
|
|
|
|
|
77 |
)
|
78 |
|
79 |
if __name__ == "__main__":
|