mirror of
https://github.com/soimort/you-get.git
synced 2025-03-12 10:20:13 +03:00
added range support for playlist
This commit is contained in:
parent
1c841f7e8c
commit
c2e5bf880e
@ -151,6 +151,62 @@ else:
|
|||||||
default_encoding = locale.getpreferredencoding().lower()
|
default_encoding = locale.getpreferredencoding().lower()
|
||||||
|
|
||||||
|
|
||||||
|
class RangeContainer:
|
||||||
|
def __init__(self, ranges):
|
||||||
|
self.ranges = ranges
|
||||||
|
|
||||||
|
def __contains__(self, value):
|
||||||
|
for ran in self.ranges:
|
||||||
|
if ((not ran[0] or ran[0] <= value) and
|
||||||
|
(not ran[1] or ran[1] >= value)):
|
||||||
|
return True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
result = []
|
||||||
|
for ran in self.ranges:
|
||||||
|
if ran[0] is None:
|
||||||
|
result.append("-")
|
||||||
|
else:
|
||||||
|
result.append(str(ran[0]))
|
||||||
|
if ran[0] != ran[1]:
|
||||||
|
result.append("-")
|
||||||
|
if ran[1] is not None and ran[1] != ran[0]:
|
||||||
|
result.append(str(ran[1]))
|
||||||
|
return " ".join(result)
|
||||||
|
|
||||||
|
|
||||||
|
class RangeProcessor(argparse.Action):
|
||||||
|
def __call__(self, parser, namespace, values, option_string=None):
|
||||||
|
ranges = []
|
||||||
|
last_num = None
|
||||||
|
ranging = False
|
||||||
|
for s in values:
|
||||||
|
if s == '-':
|
||||||
|
ranging = True
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
n = int(s)
|
||||||
|
except:
|
||||||
|
raise Exception('invalid ranging index: ' + s)
|
||||||
|
if n < 1:
|
||||||
|
raise Exception('ranging index must be positive: ' + s)
|
||||||
|
if ranging:
|
||||||
|
ranges.append([last_num, n])
|
||||||
|
ranging = False
|
||||||
|
last_num = None
|
||||||
|
else:
|
||||||
|
if last_num:
|
||||||
|
ranges.append([last_num, last_num])
|
||||||
|
last_num = n
|
||||||
|
if ranging:
|
||||||
|
ranges.append([last_num, None])
|
||||||
|
last_num = None
|
||||||
|
if last_num:
|
||||||
|
ranges.append([last_num, last_num])
|
||||||
|
setattr(namespace, self.dest, RangeContainer(ranges))
|
||||||
|
|
||||||
|
|
||||||
def rc4(key, data):
|
def rc4(key, data):
|
||||||
# all encryption algo should work on bytes
|
# all encryption algo should work on bytes
|
||||||
assert type(key) == type(data) and type(key) == type(b'')
|
assert type(key) == type(data) and type(key) == type(b'')
|
||||||
@ -1557,6 +1613,14 @@ def script_main(download, download_playlist, **kwargs):
|
|||||||
)
|
)
|
||||||
|
|
||||||
playlist_grp = parser.add_argument_group('Playlist optional options')
|
playlist_grp = parser.add_argument_group('Playlist optional options')
|
||||||
|
playlist_grp.add_argument(
|
||||||
|
'-r', '--range', nargs='+', metavar="N", action=RangeProcessor,
|
||||||
|
default=RangeContainer([[None, None]]),
|
||||||
|
help='''Specify a list of or a range of videos to download.
|
||||||
|
If preceeded by the URL, seperate them using '--'.
|
||||||
|
e.g. `-r - 3 7 8 - 12 16 -` will download videos
|
||||||
|
1 to 3, 7, 8 to 12, 16 and onwrads.'''
|
||||||
|
)
|
||||||
playlist_grp.add_argument(
|
playlist_grp.add_argument(
|
||||||
'--first', metavar='FIRST',
|
'--first', metavar='FIRST',
|
||||||
help='the first number'
|
help='the first number'
|
||||||
|
Loading…
x
Reference in New Issue
Block a user