mirror of
https://github.com/KevinMidboe/spotify-downloader.git
synced 2025-10-29 18:00:15 +00:00
Merge pull request #386 from ritiek/skip-file
Add command line options for skip and successful file
This commit is contained in:
@@ -28,6 +28,8 @@ default_conf = { 'spotify-downloader':
|
|||||||
'file-format' : '{artist} - {track_name}',
|
'file-format' : '{artist} - {track_name}',
|
||||||
'search-format' : '{artist} - {track_name} lyrics',
|
'search-format' : '{artist} - {track_name} lyrics',
|
||||||
'youtube-api-key' : None,
|
'youtube-api-key' : None,
|
||||||
|
'skip' : None,
|
||||||
|
'write-successful' : None,
|
||||||
'log-level' : 'INFO' }
|
'log-level' : 'INFO' }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,6 +177,12 @@ def get_arguments(raw_args=None, to_group=True, to_merge=True):
|
|||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-yk', '--youtube-api-key', default=config['youtube-api-key'],
|
'-yk', '--youtube-api-key', default=config['youtube-api-key'],
|
||||||
help=argparse.SUPPRESS)
|
help=argparse.SUPPRESS)
|
||||||
|
parser.add_argument(
|
||||||
|
'-sk', '--skip', default=config['skip'],
|
||||||
|
help='path to file containing tracks to skip')
|
||||||
|
parser.add_argument(
|
||||||
|
'-w', '--write-successful', default=config['write-successful'],
|
||||||
|
help='path to file to write successful tracks to')
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
'-c', '--config', default=None,
|
'-c', '--config', default=None,
|
||||||
help='path to custom config.yml file')
|
help='path to custom config.yml file')
|
||||||
|
|||||||
@@ -63,20 +63,31 @@ def check_exists(music_file, raw_song, meta_tags):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def download_list(text_file):
|
def download_list(tracks_file, skip_file=None, write_successful_file=None):
|
||||||
""" Download all songs from the list. """
|
""" Download all songs from the list. """
|
||||||
|
|
||||||
log.info('Checking and removing any duplicate tracks')
|
log.info('Checking and removing any duplicate tracks')
|
||||||
lines = internals.get_unique_tracks(text_file)
|
tracks = internals.get_unique_tracks(tracks_file)
|
||||||
|
|
||||||
# override file with unique tracks
|
# override file with unique tracks
|
||||||
with open(text_file, 'w') as listed:
|
with open(tracks_file, 'w') as f:
|
||||||
listed.write('\n'.join(lines))
|
f.write('\n'.join(tracks))
|
||||||
|
|
||||||
log.info(u'Preparing to download {} songs'.format(len(lines)))
|
# Remove tracks to skip from tracks list
|
||||||
|
if skip_file is not None:
|
||||||
|
skip_tracks = internals.get_unique_tracks(skip_file)
|
||||||
|
len_before = len(tracks)
|
||||||
|
tracks = [
|
||||||
|
track for track in tracks
|
||||||
|
if track not in skip_tracks
|
||||||
|
]
|
||||||
|
log.info('Skipping {} tracks'.format(len_before - len(tracks)))
|
||||||
|
|
||||||
|
|
||||||
|
log.info(u'Preparing to download {} songs'.format(len(tracks)))
|
||||||
downloaded_songs = []
|
downloaded_songs = []
|
||||||
|
|
||||||
for number, raw_song in enumerate(lines, 1):
|
for number, raw_song in enumerate(tracks, 1):
|
||||||
print('')
|
print('')
|
||||||
try:
|
try:
|
||||||
download_single(raw_song, number=number)
|
download_single(raw_song, number=number)
|
||||||
@@ -89,20 +100,25 @@ def download_list(text_file):
|
|||||||
download_single(raw_song, number=number)
|
download_single(raw_song, number=number)
|
||||||
# detect network problems
|
# detect network problems
|
||||||
except (urllib.request.URLError, TypeError, IOError):
|
except (urllib.request.URLError, TypeError, IOError):
|
||||||
lines.append(raw_song)
|
tracks.append(raw_song)
|
||||||
# remove the downloaded song from file
|
# remove the downloaded song from file
|
||||||
internals.trim_song(text_file)
|
internals.trim_song(tracks_file)
|
||||||
# and append it at the end of file
|
# and append it at the end of file
|
||||||
with open(text_file, 'a') as myfile:
|
with open(tracks_file, 'a') as f:
|
||||||
myfile.write(raw_song + '\n')
|
f.write('\n' + raw_song)
|
||||||
log.warning('Failed to download song. Will retry after other songs\n')
|
log.warning('Failed to download song. Will retry after other songs\n')
|
||||||
# wait 0.5 sec to avoid infinite looping
|
# wait 0.5 sec to avoid infinite looping
|
||||||
time.sleep(0.5)
|
time.sleep(0.5)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
downloaded_songs.append(raw_song)
|
downloaded_songs.append(raw_song)
|
||||||
log.debug('Removing downloaded song from text file')
|
# Add track to file of successful downloads
|
||||||
internals.trim_song(text_file)
|
log.debug('Adding downloaded song to write successful file')
|
||||||
|
if write_successful_file is not None:
|
||||||
|
with open(write_successful_file, 'a') as f:
|
||||||
|
f.write('\n' + raw_song)
|
||||||
|
log.debug('Removing downloaded song from tracks file')
|
||||||
|
internals.trim_song(tracks_file)
|
||||||
|
|
||||||
return downloaded_songs
|
return downloaded_songs
|
||||||
|
|
||||||
@@ -193,7 +209,11 @@ def main():
|
|||||||
if const.args.song:
|
if const.args.song:
|
||||||
download_single(raw_song=const.args.song)
|
download_single(raw_song=const.args.song)
|
||||||
elif const.args.list:
|
elif const.args.list:
|
||||||
download_list(text_file=const.args.list)
|
download_list(
|
||||||
|
tracks_file=const.args.list,
|
||||||
|
skip_file=const.args.skip,
|
||||||
|
write_successful_file=const.args.write_successful
|
||||||
|
)
|
||||||
elif const.args.playlist:
|
elif const.args.playlist:
|
||||||
spotify_tools.write_playlist(playlist_url=const.args.playlist)
|
spotify_tools.write_playlist(playlist_url=const.args.playlist)
|
||||||
elif const.args.album:
|
elif const.args.album:
|
||||||
|
|||||||
Reference in New Issue
Block a user