Danial7 commited on
Commit
740031c
Β·
verified Β·
1 Parent(s): 5fcc2f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -11
app.py CHANGED
@@ -7,14 +7,18 @@ import feedparser
7
 
8
  load_dotenv()
9
 
 
10
  ORS_API_KEY = os.getenv("ORS_API_KEY")
11
  OPENWEATHER_API_KEY = os.getenv("OPENWEATHER_API_KEY")
12
 
 
13
  geolocator = Nominatim(user_agent="commute_planner")
14
 
 
15
  st.title("πŸš— Free Commute Planner")
16
  st.markdown("Estimate travel time, check weather, AQI, and traffic-related news β€” no credit card needed!")
17
 
 
18
  source = st.text_input("Enter your current location:")
19
  destination = st.text_input("Enter your destination:")
20
 
@@ -34,7 +38,7 @@ if st.button("Plan My Commute") and source and destination:
34
  if not src_coords or not dest_coords:
35
  st.error("Could not find coordinates. Please enter valid locations.")
36
  else:
37
- # OpenRouteService: Travel Time
38
  ors_url = "https://api.openrouteservice.org/v2/directions/driving-car"
39
  headers = {
40
  "Authorization": ORS_API_KEY,
@@ -47,15 +51,25 @@ if st.button("Plan My Commute") and source and destination:
47
  ]
48
  }
49
  response = requests.post(ors_url, json=data, headers=headers)
 
50
  if response.status_code == 200:
51
  result = response.json()
52
- duration_sec = result['features'][0]['properties']['segments'][0]['duration']
53
- duration_min = round(duration_sec / 60, 2)
54
- st.success(f"πŸ•’ Estimated Travel Time: {duration_min} minutes")
 
 
 
 
 
55
  else:
56
- st.warning("Could not retrieve travel time. Please check your API key and inputs.")
 
 
 
 
57
 
58
- # OpenWeatherMap: Temperature
59
  weather_url = "https://api.openweathermap.org/data/2.5/weather"
60
  weather_params = {
61
  "lat": src_coords[0],
@@ -67,7 +81,7 @@ if st.button("Plan My Commute") and source and destination:
67
  temp = weather.get("main", {}).get("temp", "N/A")
68
  st.info(f"🌑️ Current Temperature: {temp}Β°C")
69
 
70
- # OpenWeatherMap: AQI
71
  aqi_url = "https://api.openweathermap.org/data/2.5/air_pollution"
72
  aqi_params = {
73
  "lat": src_coords[0],
@@ -85,14 +99,16 @@ if st.button("Plan My Commute") and source and destination:
85
  }
86
  st.info(f"πŸŒ€ Air Quality Index: {aqi} - {aqi_levels.get(aqi, 'Unknown')}")
87
 
88
- # Traffic News using RSS
89
  st.subheader("πŸ“° Traffic-Related News")
90
- feed_url = "https://www.rssmix.com/u/17853882/rss.xml" # Replace with a real local traffic feed or use another from your city
91
- feed = feedparser.parse(feed_url)
 
92
  count = 0
93
  for entry in feed.entries:
94
- if "traffic" in entry.title.lower() or "accident" in entry.title.lower() or "road" in entry.title.lower():
95
  st.markdown(f"[{entry.title}]({entry.link})")
96
  count += 1
 
97
  if count == 0:
98
  st.write("No recent traffic-related updates found.")
 
7
 
8
  load_dotenv()
9
 
10
+ # Load API keys from environment variables
11
  ORS_API_KEY = os.getenv("ORS_API_KEY")
12
  OPENWEATHER_API_KEY = os.getenv("OPENWEATHER_API_KEY")
13
 
14
+ # Initialize geolocator
15
  geolocator = Nominatim(user_agent="commute_planner")
16
 
17
+ # Streamlit UI
18
  st.title("πŸš— Free Commute Planner")
19
  st.markdown("Estimate travel time, check weather, AQI, and traffic-related news β€” no credit card needed!")
20
 
21
+ # Inputs
22
  source = st.text_input("Enter your current location:")
23
  destination = st.text_input("Enter your destination:")
24
 
 
38
  if not src_coords or not dest_coords:
39
  st.error("Could not find coordinates. Please enter valid locations.")
40
  else:
41
+ # βœ… Travel Time via OpenRouteService
42
  ors_url = "https://api.openrouteservice.org/v2/directions/driving-car"
43
  headers = {
44
  "Authorization": ORS_API_KEY,
 
51
  ]
52
  }
53
  response = requests.post(ors_url, json=data, headers=headers)
54
+
55
  if response.status_code == 200:
56
  result = response.json()
57
+ try:
58
+ duration_sec = result['features'][0]['properties']['segments'][0]['duration']
59
+ duration_min = round(duration_sec / 60, 2)
60
+ st.success(f"πŸ•’ Estimated Travel Time: {duration_min} minutes")
61
+ except (KeyError, IndexError):
62
+ st.error("Received a response but couldn't parse travel time.")
63
+ st.markdown("**Response from OpenRouteService:**")
64
+ st.json(result)
65
  else:
66
+ st.error(f"OpenRouteService API error: {response.status_code}")
67
+ try:
68
+ st.json(response.json())
69
+ except:
70
+ st.write(response.text)
71
 
72
+ # βœ… Weather from OpenWeatherMap
73
  weather_url = "https://api.openweathermap.org/data/2.5/weather"
74
  weather_params = {
75
  "lat": src_coords[0],
 
81
  temp = weather.get("main", {}).get("temp", "N/A")
82
  st.info(f"🌑️ Current Temperature: {temp}Β°C")
83
 
84
+ # βœ… Air Quality Index from OpenWeatherMap
85
  aqi_url = "https://api.openweathermap.org/data/2.5/air_pollution"
86
  aqi_params = {
87
  "lat": src_coords[0],
 
99
  }
100
  st.info(f"πŸŒ€ Air Quality Index: {aqi} - {aqi_levels.get(aqi, 'Unknown')}")
101
 
102
+ # βœ… Traffic News via RSS Feed
103
  st.subheader("πŸ“° Traffic-Related News")
104
+ rss_url = "https://www.rssmix.com/u/17853882/rss.xml" # Replace with your preferred traffic-related feed
105
+ feed = feedparser.parse(rss_url)
106
+
107
  count = 0
108
  for entry in feed.entries:
109
+ if any(keyword in entry.title.lower() for keyword in ["traffic", "accident", "road", "construction"]):
110
  st.markdown(f"[{entry.title}]({entry.link})")
111
  count += 1
112
+
113
  if count == 0:
114
  st.write("No recent traffic-related updates found.")