Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if isinstance(url, SplitResult):
parsed = url
else:
url = ensure_protocol(url)
parsed = urlsplit(url)
if not is_youtube_url(parsed):
return
_, _, path, query, fragment = parsed
# youtu.be
if parsed.hostname.endswith('youtu.be'):
if path.count('/') > 0:
v = urlpathsplit(path)[0]
if fix_common_mistakes:
v = v[:11]
if not is_youtube_video_id(v):
return
return YoutubeVideo(id=v)
return
# Hidden video in fragment
if fragment:
mv = FRAGMENT_V_RE.match(fragment)
if mv:
not url.startswith('https://') and
'facebook.' not in url
):
url = urljoin(BASE_FACEBOOK_URL, url)
else:
if not is_facebook_url(url):
return None
splitted = safe_urlsplit(url)
if not splitted.path or splitted.path == '/':
return None
# Obvious post path
if '/posts/' in splitted.path:
parts = urlpathsplit(splitted.path)
parent_id_or_handle = parts[0]
if NUMERIC_ID_RE.match(parent_id_or_handle):
return FacebookPost(parts[2], parent_id=parent_id_or_handle)
return FacebookPost(parts[2], parent_handle=parent_id_or_handle)
# Ye olded permalink path
if splitted.query and '/permalink.php' in splitted.path:
query = parse_qs(splitted.query)
return FacebookPost(query['story_fbid'][0], parent_id=query['id'][0])
# Group permalink path
if '/groups/' in splitted.path and '/permalink/' in splitted.path:
parts = urlpathsplit(splitted.path)
v = v[:11]
if not is_youtube_video_id(v):
return
return YoutubeVideo(id=v)
# Typical user url
elif path.startswith('/user/'):
user = urlpathsplit(path)[1]
return YoutubeUser(id=None, name=user)
# Channel path?
elif path.startswith('/c/'):
name = urlpathsplit(path)[1]
return YoutubeChannel(id=None, name=name)
elif path.startswith('/channel/'):
cid = urlpathsplit(path)[1]
return YoutubeChannel(id=cid, name=None)
else:
path = path.rstrip('/')
if path.count('/') == 1:
return YoutubeChannel(id=None, name=path.lstrip('/'))
return YoutubeVideo(id=v)
# Typical user url
elif path.startswith('/user/'):
user = urlpathsplit(path)[1]
return YoutubeUser(id=None, name=user)
# Channel path?
elif path.startswith('/c/'):
name = urlpathsplit(path)[1]
return YoutubeChannel(id=None, name=name)
elif path.startswith('/channel/'):
cid = urlpathsplit(path)[1]
return YoutubeChannel(id=cid, name=None)
else:
path = path.rstrip('/')
if path.count('/') == 1:
return YoutubeChannel(id=None, name=path.lstrip('/'))