Skip to content

bolig_ping.filtering

docs module bolig_ping.filtering

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Filtering of scraped results."""

from tqdm.auto import tqdm

from .data_models import Home, SearchQuery


def filter_results(homes: list[Home], search_query: SearchQuery) -> list[Home]:
    """Filter the homes based on the given criteria.

    Args:
        homes:
            The homes to filter.
        search_query:
            The search query to filter the homes by.

    Returns:
        The filtered homes.
    """
    # Filter the homes based on the monthly fee
    homes = [
        home
        for home in homes
        if home.monthly_fee is None
        or (
            (
                search_query.min_monthly_fee is None
                or home.monthly_fee >= search_query.min_monthly_fee
            )
            and (
                search_query.max_monthly_fee is None
                or home.monthly_fee <= search_query.max_monthly_fee
            )
        )
    ]

    # Filter the homes if any keyword queries were given
    if search_query.queries:
        homes = [
            home
            for home in tqdm(iterable=homes, desc="Filtering homes based on keywords")
            if home.description is not None
            and any(
                query.lower() in home.description.lower()
                for query in search_query.queries
            )
        ]

    return homes