PKL=VVurl_checker/__init__.py""" format_url: Take URL which may or may-not be formatted and return a formatted one. :param url: URL to format as str. :param force_https: Whether to force return HTTPS URL as bool :return: Formatted URL as str. EXAMPLE: >>> from url_checker.engine import format_url >>> format_url('http://yahoo.com') 'http://www.yahoo.com' >>> format_url('yahoo.com') 'http://www.yahoo.com' >>> format_url('www.yahoo.com') 'http://www.yahoo.com' >>> format_url('http://www.yahoo.com', force_https=True) 'https://www.yahoo.com' get_status_code: Will make a get request to the provided url and return the response code. :param url: The URL to test as str. :param should_format: Whether to format the URL before the GET request as bool. :return: Status code of returned response as int EXAMPLE: >>> from url_checker.engine import get_status_code >>> get_status_code('https://www.yahoo.com/sports') 200 >>> get_status_code('http://arstechnica.com/not_real') 404 >>> get_status_code('google.com') # Will fail without protocol provided 0 >>> get_status_code('google.com', should_format=True) 301 """ __version__ = "1.3.0" PKxL[0wurl_checker/engine.pyimport logbook import requests from requests.exceptions import ConnectionError, InvalidURL, MissingSchema def get_status_code(url: str, should_format: bool = False) -> int: """ Will make a get request to the provided url and return the response code. :param url: The URL to test as str. :param should_format: Whether to format the URL before the GET request as bool. :return: Status code of returned response as int EXAMPLE: >>> from url_checker.engine import get_status_code >>> get_status_code('https://www.yahoo.com/sports') 200 >>> get_status_code('http://arstechnica.com/not_real') 404 >>> get_status_code('google.com') # Will fail without protocol provided 0 >>> get_status_code('google.com', should_format=True) 301 """ try: if should_format: url = format_url(url) r = requests.get(url) if r.history: status_code = r.history[0].status_code else: status_code = r.status_code logbook.debug(f"{status_code} @ '{url}'") return status_code except (InvalidURL, MissingSchema): logbook.error(f"Invalid URL @ '{url}'") return 0 except ConnectionError: logbook.error(f"Failed to establish a connection @ '{url}'") return 0 def format_url(url: str = None, force_https: bool = False) -> str: """ Take URL which may or may-not be formatted and return a formatted one. :param url: URL to format as str. :param force_https: Whether to force return HTTPS URL as bool :return: Formatted URL as str. EXAMPLE: >>> from url_checker.engine import format_url >>> format_url('http://yahoo.com') 'http://www.yahoo.com' >>> format_url('yahoo.com') 'http://www.yahoo.com' >>> format_url('www.yahoo.com') 'http://www.yahoo.com' >>> format_url('http://www.yahoo.com', force_https=True) 'https://www.yahoo.com' """ if url.startswith('http://') or url.startswith('https://'): formatted = url else: formatted = 'http://' + url if force_https: formatted = formatted.replace('http://', 'https://') return formatted PKixL%DDurl_checker/test_module.pyfrom functools import partial import pytest from url_checker.engine import get_status_code, format_url def test_format_url_returns_with_schema(): assert format_url('http://google.com').startswith('http://') assert format_url('https://google.com').startswith('https://') assert format_url('www.google.com').startswith('http://') assert format_url('google.com').startswith('http://') def test_format_url_force_https_schema(): https_format_url = partial(format_url, force_https=True) assert https_format_url('http://google.com').startswith('https://') assert https_format_url('https://google.com').startswith('https://') assert https_format_url('www.google.com').startswith('https://') assert https_format_url('google.com').startswith('https://') def test_get_status_code_invalid_urls_return_0(): assert get_status_code('https://www.nothing.rm') == 0 assert get_status_code('nothing.rm') == 0 assert get_status_code('www.google.com') == 0 assert get_status_code('google.com') == 0 def test_get_status_code_with_format_adds_schema(): get_status_code_with_format = partial(get_status_code, should_format=True) assert get_status_code_with_format('www.google.com') != 0 assert get_status_code_with_format('google.com') != 0 def test_get_status_code_returns_initial_status_code(): assert get_status_code('https://httpbin.org/status/200') == 200 assert get_status_code('https://httpbin.org/status/302') == 302 assert get_status_code('https://httpbin.org/status/404') == 404 if __name__ == '__main__': pytest.main('-v') PKJa>&55#url_checker-1.3.0.dist-info/LICENSEThe MIT License (MIT) Copyright (c) 2017 cw-andrews Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!Hp!Qa!url_checker-1.3.0.dist-info/WHEEL HM K-*ϳR03rOK-J,/RH,zd&Y)r$[)T&UD"PK!HU$url_checker-1.3.0.dist-info/METADATAmN0E @&},Q>$cY4jv({ƨ)s\{^‹f$iiGN阎m#c;hQbk}X+I~E]X8x)sכu4o}e,#s)- \/C-GJ8'lJ#̛ƚ/(yx>[iŏyFq ؃9Xgl-s 5X#]ˢFсҔ{c }XzfkQmYvк@w>o= %'re2f7gJCDBmx$E vޮC6揹wiI3~xtr2/}Pq6^Fƒf}4UrȻCGu{T :;WDSw][zڍǾ0ٌȱ)D\')*JT O;q,{ۆC@2[Sve~UjNz>4 ,v}+"&ȓ68)7VCg]|a%qet@Y5 M޾PKL=VVurl_checker/__init__.pyPKxL[0wurl_checker/engine.pyPKixL%DD url_checker/test_module.pyPKJa>&55#url_checker-1.3.0.dist-info/LICENSEPK!Hp!Qa!url_checker-1.3.0.dist-info/WHEELPK!HU$!url_checker-1.3.0.dist-info/METADATAPK!HeXb^&"url_checker-1.3.0.dist-info/RECORDPKV