Fix view.sh startup location
[busui.git] / schedule_viewer.py
blob:a/schedule_viewer.py -> blob:b/schedule_viewer.py
--- a/schedule_viewer.py
+++ b/schedule_viewer.py
@@ -282,6 +282,23 @@
     result.sort(key = lambda x: x[1:3])
     return result
 
+  def handle_json_GET_routesearch(self, params):
+    """Return a list of routes with matching short name."""
+    schedule = self.server.schedule
+    routeshortname = params.get('routeshortname', None)
+    result = []
+    for r in schedule.GetRouteList():
+      if r.route_short_name == routeshortname:
+        servicep = None
+        for t in schedule.GetTripList():
+          if t.route_id == r.route_id:
+            servicep = t.service_period
+            break
+        result.append( (r.route_id, r.route_short_name, r.route_long_name, servicep.service_id) )
+    result.sort(key = lambda x: x[1:3])
+    return result
+
+
   def handle_json_GET_routerow(self, params):
     schedule = self.server.schedule
     route = schedule.GetRoute(params.get('route', None))
@@ -299,8 +316,19 @@
         except:
           print "Error for GetStartTime of trip #" + t.trip_id + sys.exc_info()[0]
         else:
-            result.append ( (starttime, t.trip_id) )
-    return sorted(result, key=lambda trip: trip[0])
+          cursor = t._schedule._connection.cursor()
+          cursor.execute(
+              'SELECT arrival_secs,departure_secs FROM stop_times WHERE '
+              'trip_id=? ORDER BY stop_sequence DESC LIMIT 1', (t.trip_id,))
+          (arrival_secs, departure_secs) = cursor.fetchone()
+          if arrival_secs != None:
+            endtime = arrival_secs
+          elif departure_secs != None:
+            endtime = departure_secs
+          else:
+            endtime =0
+          result.append ( (starttime, t.trip_id, endtime) )
+    return sorted(result, key=lambda trip: trip[2])
   
   def handle_json_GET_triprows(self, params):
     """Return a list of rows from the feed file that are related to this
@@ -477,8 +505,8 @@
     result = {}
     for trip in trips:
       route = schedule.GetRoute(trip.route_id)
-      if not trip.route_short_name+route.route_long_name in result:
-        result[trip.route_short_name+route.route_long_name] = (route.route_id, route.route_short_name, route.route_long_name, trip.trip_id)
+      if not route.route_short_name+route.route_long_name+trip.service_id in result:
+        result[route.route_short_name+route.route_long_name+trip.service_id] = (route.route_id, route.route_short_name, route.route_long_name, trip.trip_id, trip.service_id)
     return result
     
   def handle_json_GET_stopalltrips(self, params):
@@ -509,6 +537,8 @@
       if service_period == None or trip.service_id == service_period:
         result.append((time, (trip.trip_id, trip_name, trip.service_id), tp))
     return result
+  
+
 
   def handle_json_GET_stoptrips(self, params):
     """Given a stop_id and time in seconds since midnight return the next