def get_departures(self, url): """ Get list of next departures as Departure objects """ #TODO parse line name and direction for station site parsing if not url: # FIXME prevent from calling this method with None print "ERROR empty url" return [] # open url for 90 min timeslot / get departure for next 90 min retry = 0 tries = 2 # try a second time before return empty list while retry < tries: # http://www.wienerlinien.at/itip/linienwahl/anzeige.php?PHPSESSID=8ojk9788jlp69mbqtnqvqaqkg5&departureSizeTimeSlot=70&sortType=abfSort try: urlopen(url) html = urlopen(url + "&departureSizeTimeSlot=70").read() except HTTPError: print "HTTPError at %s" % url return [] dep = self.parse_departures(html) if dep: return dep retry += 1 if retry == tries: return [] sleep(0.5)
def get_departures_by_station(self, station): """ Get list of Departures for one station """ # TODO 1. Error handling # TODO 2. more error handling # TODO 3. ultimative error handling station = station.encode('UTF-8') html = urlopen(defaults.departures_by_station % quote_plus(station)).read() li = BeautifulSoup(html).ul.findAll('li') if li[0].a: # calculate levenshtein distance of results st = map(lambda x: (distance(station, x.a.text.encode('UTF-8')), x.a.text.encode('UTF-8'), x.a['href']), li) # take result with lowest levenshtein distance s = min(st) lnk = s[2] if len(st) > 1: print "Multiple results found, using best match:", s[1] html = urlopen(defaults.qando + lnk).read() dep = self.parse_departures_by_station(html) return dep
def get_stations(self, name): """ Get station by direction {'Directionname': [('Station name', 'url')]} """ if not name in self.lines: return {} st = Stations(name) if not st: urlopen(defaults.stations % name) st.update(self.parse_stations(urlopen(defaults.stations % name).read())) return st
def lines(self): """ Dictionary of Line names with url as value """ if not self._lines: print "Load lines" self._lines.update(self.parse_lines(urlopen(defaults.line_overview).read())) #self._lines = self.parse_lines(urlopen(defaults.line_overview).read()) if not self._lines: print "Error fetching lines" return self._lines
def search(origin_tuple, destination_tuple, dtime=None): """ build route request returns html result (as urllib response) """ if not dtime: dtime = datetime.now() origin, origin_type = origin_tuple origin, origin_city = split_station(origin) destination, destination_type = destination_tuple destination, destination_city = split_station(destination) if origin_type is None: origin_type = guess_location_type(origin) print 'Guessed origin type:', origin_type if destination_type is None: destination_type = guess_location_type(destination) print 'Guessed destination type:', destination_type if (origin_type not in POSITION_TYPES or destination_type not in POSITION_TYPES): raise ParserError('Invalid position type') post = defaults.search_post post['name_origin'] = origin post['type_origin'] = origin_type post['name_destination'] = destination post['type_destination'] = destination_type post['itdDateDayMonthYear'] = dtime.strftime('%d.%m.%Y') post['itdTime'] = dtime.strftime('%H:%M') post['place_origin'] = origin_city post['place_destination'] = destination_city params = urlencode(post) url = '%s?%s' % (defaults.action, params) #print url return urlopen(url)