Refactor __process_playlist using LazyList

This commit is contained in:
pukkandan 2021-05-28 22:07:11 +05:30
parent 55575225b4
commit 56a8fb4f77
No known key found for this signature in database
GPG Key ID: 0F00D95A001F4698

View File

@ -70,6 +70,7 @@
int_or_none, int_or_none,
iri_to_uri, iri_to_uri,
ISO3166Utils, ISO3166Utils,
LazyList,
locked_file, locked_file,
make_dir, make_dir,
make_HTTPS_handler, make_HTTPS_handler,
@ -1309,7 +1310,7 @@ def fill_missing_entries(entries, indexes):
playlist_results = [] playlist_results = []
playliststart = self.params.get('playliststart', 1) - 1 playliststart = self.params.get('playliststart', 1)
playlistend = self.params.get('playlistend') playlistend = self.params.get('playlistend')
# For backwards compatibility, interpret -1 as whole list # For backwards compatibility, interpret -1 as whole list
if playlistend == -1: if playlistend == -1:
@ -1329,50 +1330,38 @@ def iter_playlistitems(format):
playlistitems = orderedSet(iter_playlistitems(playlistitems_str)) playlistitems = orderedSet(iter_playlistitems(playlistitems_str))
ie_entries = ie_result['entries'] ie_entries = ie_result['entries']
msg = (
'Downloading %d videos' if not isinstance(ie_entries, list)
else 'Collected %d videos; downloading %%d of them' % len(ie_entries))
if not isinstance(ie_entries, (list, PagedList)):
ie_entries = LazyList(ie_entries)
def make_playlistitems_entries(list_ie_entries): entries = []
num_entries = len(list_ie_entries) for i in playlistitems or itertools.count(playliststart):
for i in playlistitems: if playlistitems is None and playlistend is not None and playlistend < i:
if -num_entries < i <= num_entries: break
yield list_ie_entries[i - 1] entry = None
elif incomplete_entries: try:
entry = ie_entries[i - 1]
if entry is None:
raise EntryNotInPlaylist() raise EntryNotInPlaylist()
except (IndexError, EntryNotInPlaylist):
if isinstance(ie_entries, list): if incomplete_entries:
n_all_entries = len(ie_entries) raise EntryNotInPlaylist()
if playlistitems: elif not playlistitems:
entries = list(make_playlistitems_entries(ie_entries)) break
else: entries.append(entry)
entries = ie_entries[playliststart:playlistend]
n_entries = len(entries)
msg = 'Collected %d videos; downloading %d of them' % (n_all_entries, n_entries)
elif isinstance(ie_entries, PagedList):
if playlistitems:
entries = []
for item in playlistitems:
entries.extend(ie_entries.getslice(
item - 1, item
))
else:
entries = ie_entries.getslice(
playliststart, playlistend)
n_entries = len(entries)
msg = 'Downloading %d videos' % n_entries
else: # iterable
if playlistitems:
entries = list(make_playlistitems_entries(list(itertools.islice(
ie_entries, 0, max(playlistitems)))))
else:
entries = list(itertools.islice(
ie_entries, playliststart, playlistend))
n_entries = len(entries)
msg = 'Downloading %d videos' % n_entries
if any((entry is None for entry in entries)):
raise EntryNotInPlaylist()
if not playlistitems and (playliststart or playlistend):
playlistitems = list(range(1 + playliststart, 1 + playliststart + len(entries)))
ie_result['entries'] = entries ie_result['entries'] = entries
# Save playlist_index before re-ordering
entries = [
((playlistitems[i - 1] if playlistitems else i), entry)
for i, entry in enumerate(entries, 1)
if entry is not None]
n_entries = len(entries)
if not playlistitems and (playliststart or playlistend):
playlistitems = list(range(playliststart, playliststart + n_entries))
ie_result['requested_entries'] = playlistitems ie_result['requested_entries'] = playlistitems
if self.params.get('allow_playlist_files', True): if self.params.get('allow_playlist_files', True):
@ -1419,11 +1408,6 @@ def make_playlistitems_entries(list_ie_entries):
self.report_error('Cannot write playlist description file ' + descfn) self.report_error('Cannot write playlist description file ' + descfn)
return return
# Save playlist_index before re-ordering
entries = [
((playlistitems[i - 1] if playlistitems else i), entry)
for i, entry in enumerate(entries, 1)]
if self.params.get('playlistreverse', False): if self.params.get('playlistreverse', False):
entries = entries[::-1] entries = entries[::-1]
if self.params.get('playlistrandom', False): if self.params.get('playlistrandom', False):
@ -1431,7 +1415,7 @@ def make_playlistitems_entries(list_ie_entries):
x_forwarded_for = ie_result.get('__x_forwarded_for_ip') x_forwarded_for = ie_result.get('__x_forwarded_for_ip')
self.to_screen('[%s] playlist %s: %s' % (ie_result['extractor'], playlist, msg)) self.to_screen('[%s] playlist %s: %s' % (ie_result['extractor'], playlist, msg % n_entries))
failures = 0 failures = 0
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):