Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_youtube_url(url, fix_common_mistakes=True):
"""
Function parsing the given url and returning either a YoutubeUser,
YoutubeChannel, YoutubeVideo or None if nothing of information could be
found.
Args:
url (str): Url to parse.
fix_common_mistakes (bool, optional): Whether to fix common mistakes
in Youtube urls as you can find them on the web. Defaults to `True`.
"""
# Inferring redirection
url = infer_redirection(url)
# Continuation urls
m = NEXT_V_RE.search(url) or NESTED_NEXT_V_RE.search(url)
if m:
return YoutubeVideo(id=m.group(1))
# Parsing
if isinstance(url, SplitResult):
parsed = url
else:
url = ensure_protocol(url)
parsed = urlsplit(url)
if not is_youtube_url(parsed):
return
def get_normalized_hostname(url, normalize_amp=True, strip_lang_subdomains=False,
infer_redirection=True):
if infer_redirection:
url = resolve(url)
if isinstance(url, SplitResult):
splitted = url
else:
try:
splitted = urlsplit(ensure_protocol(url))
except ValueError:
return None
if not splitted.hostname:
return None
hostname = splitted.hostname.lower()
pattern = IRRELEVANT_SUBDOMAIN_AMP_RE if normalize_amp else IRRELEVANT_SUBDOMAIN_RE
AMP urls. Defaults to True.
fix_common_mistakes (bool, optional): Whether to attempt solving common mistakes.
Defaults to True.
infer_redirection (bool, optional): Whether to attempt resolving common
redirects by leveraging well-known GET parameters. Defaults to `False`.
quoted (bool, optional): Normalizing to quoted or unquoted.
Defaults to True.
Returns:
string: The normalized url.
"""
original_url_arg = url
if infer_redirection:
url = resolve(url)
if isinstance(url, SplitResult):
has_protocol = bool(splitted.scheme)
splitted = url
else:
has_protocol = PROTOCOL_RE.match(url)
# Ensuring scheme so parsing works correctly
if not has_protocol:
url = 'http://' + url
# Parsing
try:
splitted = urlsplit(url)
except ValueError:
return original_url_arg