Fix `playlist_index` not obeying `playlist_start`

and add tests
Closes #720
pull/729/head
pukkandan 3 years ago
parent 8f18aca871
commit 9e598870dd
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698

@ -978,54 +978,31 @@ class TestYoutubeDL(unittest.TestCase):
ydl.process_ie_result(copy.deepcopy(playlist)) ydl.process_ie_result(copy.deepcopy(playlist))
return ydl.downloaded_info_dicts return ydl.downloaded_info_dicts
def get_ids(params): def test_selection(params, expected_ids):
return [int(v['id']) for v in get_downloaded_info_dicts(params)] results = [
(v['playlist_autonumber'] - 1, (int(v['id']), v['playlist_index']))
result = get_ids({}) for v in get_downloaded_info_dicts(params)]
self.assertEqual(result, [1, 2, 3, 4]) self.assertEqual(results, list(enumerate(zip(expected_ids, expected_ids))))
result = get_ids({'playlistend': 10}) test_selection({}, [1, 2, 3, 4])
self.assertEqual(result, [1, 2, 3, 4]) test_selection({'playlistend': 10}, [1, 2, 3, 4])
test_selection({'playlistend': 2}, [1, 2])
result = get_ids({'playlistend': 2}) test_selection({'playliststart': 10}, [])
self.assertEqual(result, [1, 2]) test_selection({'playliststart': 2}, [2, 3, 4])
test_selection({'playlist_items': '2-4'}, [2, 3, 4])
result = get_ids({'playliststart': 10}) test_selection({'playlist_items': '2,4'}, [2, 4])
self.assertEqual(result, []) test_selection({'playlist_items': '10'}, [])
result = get_ids({'playliststart': 2})
self.assertEqual(result, [2, 3, 4])
result = get_ids({'playlist_items': '2-4'})
self.assertEqual(result, [2, 3, 4])
result = get_ids({'playlist_items': '2,4'})
self.assertEqual(result, [2, 4])
result = get_ids({'playlist_items': '10'})
self.assertEqual(result, [])
result = get_ids({'playlist_items': '3-10'})
self.assertEqual(result, [3, 4])
result = get_ids({'playlist_items': '2-4,3-4,3'})
self.assertEqual(result, [2, 3, 4])
# Tests for https://github.com/ytdl-org/youtube-dl/issues/10591 # Tests for https://github.com/ytdl-org/youtube-dl/issues/10591
# @{ test_selection({'playlist_items': '2-4,3-4,3'}, [2, 3, 4])
result = get_downloaded_info_dicts({'playlist_items': '2-4,3-4,3'}) test_selection({'playlist_items': '4,2'}, [4, 2])
self.assertEqual(result[0]['playlist_index'], 2)
self.assertEqual(result[1]['playlist_index'], 3) # Tests for https://github.com/yt-dlp/yt-dlp/issues/720
# https://github.com/yt-dlp/yt-dlp/issues/302
result = get_downloaded_info_dicts({'playlist_items': '2-4,3-4,3'}) test_selection({'playlistreverse': True}, [4, 3, 2, 1])
self.assertEqual(result[0]['playlist_index'], 2) test_selection({'playliststart': 2, 'playlistreverse': True}, [4, 3, 2])
self.assertEqual(result[1]['playlist_index'], 3) test_selection({'playlist_items': '2,4', 'playlistreverse': True}, [4, 2])
self.assertEqual(result[2]['playlist_index'], 4) test_selection({'playlist_items': '4,2'}, [4, 2])
result = get_downloaded_info_dicts({'playlist_items': '4,2'})
self.assertEqual(result[0]['playlist_index'], 4)
self.assertEqual(result[1]['playlist_index'], 2)
# @}
def test_urlopen_no_file_protocol(self): def test_urlopen_no_file_protocol(self):
# see https://github.com/ytdl-org/youtube-dl/issues/8227 # see https://github.com/ytdl-org/youtube-dl/issues/8227

@ -1452,7 +1452,7 @@ class YoutubeDL(object):
# Save playlist_index before re-ordering # Save playlist_index before re-ordering
entries = [ entries = [
((playlistitems[i - 1] if playlistitems else i), entry) ((playlistitems[i - 1] if playlistitems else i + playliststart - 1), entry)
for i, entry in enumerate(entries, 1) for i, entry in enumerate(entries, 1)
if entry is not None] if entry is not None]
n_entries = len(entries) n_entries = len(entries)
@ -1517,7 +1517,7 @@ class YoutubeDL(object):
max_failures = self.params.get('skip_playlist_after_errors') or float('inf') max_failures = self.params.get('skip_playlist_after_errors') or float('inf')
for i, entry_tuple in enumerate(entries, 1): for i, entry_tuple in enumerate(entries, 1):
playlist_index, entry = entry_tuple playlist_index, entry = entry_tuple
if 'playlist_index' in self.params.get('compat_options', []): if 'playlist-index' in self.params.get('compat_options', []):
playlist_index = playlistitems[i - 1] if playlistitems else i playlist_index = playlistitems[i - 1] if playlistitems else i
self.to_screen('[download] Downloading video %s of %s' % (i, n_entries)) self.to_screen('[download] Downloading video %s of %s' % (i, n_entries))
# This __x_forwarded_for_ip thing is a bit ugly but requires # This __x_forwarded_for_ip thing is a bit ugly but requires

Loading…
Cancel
Save