diff --git a/flatisfy/config.py b/flatisfy/config.py index eb3bf91..aa97508 100644 --- a/flatisfy/config.py +++ b/flatisfy/config.py @@ -34,7 +34,7 @@ DEFAULT_CONFIG = { "cost": (None, None), # (min, max) in currency unit "rooms": (None, None), # (min, max) "bedrooms": (None, None), # (min, max) - "minimum_pictures": None, + "minimum_photos": None, "time_to": {} # Dict mapping names to {"gps": [lat, lng], # "time": (min, max) } # Time is in seconds @@ -138,10 +138,11 @@ def validate_config(config, check_with_data): assert "type" in constraint assert isinstance(constraint["type"], str) assert constraint["type"].upper() in ["RENT", "SALE", "SHARING"] - - assert "minimum_pictures" in constraint - assert isinstance(constraint["minimum_pictures"], int) - assert constraint["minimum_pictures"] >= 0 + + assert "minimum_photos" in constraint + if constraint["minimum_photos"]: + assert isinstance(constraint["minimum_photos"], int) + assert constraint["minimum_photos"] >= 0 assert "house_types" in constraint assert constraint["house_types"] diff --git a/flatisfy/filters/__init__.py b/flatisfy/filters/__init__.py index 34c064e..2bbf2f0 100644 --- a/flatisfy/filters/__init__.py +++ b/flatisfy/filters/__init__.py @@ -26,7 +26,6 @@ def refine_with_housing_criteria(flats_list, constraint): :param flats_list: A list of flats dict to filter. :param constraint: The constraint that the ``flats_list`` should satisfy. - :param config: A config dict. :return: A tuple of flats to keep and flats to delete. """ # For each flat, the associated `is_ok` value indicate whether it should be @@ -67,12 +66,51 @@ def refine_with_housing_criteria(flats_list, constraint): field.capitalize(), flat["id"]) is_ok[i] = is_ok[i] and is_within_interval - # Check number of pictures - print(constraint['minimum_pictures'], len(flat['photos'])) - if not (constraint['minimum_pictures'] <= len(flat['photos'])): - is_ok[i] = False + return ( + [ + flat + for i, flat in enumerate(flats_list) + if is_ok[i] + ], + [ + flat + for i, flat in enumerate(flats_list) + if not is_ok[i] + ] + ) +def refine_with_minimum_photos(flats_list, constraint): + """ + Filter a list of flats according to the minimum number of photos criterion. + + .. note :: This has to be done in a separate function and not with the + other criterias as photos are only fetched in the second pass. + + :param flats_list: A list of flats dict to filter. + :param constraint: The constraint that the ``flats_list`` should satisfy. + :return: A tuple of flats to keep and flats to delete. + """ + # For each flat, the associated `is_ok` value indicate whether it should be + # kept or discarded. + is_ok = [True for _ in flats_list] + + for i, flat in enumerate(flats_list): + # Check number of pictures + has_enough_photos = tools.is_within_interval( + flat.get('photos', []), + constraint['minimum_photos'], + None + ) + if not has_enough_photos: + LOGGER.info( + "Flat %s only has %d photos, it should have at least %d.", + flat["id"], + len(flat['photos']), + constraint['minimum_photos'] + ) + is_ok[i] = False + return ( [ flat @@ -86,6 +124,7 @@ def refine_with_housing_criteria(flats_list, constraint): ] ) + @tools.timeit def first_pass(flats_list, constraint, config): """ @@ -163,6 +202,10 @@ def second_pass(flats_list, constraint, config): flats_list, ignored_list = refine_with_housing_criteria(flats_list, constraint) + # Remove return housing posts which do not have enough photos + flats_list, ignored_list = refine_with_minimum_photos(flats_list, + constraint) + return { "new": flats_list, "ignored": ignored_list,