add support for m.youtube.com as a netloc when validating source urls, resolves #264

This commit is contained in:
meeb 2022-09-04 11:57:15 +10:00
parent 8426c7309a
commit 05d50c958e
3 changed files with 17 additions and 7 deletions

View File

@ -36,6 +36,9 @@ class FrontEndTestCase(TestCase):
test_sources = { test_sources = {
'youtube-channel': { 'youtube-channel': {
'valid': ( 'valid': (
'https://m.youtube.com/testchannel',
'https://m.youtube.com/c/testchannel',
'https://m.youtube.com/c/testchannel/videos',
'https://www.youtube.com/testchannel', 'https://www.youtube.com/testchannel',
'https://www.youtube.com/c/testchannel', 'https://www.youtube.com/c/testchannel',
'https://www.youtube.com/c/testchannel/videos', 'https://www.youtube.com/c/testchannel/videos',
@ -47,6 +50,7 @@ class FrontEndTestCase(TestCase):
'invalid_domain': ( 'invalid_domain': (
'https://www.test.com/c/testchannel', 'https://www.test.com/c/testchannel',
'https://www.example.com/c/testchannel', 'https://www.example.com/c/testchannel',
'https://n.youtube.com/c/testchannel',
), ),
'invalid_path': ( 'invalid_path': (
'https://www.youtube.com/test/invalid', 'https://www.youtube.com/test/invalid',
@ -62,6 +66,8 @@ class FrontEndTestCase(TestCase):
}, },
'youtube-channel-id': { 'youtube-channel-id': {
'valid': ( 'valid': (
'https://m.youtube.com/channel/channelid',
'https://m.youtube.com/channel/channelid/videos',
'https://www.youtube.com/channel/channelid', 'https://www.youtube.com/channel/channelid',
'https://www.youtube.com/channel/channelid/videos', 'https://www.youtube.com/channel/channelid/videos',
), ),
@ -72,6 +78,7 @@ class FrontEndTestCase(TestCase):
'invalid_domain': ( 'invalid_domain': (
'https://www.test.com/channel/channelid', 'https://www.test.com/channel/channelid',
'https://www.example.com/channel/channelid', 'https://www.example.com/channel/channelid',
'https://n.youtube.com/channel/channelid',
), ),
'invalid_path': ( 'invalid_path': (
'https://www.youtube.com/test/invalid', 'https://www.youtube.com/test/invalid',
@ -83,6 +90,8 @@ class FrontEndTestCase(TestCase):
}, },
'youtube-playlist': { 'youtube-playlist': {
'valid': ( 'valid': (
'https://m.youtube.com/playlist?list=testplaylist',
'https://m.youtube.com/watch?v=testvideo&list=testplaylist',
'https://www.youtube.com/playlist?list=testplaylist', 'https://www.youtube.com/playlist?list=testplaylist',
'https://www.youtube.com/watch?v=testvideo&list=testplaylist', 'https://www.youtube.com/watch?v=testvideo&list=testplaylist',
), ),
@ -93,6 +102,7 @@ class FrontEndTestCase(TestCase):
'invalid_domain': ( 'invalid_domain': (
'https://www.test.com/playlist?list=testplaylist', 'https://www.test.com/playlist?list=testplaylist',
'https://www.example.com/playlist?list=testplaylist', 'https://www.example.com/playlist?list=testplaylist',
'https://n.youtube.com/playlist?list=testplaylist',
), ),
'invalid_path': ( 'invalid_path': (
'https://www.youtube.com/notplaylist?list=testplaylist', 'https://www.youtube.com/notplaylist?list=testplaylist',

View File

@ -14,9 +14,9 @@ def validate_url(url, validator):
Validate a URL against a dict of validation requirements. Returns an extracted Validate a URL against a dict of validation requirements. Returns an extracted
part of the URL if the URL is valid, if invalid raises a ValidationError. part of the URL if the URL is valid, if invalid raises a ValidationError.
''' '''
valid_scheme, valid_netloc, valid_path, invalid_paths, valid_query, \ valid_scheme, valid_netlocs, valid_path, invalid_paths, valid_query, \
extract_parts = ( extract_parts = (
validator['scheme'], validator['domain'], validator['path_regex'], validator['scheme'], validator['domains'], validator['path_regex'],
validator['path_must_not_match'], validator['qs_args'], validator['path_must_not_match'], validator['qs_args'],
validator['extract_key'] validator['extract_key']
) )
@ -25,8 +25,8 @@ def validate_url(url, validator):
if url_scheme != valid_scheme: if url_scheme != valid_scheme:
raise ValidationError(f'invalid scheme "{url_scheme}" must be "{valid_scheme}"') raise ValidationError(f'invalid scheme "{url_scheme}" must be "{valid_scheme}"')
url_netloc = str(url_parts.netloc).strip().lower() url_netloc = str(url_parts.netloc).strip().lower()
if url_netloc != valid_netloc: if url_netloc not in valid_netlocs:
raise ValidationError(f'invalid domain "{url_netloc}" must be "{valid_netloc}"') raise ValidationError(f'invalid domain "{url_netloc}" must be one of "{valid_netlocs}"')
url_path = str(url_parts.path).strip() url_path = str(url_parts.path).strip()
matches = re.findall(valid_path, url_path) matches = re.findall(valid_path, url_path)
if not matches: if not matches:

View File

@ -171,7 +171,7 @@ class ValidateSourceView(FormView):
validation_urls = { validation_urls = {
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: { Source.SOURCE_TYPE_YOUTUBE_CHANNEL: {
'scheme': 'https', 'scheme': 'https',
'domain': 'www.youtube.com', 'domains': ('m.youtube.com', 'www.youtube.com'),
'path_regex': '^\/(c\/)?([^\/]+)(\/videos)?$', 'path_regex': '^\/(c\/)?([^\/]+)(\/videos)?$',
'path_must_not_match': ('/playlist', '/c/playlist'), 'path_must_not_match': ('/playlist', '/c/playlist'),
'qs_args': [], 'qs_args': [],
@ -180,7 +180,7 @@ class ValidateSourceView(FormView):
}, },
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: { Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: {
'scheme': 'https', 'scheme': 'https',
'domain': 'www.youtube.com', 'domains': ('m.youtube.com', 'www.youtube.com'),
'path_regex': '^\/channel\/([^\/]+)(\/videos)?$', 'path_regex': '^\/channel\/([^\/]+)(\/videos)?$',
'path_must_not_match': ('/playlist', '/c/playlist'), 'path_must_not_match': ('/playlist', '/c/playlist'),
'qs_args': [], 'qs_args': [],
@ -189,7 +189,7 @@ class ValidateSourceView(FormView):
}, },
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: { Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: {
'scheme': 'https', 'scheme': 'https',
'domain': 'www.youtube.com', 'domains': ('m.youtube.com', 'www.youtube.com'),
'path_regex': '^\/(playlist|watch)$', 'path_regex': '^\/(playlist|watch)$',
'path_must_not_match': (), 'path_must_not_match': (),
'qs_args': ('list',), 'qs_args': ('list',),