No longer make queries with more than three cities

Some postal codes correspond to multiple cities, then sometimes we were
spawning queries with more than three cities. This should no longer be
the case. This fixes https://github.com/Phyks/Flatisfy/issues/10.
This commit is contained in:
Lucas Verney 2017-10-24 15:50:19 -04:00
parent c96b2a3f4c
commit 7fd44b7e8f

View File

@ -95,7 +95,7 @@ class WeboobProxy(object):
""" """
Build Weboob ``weboob.capabilities.housing.Query`` objects from the Build Weboob ``weboob.capabilities.housing.Query`` objects from the
constraints defined in the configuration. Each query has at most 3 constraints defined in the configuration. Each query has at most 3
postal codes, to comply with housing websites limitations. cities, to comply with housing websites limitations.
:param constraints_dict: A dictionary of constraints, as defined in the :param constraints_dict: A dictionary of constraints, as defined in the
config. config.
@ -103,24 +103,23 @@ class WeboobProxy(object):
objects. Returns ``None`` if an error occurred. objects. Returns ``None`` if an error occurred.
""" """
queries = [] queries = []
for postal_codes in tools.batch(constraints_dict["postal_codes"], 3):
query = Query() # First, find all matching cities for the postal codes in constraints
query.cities = [] matching_cities = []
for postal_code in postal_codes: for postal_code in constraints_dict["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): matching_cities.append(city)
matching_cities.append(city) except CallErrors as exc:
except CallErrors as exc: # If an error occured, just log it
# If an error occured, just log it LOGGER.error(
LOGGER.error( (
( "An error occured while building query for "
"An error occured while building query for " "postal code %s: %s"
"postal code %s: %s" ),
), postal_code,
postal_code, str(exc)
str(exc) )
)
if not matching_cities: if not matching_cities:
# If postal code gave no match, warn the user # If postal code gave no match, warn the user
@ -129,9 +128,10 @@ class WeboobProxy(object):
postal_code postal_code
) )
# Append the matched cities to the query # Then, build queries by grouping cities by at most 3
for city in matching_cities: for cities_batch in tools.batch(matching_cities, 3):
query.cities.append(city) query = Query()
query.cities = list(cities_batch)
try: try:
query.house_types = [ query.house_types = [