Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for receiving HTML input and bug fix #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -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)
------------------

Expand Down
2 changes: 1 addition & 1 deletion src/favicon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
21 changes: 15 additions & 6 deletions src/favicon/favicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand All @@ -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)

12 changes: 12 additions & 0 deletions tests/test_favicon.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '''<!DOCTYPE html><!--[if lt IE 7]><html class="no-js lt-ie9 lt-ie8 lt-ie7" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]--><!--[if IE 7]><html class="no-js lt-ie9 lt-ie8" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]--><!--[if IE 8]><html class="no-js lt-ie9" lang="en-US" prefix="og: http://ogp.me/ns#"> <![endif]--><!--[if gt IE 8]><!--><html class="no-js" lang="en-US" prefix="og: http://ogp.me/ns#"> <!--<![endif]--><head> <meta charset="utf-8"> <title>Home - MOCK.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="shortcut icon" type="image/x-icon" href="http://mock.com/wp-content/uploads/2014/03/favicon.ico"/> <link rel="canonical" href="http://mock.com/"/> <meta property="og:locale" content="en_US"/> <meta property="og:type" content="website"/> <meta property="og:title" content="Home - MOCK.com"/> <meta property="og:url" content="http://mock.com/"/> <meta property="og:site_name" content="MOCK.com"/></head><body>Test</body></html>''' # noqa

icons = favicon.get(
'http://mock.com/',
html_override=mock_com_html,
)
assert icons
assert icons[0].url == 'http://mock.com/favicon.ico'
assert icons[1].url == 'http://mock.com/wp-content/uploads/2014/03/favicon.ico'