add full support for YouTube channels with no vanity name, resolves #9
This commit is contained in:
parent
55578f4de7
commit
68a62d8a7c
|
@ -28,10 +28,13 @@ class Source(models.Model):
|
|||
'''
|
||||
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL = 'c'
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL_ID = 'i'
|
||||
SOURCE_TYPE_YOUTUBE_PLAYLIST = 'p'
|
||||
SOURCE_TYPES = (SOURCE_TYPE_YOUTUBE_CHANNEL, SOURCE_TYPE_YOUTUBE_PLAYLIST)
|
||||
SOURCE_TYPES = (SOURCE_TYPE_YOUTUBE_CHANNEL, SOURCE_TYPE_YOUTUBE_CHANNEL_ID,
|
||||
SOURCE_TYPE_YOUTUBE_PLAYLIST)
|
||||
SOURCE_TYPE_CHOICES = (
|
||||
(SOURCE_TYPE_YOUTUBE_CHANNEL, _('YouTube channel')),
|
||||
(SOURCE_TYPE_YOUTUBE_CHANNEL_ID, _('YouTube channel by ID')),
|
||||
(SOURCE_TYPE_YOUTUBE_PLAYLIST, _('YouTube playlist')),
|
||||
)
|
||||
|
||||
|
@ -98,21 +101,25 @@ class Source(models.Model):
|
|||
# Fontawesome icons used for the source on the front end
|
||||
ICONS = {
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL: '<i class="fab fa-youtube"></i>',
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL_ID: '<i class="fab fa-youtube"></i>',
|
||||
SOURCE_TYPE_YOUTUBE_PLAYLIST: '<i class="fab fa-youtube"></i>',
|
||||
}
|
||||
# Format to use to display a URL for the source
|
||||
URLS = {
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL: 'https://www.youtube.com/c/{key}',
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'https://www.youtube.com/channel/{key}',
|
||||
SOURCE_TYPE_YOUTUBE_PLAYLIST: 'https://www.youtube.com/playlist?list={key}',
|
||||
}
|
||||
# Callback functions to get a list of media from the source
|
||||
INDEXERS = {
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL: get_youtube_media_info,
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL_ID: get_youtube_media_info,
|
||||
SOURCE_TYPE_YOUTUBE_PLAYLIST: get_youtube_media_info,
|
||||
}
|
||||
# Field names to find the media ID used as the key when storing media
|
||||
KEY_FIELD = {
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL: 'id',
|
||||
SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'id',
|
||||
SOURCE_TYPE_YOUTUBE_PLAYLIST: 'id',
|
||||
}
|
||||
|
||||
|
@ -433,32 +440,39 @@ class Media(models.Model):
|
|||
# Format to use to display a URL for the media
|
||||
URLS = {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'https://www.youtube.com/watch?v={key}',
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'https://www.youtube.com/watch?v={key}',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'https://www.youtube.com/watch?v={key}',
|
||||
}
|
||||
# Maps standardised names to names used in source metdata
|
||||
METADATA_FIELDS = {
|
||||
'upload_date': {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'upload_date',
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'upload_date',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'upload_date',
|
||||
},
|
||||
'title': {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'title',
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'title',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'title',
|
||||
},
|
||||
'thumbnail': {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'thumbnail',
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'thumbnail',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'thumbnail',
|
||||
},
|
||||
'description': {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'description',
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'description',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'description',
|
||||
},
|
||||
'duration': {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'duration',
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'duration',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'duration',
|
||||
},
|
||||
'formats': {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'formats',
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: 'formats',
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: 'formats',
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,10 +10,13 @@
|
|||
</div>
|
||||
{% include 'infobox.html' with message=message %}
|
||||
<div class="row">
|
||||
<div class="col s12 l6 margin-bottom">
|
||||
<div class="col m12 xl4 margin-bottom">
|
||||
<a href="{% url 'sync:validate-source' source_type='youtube-channel' %}" class="btn">Add a YouTube channel <i class="fab fa-youtube"></i></a>
|
||||
</div>
|
||||
<div class="col s12 l6 margin-bottom">
|
||||
<div class="col m12 xl4 margin-bottom">
|
||||
<a href="{% url 'sync:validate-source' source_type='youtube-channel-id' %}" class="btn">Add a YouTube channel by ID <i class="fab fa-youtube"></i></a>
|
||||
</div>
|
||||
<div class="col m12 xl4 margin-bottom">
|
||||
<a href="{% url 'sync:validate-source' source_type='youtube-playlist' %}" class="btn">Add a YouTube playlist <i class="fab fa-youtube"></i></a>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -28,6 +28,7 @@ class FrontEndTestCase(TestCase):
|
|||
def test_validate_source(self):
|
||||
test_source_types = {
|
||||
'youtube-channel': Source.SOURCE_TYPE_YOUTUBE_CHANNEL,
|
||||
'youtube-channel-id': Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID,
|
||||
'youtube-playlist': Source.SOURCE_TYPE_YOUTUBE_PLAYLIST,
|
||||
}
|
||||
test_sources = {
|
||||
|
@ -36,8 +37,6 @@ class FrontEndTestCase(TestCase):
|
|||
'https://www.youtube.com/testchannel',
|
||||
'https://www.youtube.com/c/testchannel',
|
||||
'https://www.youtube.com/c/testchannel/videos',
|
||||
'https://www.youtube.com/channel/testchannel',
|
||||
'https://www.youtube.com/channel/testchannel/videos',
|
||||
),
|
||||
'invalid_schema': (
|
||||
'http://www.youtube.com/c/playlist',
|
||||
|
@ -53,13 +52,37 @@ class FrontEndTestCase(TestCase):
|
|||
),
|
||||
'invalid_is_playlist': (
|
||||
'https://www.youtube.com/c/playlist',
|
||||
'https://www.youtube.com/c/playlist',
|
||||
),
|
||||
'invalid_channel_with_id': (
|
||||
'https://www.youtube.com/channel/channelid',
|
||||
'https://www.youtube.com/channel/channelid/videos',
|
||||
),
|
||||
},
|
||||
'youtube-channel-id': {
|
||||
'valid': (
|
||||
'https://www.youtube.com/channel/channelid',
|
||||
'https://www.youtube.com/channel/channelid/videos',
|
||||
),
|
||||
'invalid_schema': (
|
||||
'http://www.youtube.com/channel/channelid',
|
||||
'ftp://www.youtube.com/channel/channelid',
|
||||
),
|
||||
'invalid_domain': (
|
||||
'https://www.test.com/channel/channelid',
|
||||
'https://www.example.com/channel/channelid',
|
||||
),
|
||||
'invalid_path': (
|
||||
'https://www.youtube.com/test/invalid',
|
||||
'https://www.youtube.com/channel/test/invalid',
|
||||
),
|
||||
'invalid_is_named_channel': (
|
||||
'https://www.youtube.com/c/testname',
|
||||
),
|
||||
},
|
||||
'youtube-playlist': {
|
||||
'valid': (
|
||||
'https://www.youtube.com/playlist?list=testplaylist'
|
||||
'https://www.youtube.com/watch?v=testvideo&list=testplaylist'
|
||||
'https://www.youtube.com/playlist?list=testplaylist',
|
||||
'https://www.youtube.com/watch?v=testvideo&list=testplaylist',
|
||||
),
|
||||
'invalid_schema': (
|
||||
'http://www.youtube.com/playlist?list=testplaylist',
|
||||
|
@ -76,6 +99,7 @@ class FrontEndTestCase(TestCase):
|
|||
'invalid_is_channel': (
|
||||
'https://www.youtube.com/testchannel',
|
||||
'https://www.youtube.com/c/testchannel',
|
||||
'https://www.youtube.com/channel/testchannel',
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -86,19 +110,21 @@ class FrontEndTestCase(TestCase):
|
|||
response = c.get('/source-validate/invalid')
|
||||
self.assertEqual(response.status_code, 404)
|
||||
for (source_type, tests) in test_sources.items():
|
||||
for test, field in tests.items():
|
||||
source_type_char = test_source_types.get(source_type)
|
||||
data = {'source_url': field, 'source_type': source_type_char}
|
||||
response = c.post(f'/source-validate/{source_type}', data)
|
||||
if test == 'valid':
|
||||
# Valid source tests should bounce to /source-add
|
||||
self.assertEqual(response.status_code, 302)
|
||||
url_parts = urlsplit(response.url)
|
||||
self.assertEqual(url_parts.path, '/source-add')
|
||||
else:
|
||||
# Invalid source tests should reload the page with an error message
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn('<ul class="errorlist">', response.content.decode())
|
||||
for test, urls in tests.items():
|
||||
for url in urls:
|
||||
source_type_char = test_source_types.get(source_type)
|
||||
data = {'source_url': url, 'source_type': source_type_char}
|
||||
response = c.post(f'/source-validate/{source_type}', data)
|
||||
if test == 'valid':
|
||||
# Valid source tests should bounce to /source-add
|
||||
self.assertEqual(response.status_code, 302)
|
||||
url_parts = urlsplit(response.url)
|
||||
self.assertEqual(url_parts.path, '/source-add')
|
||||
else:
|
||||
# Invalid source tests should reload the page with an error
|
||||
self.assertEqual(response.status_code, 200)
|
||||
self.assertIn('<ul class="errorlist">',
|
||||
response.content.decode())
|
||||
|
||||
def test_add_source_prepopulation(self):
|
||||
c = Client()
|
||||
|
|
|
@ -128,10 +128,12 @@ class ValidateSourceView(FormView):
|
|||
}
|
||||
source_types = {
|
||||
'youtube-channel': Source.SOURCE_TYPE_YOUTUBE_CHANNEL,
|
||||
'youtube-channel-id': Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID,
|
||||
'youtube-playlist': Source.SOURCE_TYPE_YOUTUBE_PLAYLIST,
|
||||
}
|
||||
help_item = {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: _('YouTube channel'),
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: _('YouTube channel ID'),
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: _('YouTube playlist'),
|
||||
}
|
||||
help_texts = {
|
||||
|
@ -141,6 +143,13 @@ class ValidateSourceView(FormView):
|
|||
'where <strong>CHANNELNAME</strong> is the name of the channel you want '
|
||||
'to add.'
|
||||
),
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: _(
|
||||
'Enter a YouTube channel URL by channel ID into the box below. A channel '
|
||||
'URL by channel ID will be in the format of <strong>'
|
||||
'https://www.youtube.com/channel/BiGLoNgUnIqUeId</strong> '
|
||||
'where <strong>BiGLoNgUnIqUeId</strong> is the ID of the channel you want '
|
||||
'to add.'
|
||||
),
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: _(
|
||||
'Enter a YouTube playlist URL into the box below. A playlist URL will be '
|
||||
'in the format of <strong>https://www.youtube.com/playlist?list='
|
||||
|
@ -150,6 +159,8 @@ class ValidateSourceView(FormView):
|
|||
}
|
||||
help_examples = {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: 'https://www.youtube.com/google',
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: ('https://www.youtube.com/channel/'
|
||||
'UCK8sQmJBp8GCxrOtXWBpyEA'),
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: ('https://www.youtube.com/playlist?list='
|
||||
'PL590L5WQmH8dpP0RyH5pCfIaDEdt9nk7r')
|
||||
}
|
||||
|
@ -157,12 +168,21 @@ class ValidateSourceView(FormView):
|
|||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: {
|
||||
'scheme': 'https',
|
||||
'domain': 'www.youtube.com',
|
||||
'path_regex': '^\/(c\/|channel\/)?([^\/]+)(\/videos)?$',
|
||||
'path_regex': '^\/(c\/)?([^\/]+)(\/videos)?$',
|
||||
'path_must_not_match': ('/playlist', '/c/playlist'),
|
||||
'qs_args': [],
|
||||
'extract_key': ('path_regex', 1),
|
||||
'example': 'https://www.youtube.com/SOMECHANNEL'
|
||||
},
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: {
|
||||
'scheme': 'https',
|
||||
'domain': 'www.youtube.com',
|
||||
'path_regex': '^\/channel\/([^\/]+)(\/videos)?$',
|
||||
'path_must_not_match': ('/playlist', '/c/playlist'),
|
||||
'qs_args': [],
|
||||
'extract_key': ('path_regex', 0),
|
||||
'example': 'https://www.youtube.com/channel/CHANNELID'
|
||||
},
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: {
|
||||
'scheme': 'https',
|
||||
'domain': 'www.youtube.com',
|
||||
|
@ -175,6 +195,7 @@ class ValidateSourceView(FormView):
|
|||
}
|
||||
prepopulate_fields = {
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL: ('source_type', 'key', 'name', 'directory'),
|
||||
Source.SOURCE_TYPE_YOUTUBE_CHANNEL_ID: ('source_type', 'key'),
|
||||
Source.SOURCE_TYPE_YOUTUBE_PLAYLIST: ('source_type', 'key'),
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue