The code uses the XML-based OC Transpo data feed to create a list of the bus name, where it's headed, and the times. Keep in mind that I am a beginner at python so any advice at all is appreciated.
import subprocess, pprint
from bs4 import BeautifulSoup
def format_set(result_set):
new_set = []
for el in result_set:
new_set.append(str(el.get_text()))
return new_set
def get_stop_number():
print('Please enter your desired stop number (or \'quit\'):')
stop_numb = raw_input('> ')
try:
return int(stop_numb)
except:
print('Exiting...')
return 'quit'
def get_stop_info(stopNo):
try:
output = subprocess.check_output(('curl -d "appID=ba91e757&apiKey=…'
'&stopNo={}&format=xml" https://api.octranspo1.com/v1.2/GetNextTripsForStopAllRoutes').format(stopNo), shell=True)
soup = BeautifulSoup(output, 'xml')
except:
print('An error occured!')
return None
summary = []
for el in soup.find_all('Route'):
routeNo = int(el.find('RouteNo').get_text())
routeHeading = str(el.find('RouteHeading').get_text())
times = format_set(el.find_all('TripStartTime'))
x = [routeNo, routeHeading, times]
summary.append(x)
return summary
def is_empty(any_structure):
if any_structure:
return False
else:
return True
if __name__ == '__main__':
while True:
stop_number = get_stop_number()
if stop_number == 'quit':
break
summary = get_stop_info(stop_number)
pprint.pprint(summary)
Sample output:
Please enter your desired stop number (or 'quit'): > 3058 % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 7756 100 7679 100 77 4654 46 0:00:01 0:00:01 --:--:-- 4656 [[92, 'St-Laurent', ['11:30', '12:00', '12:30']], [92, 'Stittsville', []], [96, 'St-Laurent', ['10:51', '11:21', '11:51']], [96, 'Terry Fox', ['10:09', '10:39', '11:09']], [118, 'Hurdman', ['11:20', '11:40', '12:00']], [118, 'Kanata', []], [162, 'Stittsville', ['11:55', '12:55', '13:55']], [162, 'Terry Fox', []], [168, 'Bridlewood', ['11:35', '12:05', '12:35']], [168, 'Terry Fox', []]] Please enter your desired stop number (or 'quit'): > quit Exiting...