Repository: benbusby/whoogle-search
File: app/filter.py (Python)

Code before:
    return parse_qs(q_str)['q'][0] if ('&q=' in href or '?q=' in href) else ''


def clean_query(query: str) -> str:
    """Strips the blocked site list from the query, if one is being
    used.

    Args:
        query: The query string

    Returns:
        str: The query string without any "-site:..." filters
    """
    return query[:query.find('-site:')] if '-site:' in query else query


class Filter:
    # Limit used for determining if a result is a "regular" result or a list
    # type result (such as "people also asked", "related searches", etc)
    RESULT_CHILD_LIMIT = 7

    def __init__(self, user_key: str, mobile=False, config=None) -> None:
        if config is None:
            config = {}
        else:
            self.config = config
        self.near = config['near'] if 'near' in config else ''
        self.dark = config['dark'] if 'dark' in config else False
        self.nojs = config['nojs'] if 'nojs' in config else False
        self.new_tab = config['new_tab'] if 'new_tab' in config else False
        self.alt_redirect = config['alts'] if 'alts' in config else False
        self.mobile = mobile
        self.user_key = user_key
        self.main_divs = ResultSet('')
        self._elements = 0

    def __getitem__(self, name):
        return getattr(self, name)

    @property
    def elements(self):
        return self._elements

    def reskin(self, page: str) -> str:
        # Aesthetic only re-skinning
        if self.dark:
            page = page.replace(
                'fff', '000').replace(
                '202124', 'ddd').replace(
                '1967D2', '3b85ea')


Review comment (suggestion):
`self.config` needs to be declared for in all scenarios (including if `config is None`), so declaring it separately here causes problems. Might be better to move to the end of `__init__` so that we don't also have to declare `self.config` for every item during init. For instance:

```python
        if config is None:
            config = {}
        self.near = config['near'] if 'near' in config else ''
        # ...
        # <rest of __init__>
        # ...
        self.config = config
```

Code after:
    return parse_qs(q_str)['q'][0] if ('&q=' in href or '?q=' in href) else ''


def clean_query(query: str) -> str:
    """Strips the blocked site list from the query, if one is being
    used.

    Args:
        query: The query string

    Returns:
        str: The query string without any "-site:..." filters
    """
    return query[:query.find('-site:')] if '-site:' in query else query


class Filter:
    # Limit used for determining if a result is a "regular" result or a list
    # type result (such as "people also asked", "related searches", etc)
    RESULT_CHILD_LIMIT = 7

    def __init__(self, user_key: str, mobile=False, config=None) -> None:
        if config is None:
            config = {}
        self.near = config['near'] if 'near' in config else ''
        self.dark = config['dark'] if 'dark' in config else False
        self.nojs = config['nojs'] if 'nojs' in config else False
        self.new_tab = config['new_tab'] if 'new_tab' in config else False
        self.alt_redirect = config['alts'] if 'alts' in config else False
        self.block_title = (
            config['block_title'] if 'block_title' in config else '')
        self.block_url = (
            config['block_url'] if 'block_url' in config else '')
        self.mobile = mobile
        self.user_key = user_key
        self.main_divs = ResultSet('')
        self._elements = 0

    def __getitem__(self, name):
        return getattr(self, name)

    @property
    def elements(self):
        return self._elements

    def reskin(self, page: str) -> str:
        # Aesthetic only re-skinning
        if self.dark:
            page = page.replace(
                'fff', '000').replace(
                '202124', 'ddd').replace(