def testProbeForResponse(self): # Tests retrieval from server, parsing, and # conversion of XML to Python dictionary. url = 'http://www.google.com' response = probeForResponse( url ) #print( response ) self.assertTrue( response != None )
def main(): """ Connect to the OneBusAway server and perform certain behaviors. """ global currLat global currLong readCommandLineArgs() # Get all possible regions regionOptionsURL = "http://regions.onebusaway.org/regions-v3.xml" regionServerResponse = probeForResponse( regionOptionsURL ) regionDict = convertToDict( regionServerResponse ) regionsList = BusWorker.makeRegions( regionDict ) # Determines if user location is supported by the server user_region = BusWorker.getRegionFromCoordinates(regionsList, currLat, currLong) if( user_region == None ): print("Your location is not supported,", end=" ") print("or you live in a region without bus services!") sys.exit() serverURL = user_region.getRegionURL() # Get API key for OBA OBA_APIKey = getAPIKey( "keys/OBA_API.key" ) # Get all bus stops near input location nearbyStopURL = serverURL + "api/where/stops-for-location.xml?key=" + \ OBA_APIKey + "&lat=" + str(currLat) + "&lon=" + str(currLong) serverResponse = probeForResponse( nearbyStopURL ) stopDict = convertToDict( serverResponse ) # Store nearest stop information try: allStops = BusWorker.makeStops( stopDict ) except AssertionError as x: print( x ) sys.exit() # Display bus stops chosenStopNdx = BusWorker.displayBusStops( allStops ) print() print("--> You chose bus stop:") print(allStops[chosenStopNdx]) # Get arrival information for a stop stopURL = serverURL + "where/arrivals-and-departures-for-stop/" + \ allStops[chosenStopNdx].getStopID() + ".xml?key=" + OBA_APIKey timeServerResponse = probeForResponse( stopURL ) timeDict = convertToDict( timeServerResponse ) # Get route for chosen stop try: allRoutes = BusWorker.makeRoutes( timeDict ) except AssertionError as x: print( x ) sys.exit() # Display bus routes chosenRouteNdx = BusWorker.displayBusRoutes( allRoutes ) chosenStop = allStops[chosenStopNdx] stopLat = chosenStop.getLat() stopLong = chosenStop.getLong() # API Key for Google Maps GOOGLE_APIKey = getAPIKey( "keys/GOOGLE_API.key") distanceTimeURL = "https://maps.googleapis.com/maps/api/distancematrix/xml?" + \ "origins=" + str(currLat) + "," + str(currLong) + "&destinations=" + \ str(stopLat) + "," + str(stopLong) + "&mode=walking" + "&key=" + str(GOOGLE_APIKey) googleResponse = probeForResponse( distanceTimeURL ) googleDict = convertToDict( googleResponse ) print() print("----------------------- RESULTS -----------------------") print() print( "--> You are at location (" + str(currLat) + "," + str(currLong) + "), which is at address:" ) print( "\t" + str(googleDict['origin_address']) ) print( "--> The bus stop is at location (" + str( stopLat ) + \ "," + str( stopLong ) + "), which is at address:" ) print( "\t" + str(googleDict['destination_address']) ) print() curTime = makeEpochReadable( int(timeDict['currentTime']) ) print( "--> The current time is:\t" + str(curTime) ) arriv = int( allRoutes[chosenRouteNdx].getArrivalTime() ) busArrivalTime = makeEpochReadable( arriv ) print( "--> The bus will arrive at:\t" + str(busArrivalTime) ) print() # Time information in seconds busArrivalInterval = hmsToSeconds( str(busArrivalTime - curTime) ) getToBusInterval = int( googleDict['row']['element']['duration']['value'] ) print( "--> The bus will arrive in " + str( int(busArrivalInterval / 60) ) + " mins") print( "--> It will take you " + str( googleDict['row']['element']['duration']['text']) + \ " to walk to the bus stop.") # Print out final results if( getToBusInterval > ( busArrivalInterval + 60 ) ): print( "--> You probably won't make it in time! :(") elif ( (getToBusInterval >= (busArrivalInterval - 60)) and (getToBusInterval <= (busArrivalInterval + 60)) ): print( "--> It's a close one, you may or may not make it!!! :O") else: print( "--> You'll make it in time! :)")