Handle weboob exceptions, fix issue #25

This commit is contained in:
Lucas Verney 2017-04-28 09:45:21 +02:00
parent 0fb5f28184
commit bd3e599d12
2 changed files with 44 additions and 9 deletions

View File

@ -29,6 +29,7 @@ def filter_flats(config, flats_list, fetch_details=True):
:param flats_list: The initial list of flat objects to filter. :param flats_list: The initial list of flat objects to filter.
:return: A dict mapping flat status and list of flat objects. :return: A dict mapping flat status and list of flat objects.
""" """
# pylint: disable=locally-disabled,redefined-variable-type
# Add the flatisfy metadata entry and prepare the flat objects # Add the flatisfy metadata entry and prepare the flat objects
flats_list = metadata.init(flats_list) flats_list = metadata.init(flats_list)

View File

@ -17,6 +17,7 @@ LOGGER = logging.getLogger(__name__)
try: try:
from weboob.capabilities.housing import Query from weboob.capabilities.housing import Query
from weboob.core.bcall import CallErrors
from weboob.core.ouiboube import WebNip from weboob.core.ouiboube import WebNip
from weboob.tools.json import WeboobEncoder from weboob.tools.json import WeboobEncoder
except ImportError: except ImportError:
@ -103,15 +104,31 @@ class WeboobProxy(object):
query = Query() query = Query()
query.cities = [] query.cities = []
for postal_code in postal_codes: for postal_code in postal_codes:
matching_cities = []
try: try:
for city in self.webnip.do("search_city", postal_code): for city in self.webnip.do("search_city", postal_code):
query.cities.append(city) matching_cities.append(city)
except IndexError: except CallErrors as exc:
# If an error occured, just log it
LOGGER.error( LOGGER.error(
(
"An error occured while building query for "
"postal code %s: %s"
),
postal_code,
str(exc)
)
if not matching_cities:
# If postal code gave no match, warn the user
LOGGER.warn(
"Postal code %s could not be matched with a city.", "Postal code %s could not be matched with a city.",
postal_code postal_code
) )
return None
# Append the matched cities to the query
for city in matching_cities:
query.cities.append(city)
try: try:
query.house_types = [ query.house_types = [
@ -154,11 +171,18 @@ class WeboobProxy(object):
""" """
housings = [] housings = []
# TODO: Handle max_entries better # TODO: Handle max_entries better
try:
for housing in itertools.islice( for housing in itertools.islice(
self.webnip.do('search_housings', query), self.webnip.do('search_housings', query),
max_entries max_entries
): ):
housings.append(json.dumps(housing, cls=WeboobEncoder)) housings.append(json.dumps(housing, cls=WeboobEncoder))
except CallErrors as exc:
# If an error occured, just log it
LOGGER.error(
"An error occured while fetching the housing posts: %s",
str(exc)
)
return housings return housings
def info(self, full_flat_id): def info(self, full_flat_id):
@ -169,13 +193,23 @@ class WeboobProxy(object):
(ID@BACKEND) (ID@BACKEND)
:return: The details in JSON. :return: The details in JSON.
""" """
housing = {}
flat_id, backend_name = full_flat_id.rsplit("@", 1) flat_id, backend_name = full_flat_id.rsplit("@", 1)
backend = next( backend = next(
backend backend
for backend in self.backends for backend in self.backends
if backend.name == backend_name if backend.name == backend_name
) )
try:
housing = backend.get_housing(flat_id) housing = backend.get_housing(flat_id)
except CallErrors as exc:
# If an error occured, just log it
LOGGER.error(
"An error occured while fetching housing %s: %s",
full_flat_id,
str(exc)
)
housing.id = full_flat_id # Otherwise, we miss the @backend afterwards housing.id = full_flat_id # Otherwise, we miss the @backend afterwards
return json.dumps(housing, cls=WeboobEncoder) return json.dumps(housing, cls=WeboobEncoder)