mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Add --overwrite option (#182)
* Add overwrite option * Fix tests * address changes requested
This commit is contained in:
committed by
Ritiek Malhotra
parent
64548b6894
commit
01bb783724
@@ -196,7 +196,8 @@ and it will start downloading songs mentioned in `list.txt`.
|
|||||||
resume from the song where you stopped it the next time you want to download
|
resume from the song where you stopped it the next time you want to download
|
||||||
the songs present in `list.txt`.
|
the songs present in `list.txt`.
|
||||||
|
|
||||||
- Songs that are already downloaded will be skipped and not be downloaded again.
|
- Songs that are already downloaded will prompt you to overwrite or skip. This behavior can be changed by
|
||||||
|
passing `--overwrite {prompt,skip,force}`.
|
||||||
|
|
||||||
#### Download by Playlist Link
|
#### Download by Playlist Link
|
||||||
|
|
||||||
|
|||||||
@@ -62,6 +62,10 @@ def get_arguments():
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-f', '--folder', default=(os.path.join(sys.path[0], 'Music')),
|
'-f', '--folder', default=(os.path.join(sys.path[0], 'Music')),
|
||||||
help='path to folder where files will be stored in')
|
help='path to folder where files will be stored in')
|
||||||
|
parser.add_argument(
|
||||||
|
'--overwrite', default='prompt',
|
||||||
|
help='change the overwrite policy',
|
||||||
|
choices={'prompt', 'force', 'skip'})
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-i', '--input-ext', default='.m4a',
|
'-i', '--input-ext', default='.m4a',
|
||||||
help='prefered input format .m4a or .webm (Opus)')
|
help='prefered input format .m4a or .webm (Opus)')
|
||||||
|
|||||||
23
spotdl.py
23
spotdl.py
@@ -280,7 +280,7 @@ def download_song(file_name, content):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
def check_exists(music_file, raw_song, meta_tags, islist=True):
|
def check_exists(music_file, raw_song, meta_tags):
|
||||||
""" Check if the input song already exists in the given folder. """
|
""" Check if the input song already exists in the given folder. """
|
||||||
log.debug('Cleaning any temp files and checking '
|
log.debug('Cleaning any temp files and checking '
|
||||||
'if "{}" already exists'.format(music_file))
|
'if "{}" already exists'.format(music_file))
|
||||||
@@ -304,11 +304,8 @@ def check_exists(music_file, raw_song, meta_tags, islist=True):
|
|||||||
os.remove(os.path.join(args.folder, song))
|
os.remove(os.path.join(args.folder, song))
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# if downloading only single song, prompt to re-download
|
log.warning('"{}" already exists'.format(song))
|
||||||
if islist:
|
if args.overwrite == 'prompt':
|
||||||
log.warning('"{}" already exists'.format(song))
|
|
||||||
return True
|
|
||||||
else:
|
|
||||||
log.info('"{}" has already been downloaded. '
|
log.info('"{}" has already been downloaded. '
|
||||||
'Re-download? (y/N): '.format(song))
|
'Re-download? (y/N): '.format(song))
|
||||||
prompt = input('> ')
|
prompt = input('> ')
|
||||||
@@ -317,6 +314,13 @@ def check_exists(music_file, raw_song, meta_tags, islist=True):
|
|||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
elif args.overwrite == 'force':
|
||||||
|
os.remove(os.path.join(args.folder, song))
|
||||||
|
log.info('Overwriting "{}"'.format(song))
|
||||||
|
return False
|
||||||
|
elif args.overwrite == 'skip':
|
||||||
|
log.info('Skipping "{}"'.format(song))
|
||||||
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@@ -401,11 +405,6 @@ def grab_album(album):
|
|||||||
|
|
||||||
def grab_single(raw_song, number=None):
|
def grab_single(raw_song, number=None):
|
||||||
""" Logic behind downloading a song. """
|
""" Logic behind downloading a song. """
|
||||||
if number:
|
|
||||||
islist = True
|
|
||||||
else:
|
|
||||||
islist = False
|
|
||||||
|
|
||||||
if internals.is_youtube(raw_song):
|
if internals.is_youtube(raw_song):
|
||||||
log.debug('Input song is a YouTube URL')
|
log.debug('Input song is a YouTube URL')
|
||||||
content = go_pafy(raw_song, meta_tags=None)
|
content = go_pafy(raw_song, meta_tags=None)
|
||||||
@@ -433,7 +432,7 @@ def grab_single(raw_song, number=None):
|
|||||||
|
|
||||||
file_name = internals.sanitize_title(songname)
|
file_name = internals.sanitize_title(songname)
|
||||||
|
|
||||||
if not check_exists(file_name, raw_song, meta_tags, islist=islist):
|
if not check_exists(file_name, raw_song, meta_tags):
|
||||||
if download_song(file_name, content):
|
if download_song(file_name, content):
|
||||||
input_song = file_name + args.input_ext
|
input_song = file_name + args.input_ext
|
||||||
output_song = file_name + args.output_ext
|
output_song = file_name + args.output_ext
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ class TestArgs:
|
|||||||
output_ext = '.mp3'
|
output_ext = '.mp3'
|
||||||
folder = 'test'
|
folder = 'test'
|
||||||
log_level = logger.logging.DEBUG
|
log_level = logger.logging.DEBUG
|
||||||
|
overwrite = 'skip'
|
||||||
|
|
||||||
test_args = TestArgs()
|
test_args = TestArgs()
|
||||||
setattr(spotdl, "args", test_args)
|
setattr(spotdl, "args", test_args)
|
||||||
@@ -39,7 +40,7 @@ def test_check_exists():
|
|||||||
expect_check = False
|
expect_check = False
|
||||||
# prerequisites for determining filename
|
# prerequisites for determining filename
|
||||||
file_name = spotdl.internals.sanitize_title(title)
|
file_name = spotdl.internals.sanitize_title(title)
|
||||||
check = spotdl.check_exists(file_name, raw_song, meta_tags=None, islist=True)
|
check = spotdl.check_exists(file_name, raw_song, meta_tags=None)
|
||||||
assert check == expect_check
|
assert check == expect_check
|
||||||
|
|
||||||
|
|
||||||
@@ -79,6 +80,6 @@ def test_check_exists2():
|
|||||||
# prerequisites for determining filename
|
# prerequisites for determining filename
|
||||||
file_name = spotdl.internals.sanitize_title(title)
|
file_name = spotdl.internals.sanitize_title(title)
|
||||||
os.remove(os.path.join(spotdl.args.folder, input_song))
|
os.remove(os.path.join(spotdl.args.folder, input_song))
|
||||||
check = spotdl.check_exists(file_name, raw_song, meta_tags=None, islist=True)
|
check = spotdl.check_exists(file_name, raw_song, meta_tags=None)
|
||||||
os.remove(os.path.join(spotdl.args.folder, output_song))
|
os.remove(os.path.join(spotdl.args.folder, output_song))
|
||||||
assert check == expect_check
|
assert check == expect_check
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ class TestArgs:
|
|||||||
output_ext = '.mp3'
|
output_ext = '.mp3'
|
||||||
folder = 'test'
|
folder = 'test'
|
||||||
log_level = 'DEBUG'
|
log_level = 'DEBUG'
|
||||||
|
overwrite = 'skip'
|
||||||
|
|
||||||
test_args = TestArgs()
|
test_args = TestArgs()
|
||||||
setattr(spotdl, "args", test_args)
|
setattr(spotdl, "args", test_args)
|
||||||
@@ -49,7 +50,7 @@ def test_check_exists():
|
|||||||
songname = spotdl.generate_songname(meta_tags)
|
songname = spotdl.generate_songname(meta_tags)
|
||||||
global file_name
|
global file_name
|
||||||
file_name = spotdl.internals.sanitize_title(songname)
|
file_name = spotdl.internals.sanitize_title(songname)
|
||||||
check = spotdl.check_exists(file_name, raw_song, meta_tags, islist=True)
|
check = spotdl.check_exists(file_name, raw_song, meta_tags)
|
||||||
assert check == expect_check
|
assert check == expect_check
|
||||||
|
|
||||||
|
|
||||||
@@ -85,6 +86,6 @@ def test_check_exists2():
|
|||||||
expect_check = True
|
expect_check = True
|
||||||
# prerequisites for determining filename
|
# prerequisites for determining filename
|
||||||
os.remove(os.path.join(spotdl.args.folder, input_song))
|
os.remove(os.path.join(spotdl.args.folder, input_song))
|
||||||
check = spotdl.check_exists(file_name, raw_song, meta_tags, islist=True)
|
check = spotdl.check_exists(file_name, raw_song, meta_tags)
|
||||||
os.remove(os.path.join(spotdl.args.folder, output_song))
|
os.remove(os.path.join(spotdl.args.folder, output_song))
|
||||||
assert check == expect_check
|
assert check == expect_check
|
||||||
|
|||||||
Reference in New Issue
Block a user