def get_destinations(station, direction, time_to_stop, max_arrivals, show_missed): '''Help return a parsed prediction.''' soup = BeautifulStoneSoup(functions.get_xml("http://api.bart.gov/api/etd.aspx?cmd=etd&orig=" + station + direction + "&key=MW9S-E7SL-26DU-VV8V"), selfClosingTags=[]) routes = soup.findAll('etd') list = [] for route in routes: trains = route.findAll('estimate') if not show_missed: trains = filter(lambda train: True if functions.get_leave_at(time_to_stop, train.minutes.contents[0]) != -1 else False, trains) trains = trains[:max_arrivals] list.append({"title": route.destination.contents[0], "vehicles" : [{"minutes": train.minutes.contents[0]} for train in trains]}) return list
def get_directions(stop, max_arrivals, show_missed): '''Return a parsed prediction.''' try: html = functions.get_xml('http://www.metrotransit.org/Mobile/NexTripText.aspx?route=' + str(stop.line_tag) + '&direction=' + str(stop.direction_tag) + '&stop=' + str(stop.stop_tag)) except: return ['http://www.metrotransit.org/Mobile/NexTripText.aspx?route=' + str(stop.line_tag) + '&direction=' + str(stop.direction_tag) + '&stop=' + str(stop.stop_tag)] soup = BeautifulSoup(html) current_time = soup.html.body.find('span', 'nextripCurrentTime').string[14:-3] (current_hours, current_minutes) = current_time.split(":") current_hours = int(current_hours) current_minutes = int(current_minutes) direction = filter(lambda x: x["tag"] == stop.direction_tag, directions(stop.agency_tag, stop.line_tag))[0] predictions = [] departTable = soup.html.body.find('div','nextripDepartures') if not departTable: return [] rows = departTable.findAll(attrs={'class':re.compile(r'\bdata\b')}) for row in rows: minutes = row.find(attrs={'class':re.compile(r'\bcol3\b')}) actualTime = ('red' not in minutes['class'].split(' ')) if actualTime: try: if minutes.string == "Due": minutes = 0 else: minutes = int(minutes.string[:-4]) except: logging.warning("PARSING BUS PREDICTION: " + str(row)) else: (scheduled_hours, scheduled_minutes) = minutes.string.split(":") scheduled_hours = int(scheduled_hours) scheduled_minutes = int(scheduled_minutes) minutes = (scheduled_hours - current_hours)%12 * 60 + (scheduled_minutes - current_minutes) predictions.append({"id" : "0", "minutes": minutes}) if not show_missed: predictions = filter(lambda prediction: True if functions.get_leave_at(stop.time_to_stop, prediction['minutes']) != -1 else False, predictions) predictions = sorted(predictions, key=lambda x: int(x['minutes']))[:max_arrivals] return [{"title": "", "destinations": [{"title": direction["title"], "vehicles": predictions}]}]