mirror of
https://github.com/soimort/you-get.git
synced 2025-02-03 16:53:56 +03:00
Added an auto rename option and fixed the force option
I've noticed that if I am downloading two videos with same names from youtube (e.g. https://www.youtube.com/watch?v=606hmlA_nxw and https://www.youtube.com/watch?v=CLrXTnggUeY), only one of them will be saved (usually the bigger one according to the original script "os.path.getsize(output_filepath) >= total_size * 0.9"). However, I want them both while preserving their names from youtube. So I modified the source code. It looks like there are a lot of changes, but I just added an indent and everything changed. Also, I've noticed that "force" is not working at all. I fixed that issue.
This commit is contained in:
parent
6e38f28474
commit
a22dce896c
@ -134,6 +134,7 @@ player = None
|
|||||||
extractor_proxy = None
|
extractor_proxy = None
|
||||||
cookies = None
|
cookies = None
|
||||||
output_filename = None
|
output_filename = None
|
||||||
|
auto_rename = False
|
||||||
|
|
||||||
fake_headers = {
|
fake_headers = {
|
||||||
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', # noqa
|
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', # noqa
|
||||||
@ -598,27 +599,40 @@ def url_save(
|
|||||||
tmp_headers['Referer'] = refer
|
tmp_headers['Referer'] = refer
|
||||||
file_size = url_size(url, faker=faker, headers=tmp_headers)
|
file_size = url_size(url, faker=faker, headers=tmp_headers)
|
||||||
|
|
||||||
if os.path.exists(filepath):
|
continue_renameing = True
|
||||||
if not force and file_size == os.path.getsize(filepath):
|
while continue_renameing:
|
||||||
if not is_part:
|
continue_renameing = False
|
||||||
if bar:
|
if os.path.exists(filepath):
|
||||||
bar.done()
|
if not force and file_size == os.path.getsize(filepath):
|
||||||
print(
|
if not is_part:
|
||||||
'Skipping {}: file already exists'.format(
|
if bar:
|
||||||
tr(os.path.basename(filepath))
|
bar.done()
|
||||||
|
print(
|
||||||
|
'Skipping {}: file already exists'.format(
|
||||||
|
tr(os.path.basename(filepath))
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
else:
|
||||||
|
if bar:
|
||||||
|
bar.update_received(file_size)
|
||||||
|
return
|
||||||
else:
|
else:
|
||||||
if bar:
|
if not is_part:
|
||||||
bar.update_received(file_size)
|
if bar:
|
||||||
return
|
bar.done()
|
||||||
else:
|
if not force and auto_rename:
|
||||||
if not is_part:
|
path, ext = os.path.basename(filepath).rsplit('.', 1)
|
||||||
if bar:
|
if (re.compile(' \(\d\)').match(path[-4:]) is None):
|
||||||
bar.done()
|
thisfile = path + ' (1).' + ext
|
||||||
print('Overwriting %s' % tr(os.path.basename(filepath)), '...')
|
else:
|
||||||
elif not os.path.exists(os.path.dirname(filepath)):
|
thisfile = path[:-2] + str(int(path[-2]) + 1) + ').' + ext
|
||||||
os.mkdir(os.path.dirname(filepath))
|
filepath = os.path.join(os.path.dirname(filepath), thisfile)
|
||||||
|
print('Changing name to %s' % tr(os.path.basename(filepath)), '...')
|
||||||
|
continue_renameing = True
|
||||||
|
continue
|
||||||
|
print('Overwriting %s' % tr(os.path.basename(filepath)), '...')
|
||||||
|
elif not os.path.exists(os.path.dirname(filepath)):
|
||||||
|
os.mkdir(os.path.dirname(filepath))
|
||||||
|
|
||||||
temp_filepath = filepath + '.download' if file_size != float('inf') \
|
temp_filepath = filepath + '.download' if file_size != float('inf') \
|
||||||
else filepath
|
else filepath
|
||||||
@ -883,7 +897,7 @@ def download_urls(
|
|||||||
output_filepath = os.path.join(output_dir, output_filename)
|
output_filepath = os.path.join(output_dir, output_filename)
|
||||||
|
|
||||||
if total_size:
|
if total_size:
|
||||||
if not force and os.path.exists(output_filepath) \
|
if not force and os.path.exists(output_filepath) and not auto_rename\
|
||||||
and os.path.getsize(output_filepath) >= total_size * 0.9:
|
and os.path.getsize(output_filepath) >= total_size * 0.9:
|
||||||
print('Skipping %s: file already exists' % output_filepath)
|
print('Skipping %s: file already exists' % output_filepath)
|
||||||
print()
|
print()
|
||||||
@ -1370,6 +1384,10 @@ def script_main(download, download_playlist, **kwargs):
|
|||||||
'-l', '--playlist', action='store_true',
|
'-l', '--playlist', action='store_true',
|
||||||
help='Prefer to download a playlist'
|
help='Prefer to download a playlist'
|
||||||
)
|
)
|
||||||
|
download_grp.add_argument(
|
||||||
|
'-a', '--auto-rename', action='store_true', default=False,
|
||||||
|
help='Auto rename same name different files'
|
||||||
|
)
|
||||||
|
|
||||||
proxy_grp = parser.add_argument_group('Proxy options')
|
proxy_grp = parser.add_argument_group('Proxy options')
|
||||||
proxy_grp = proxy_grp.add_mutually_exclusive_group()
|
proxy_grp = proxy_grp.add_mutually_exclusive_group()
|
||||||
@ -1414,11 +1432,16 @@ def script_main(download, download_playlist, **kwargs):
|
|||||||
global player
|
global player
|
||||||
global extractor_proxy
|
global extractor_proxy
|
||||||
global output_filename
|
global output_filename
|
||||||
|
global auto_rename
|
||||||
|
|
||||||
output_filename = args.output_filename
|
output_filename = args.output_filename
|
||||||
extractor_proxy = args.extractor_proxy
|
extractor_proxy = args.extractor_proxy
|
||||||
|
|
||||||
info_only = args.info
|
info_only = args.info
|
||||||
|
if args.force:
|
||||||
|
force = True
|
||||||
|
if args.auto_rename:
|
||||||
|
auto_rename = True
|
||||||
if args.url:
|
if args.url:
|
||||||
dry_run = True
|
dry_run = True
|
||||||
if args.json:
|
if args.json:
|
||||||
|
Loading…
Reference in New Issue
Block a user