1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-08-15 00:48:28 +00:00

Reduce code dup

This commit is contained in:
7x11x13 2025-01-01 17:47:30 -05:00
parent ec7250f145
commit 460ff55014

View File

@ -2460,31 +2460,30 @@ def __init__(self, ydl, info_dict):
@classmethod
def parse_playlist_items(cls, string, playlist_index):
def slice_from_mobj(segment, mobj):
start, end, step, has_range = mobj.group('start', 'end', 'step', 'range')
if int_or_none(step) == 0:
raise ValueError(f'Step in {segment!r} cannot be zero')
return (
slice(int_or_none(start), float_or_none(end), int_or_none(step))
if has_range
else slice(int(start), int(start))
)
for segment in string.split(','):
if not segment:
raise ValueError('There are two or more consecutive commas')
mobj = cls.PLAYLIST_ITEMS_RE.fullmatch(segment)
if mobj:
start, end, step, has_range = mobj.group('start', 'end', 'step', 'range')
if int_or_none(step) == 0:
raise ValueError(f'Step in {segment!r} cannot be zero')
yield slice(int_or_none(start), float_or_none(end), int_or_none(step)) if has_range else int(start)
yield slice_from_mobj(segment, mobj)
continue
if not cls.NESTED_PLAYLIST_RE.fullmatch(segment):
raise ValueError(f'{segment!r} is not a valid specification')
for depth, mobj in enumerate(cls.NESTED_PLAYLIST_SEGMENT_RE.finditer(segment)):
start, end, step, has_range = mobj.group('start', 'end', 'step', 'range')
if int_or_none(step) == 0:
raise ValueError(f'Step in {segment!r} cannot be zero')
slice_ = (
slice(int_or_none(start), float_or_none(end), int_or_none(step))
if has_range
else slice(int(start), int(start))
)
slice_ = slice_from_mobj(segment, mobj)
if depth == len(playlist_index):
yield slice_
break