From 05d50c958ee2e099ee7829826a07ded409b04639 Mon Sep 17 00:00:00 2001 From: meeb Date: Sun, 4 Sep 2022 11:57:15 +1000 Subject: [PATCH] add support for m.youtube.com as a netloc when validating source urls, resolves #264 --- tubesync/sync/tests.py | 10 ++++++++++ tubesync/sync/utils.py | 8 ++++---- tubesync/sync/views.py | 6 +++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/tubesync/sync/tests.py b/tubesync/sync/tests.py index 4963c3c..25fecc0 100644 --- a/tubesync/sync/tests.py +++ b/tubesync/sync/tests.py @@ -36,6 +36,9 @@ class FrontEndTestCase(TestCase): test_sources = { 'youtube-channel': { '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/c/testchannel', 'https://www.youtube.com/c/testchannel/videos', @@ -47,6 +50,7 @@ class FrontEndTestCase(TestCase): 'invalid_domain': ( 'https://www.test.com/c/testchannel', 'https://www.example.com/c/testchannel', + 'https://n.youtube.com/c/testchannel', ), 'invalid_path': ( 'https://www.youtube.com/test/invalid', @@ -62,6 +66,8 @@ class FrontEndTestCase(TestCase): }, 'youtube-channel-id': { '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/videos', ), @@ -72,6 +78,7 @@ class FrontEndTestCase(TestCase): 'invalid_domain': ( 'https://www.test.com/channel/channelid', 'https://www.example.com/channel/channelid', + 'https://n.youtube.com/channel/channelid', ), 'invalid_path': ( 'https://www.youtube.com/test/invalid', @@ -83,6 +90,8 @@ class FrontEndTestCase(TestCase): }, 'youtube-playlist': { '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/watch?v=testvideo&list=testplaylist', ), @@ -93,6 +102,7 @@ class FrontEndTestCase(TestCase): 'invalid_domain': ( 'https://www.test.com/playlist?list=testplaylist', 'https://www.example.com/playlist?list=testplaylist', + 'https://n.youtube.com/playlist?list=testplaylist', ), 'invalid_path': ( 'https://www.youtube.com/notplaylist?list=testplaylist', diff --git a/tubesync/sync/utils.py b/tubesync/sync/utils.py index e20c418..dcf1e2f 100644 --- a/tubesync/sync/utils.py +++ b/tubesync/sync/utils.py @@ -14,9 +14,9 @@ def validate_url(url, validator): 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. ''' - valid_scheme, valid_netloc, valid_path, invalid_paths, valid_query, \ + valid_scheme, valid_netlocs, valid_path, invalid_paths, valid_query, \ 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['extract_key'] ) @@ -25,8 +25,8 @@ def validate_url(url, validator): if url_scheme != valid_scheme: raise ValidationError(f'invalid scheme "{url_scheme}" must be "{valid_scheme}"') url_netloc = str(url_parts.netloc).strip().lower() - if url_netloc != valid_netloc: - raise ValidationError(f'invalid domain "{url_netloc}" must be "{valid_netloc}"') + if url_netloc not in valid_netlocs: + raise ValidationError(f'invalid domain "{url_netloc}" must be one of "{valid_netlocs}"') url_path = str(url_parts.path).strip() matches = re.findall(valid_path, url_path) if not matches: diff --git a/tubesync/sync/views.py b/tubesync/sync/views.py index f3e6927..ce09fe1 100644 --- a/tubesync/sync/views.py +++ b/tubesync/sync/views.py @@ -171,7 +171,7 @@ class ValidateSourceView(FormView): validation_urls = { Source.SOURCE_TYPE_YOUTUBE_CHANNEL: { 'scheme': 'https', - 'domain': 'www.youtube.com', + 'domains': ('m.youtube.com', 'www.youtube.com'), 'path_regex': '^\/(c\/)?([^\/]+)(\/videos)?$', 'path_must_not_match': ('/playlist', '/c/playlist'), 'qs_args': [], @@ -180,7 +180,7 @@ class ValidateSourceView(FormView): }, Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: { 'scheme': 'https', - 'domain': 'www.youtube.com', + 'domains': ('m.youtube.com', 'www.youtube.com'), 'path_regex': '^\/channel\/([^\/]+)(\/videos)?$', 'path_must_not_match': ('/playlist', '/c/playlist'), 'qs_args': [], @@ -189,7 +189,7 @@ class ValidateSourceView(FormView): }, Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: { 'scheme': 'https', - 'domain': 'www.youtube.com', + 'domains': ('m.youtube.com', 'www.youtube.com'), 'path_regex': '^\/(playlist|watch)$', 'path_must_not_match': (), 'qs_args': ('list',),