[BUGFIX] Fix youtube url validator for channels without 'c', 'channel' or 'user' (#109)
* Fix youtube url validator for channels without 'c', 'channel' or 'user' * Fix linter errors
This commit is contained in:
parent
c1aa973120
commit
12a857bf9a
2 changed files with 10 additions and 2 deletions
|
|
@ -114,6 +114,7 @@ class YoutubeChannelUrlValidator(StringValidator):
|
||||||
- https://www.youtube.com/channel/UCuAXFkgsw1L7xaCfnd5JJOw
|
- https://www.youtube.com/channel/UCuAXFkgsw1L7xaCfnd5JJOw
|
||||||
- https://www.youtube.com/user/username
|
- https://www.youtube.com/user/username
|
||||||
- https://www.youtube.com/c/channel_name
|
- https://www.youtube.com/c/channel_name
|
||||||
|
- https://youtube.com/channel_name
|
||||||
"""
|
"""
|
||||||
# If the url doesn't contain youtube, assume it is invalid
|
# If the url doesn't contain youtube, assume it is invalid
|
||||||
if "youtube.com" not in url:
|
if "youtube.com" not in url:
|
||||||
|
|
@ -125,12 +126,16 @@ class YoutubeChannelUrlValidator(StringValidator):
|
||||||
|
|
||||||
query = urlparse(url)
|
query = urlparse(url)
|
||||||
if query.hostname in ("youtube.com", "www.youtube.com"):
|
if query.hostname in ("youtube.com", "www.youtube.com"):
|
||||||
|
query_path_split = query.path.split("/")
|
||||||
if any(query.path.startswith(qpath) for qpath in ("/channel/", "/user/", "/c/")):
|
if any(query.path.startswith(qpath) for qpath in ("/channel/", "/user/", "/c/")):
|
||||||
# Ensure there is "/path/id".split('/') = ['', 'path', 'id'] at least three
|
# Ensure there is "/path/id".split('/') = ['', 'path', 'id'] at least three
|
||||||
query_path_split = query.path.split("/")
|
|
||||||
if len(query_path_split) < 3 or len(query_path_split[2]) == 0:
|
if len(query_path_split) < 3 or len(query_path_split[2]) == 0:
|
||||||
return None
|
return None
|
||||||
return f"https://youtube.com{query.path}"
|
return f"https://youtube.com{query.path}"
|
||||||
|
if len(query_path_split) == 2 and len(query_path_split[1]) > 0:
|
||||||
|
# For channels that do not feature a 'c' or 'channel'
|
||||||
|
# Ensure length of channel string is at least one
|
||||||
|
return f"https://youtube.com{query.path}"
|
||||||
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -105,6 +105,10 @@ class TestYoutubeChannelUrlValidator:
|
||||||
"https://www.youtube.com/user/videogamedunkey",
|
"https://www.youtube.com/user/videogamedunkey",
|
||||||
"https://youtube.com/user/videogamedunkey",
|
"https://youtube.com/user/videogamedunkey",
|
||||||
),
|
),
|
||||||
|
(
|
||||||
|
"https://youtube.com/extracredits",
|
||||||
|
"https://youtube.com/extracredits",
|
||||||
|
),
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
def test_youtube_channel_url_validator_success(self, url, expected_url):
|
def test_youtube_channel_url_validator_success(self, url, expected_url):
|
||||||
|
|
@ -117,7 +121,6 @@ class TestYoutubeChannelUrlValidator:
|
||||||
"www.youtube.com/cha/UCuAXFkgsw1L7xaCfnd5JJOw",
|
"www.youtube.com/cha/UCuAXFkgsw1L7xaCfnd5JJOw",
|
||||||
"www.nopetube.com/channel/asdfdsf",
|
"www.nopetube.com/channel/asdfdsf",
|
||||||
"www.youtube.com/channel/",
|
"www.youtube.com/channel/",
|
||||||
"www.youtube.com/channel",
|
|
||||||
"www.youtube.com/channell/asdfasdf",
|
"www.youtube.com/channell/asdfasdf",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue