diff --git a/CHANGELOG.rst b/CHANGELOG.rst index c827931..d04a92b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,12 @@ Changelog ========= +0.7.1 (2020-06-17) +------------------ + +* Added support for receiving HTML input +* Fixed issue with dimensions(tag) not working for some websites + 0.7.0 (2019-08-31) ------------------ diff --git a/src/favicon/__init__.py b/src/favicon/__init__.py index fe777f5..e78c4a5 100644 --- a/src/favicon/__init__.py +++ b/src/favicon/__init__.py @@ -8,7 +8,7 @@ __all__ = ["get", "Icon"] __title__ = "favicon" -__version__ = "0.7.0" +__version__ = "0.7.1" __author__ = "Scott Werner" __license__ = "MIT" __copyright__ = "Copyright 2019 Scott Werner" diff --git a/src/favicon/favicon.py b/src/favicon/favicon.py index 5730f91..ced8c34 100644 --- a/src/favicon/favicon.py +++ b/src/favicon/favicon.py @@ -40,12 +40,15 @@ Icon = namedtuple('Icon', ['url', 'width', 'height', 'format']) -def get(url, *args, **request_kwargs): +def get(url, *args, html_override=None, **request_kwargs): """Get all fav icons for a url. :param url: Homepage. :type url: str + :param html_override: HTML input, as string. Will be used instead of an HTTP response from the `url`. + :type html_override: str or None + :param request_kwargs: Request headers argument. :type request_kwargs: Dict @@ -62,16 +65,21 @@ def get(url, *args, **request_kwargs): request_kwargs.setdefault('headers', HEADERS) request_kwargs.setdefault('allow_redirects', True) - response = requests.get(url, **request_kwargs) - response.raise_for_status() + if html_override is None: + response = requests.get(url, **request_kwargs) + response.raise_for_status() + final_url = response.url + html_override = response.text + else: + final_url = url icons = set() - default_icon = default(response.url, **request_kwargs) + default_icon = default(final_url, **request_kwargs) if default_icon: icons.add(default_icon) - link_icons = tags(response.url, response.text) + link_icons = tags(final_url, html_override) if link_icons: icons.update(link_icons) @@ -182,7 +190,7 @@ def dimensions(tag): if sizes and sizes != 'any': size = sizes.split(' ') # '16x16 32x32 64x64' size.sort(reverse=True) - width, height = re.split(r'[x\xd7]', size[0]) + width, height = re.split(r'[x\xd7/]', size[0]) else: filename = tag.get('href') or tag.get('content') size = SIZE_RE.search(filename) @@ -195,3 +203,4 @@ def dimensions(tag): width = ''.join(c for c in width if c.isdigit()) height = ''.join(c for c in height if c.isdigit()) return int(width), int(height) + diff --git a/tests/test_favicon.py b/tests/test_favicon.py index bc91e4e..8b32cc1 100644 --- a/tests/test_favicon.py +++ b/tests/test_favicon.py @@ -201,3 +201,15 @@ def test_request_kwargs(m): ) def test_is_absolute_helper(url, expected): assert is_absolute(url) == expected + +def test_html_input(): + # contents of mock.com + mock_com_html = '''