[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:
Philipp Rintz 2022-07-18 20:48:23 +02:00 committed by GitHub
parent c1aa973120
commit 12a857bf9a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 2 deletions

View file

@ -114,6 +114,7 @@ class YoutubeChannelUrlValidator(StringValidator):
- https://www.youtube.com/channel/UCuAXFkgsw1L7xaCfnd5JJOw
- https://www.youtube.com/user/username
- https://www.youtube.com/c/channel_name
- https://youtube.com/channel_name
"""
# If the url doesn't contain youtube, assume it is invalid
if "youtube.com" not in url:
@ -125,12 +126,16 @@ class YoutubeChannelUrlValidator(StringValidator):
query = urlparse(url)
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/")):
# 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:
return None
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

View file

@ -105,6 +105,10 @@ class TestYoutubeChannelUrlValidator:
"https://www.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):
@ -117,7 +121,6 @@ class TestYoutubeChannelUrlValidator:
"www.youtube.com/cha/UCuAXFkgsw1L7xaCfnd5JJOw",
"www.nopetube.com/channel/asdfdsf",
"www.youtube.com/channel/",
"www.youtube.com/channel",
"www.youtube.com/channell/asdfasdf",
],
)