Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def determine_filenames(self, extension):
expected_filenames = []
for song in self.artist.songs:
fn = "lyrics_{name}_{song}.{ext}".format(name=self.artist.name,
song=song.title,
ext=extension)
fn = sanitize_filename(fn.lower().replace(" ", ""))
expected_filenames.append(fn)
return expected_filenames
result is returned as a string.
:sanitize: Sanitizes the filename if True.
OUTPUT
If `filename` is None, returns the lyrics as
a plain string. Otherwise None.
"""
data = self._body
data['songs'] = [song._body for song in self.songs]
# Return the json string if no output path was specified
if not filename:
return json.dumps(data, indent=1)
# Save Song object to a json file
filename = sanitize_filename(filename) if sanitize else filename
with open(filename, 'w') as ff:
json.dump(data, ff, indent=1)
return None
result is returned as a string.
:binary_encoding: Enable binary encoding of text data.
:sanitize: Sanitizes the filename if True.
OUTPUT
If `filename` is None, returns the lyrics as
a plain string. Otherwise None.
"""
data = ' '.join(song.lyrics for song in self.songs)
# Return the lyrics as a string if no `filename` was specified
if not filename:
return data
# Save song lyrics to a text file
filename = sanitize_filename(filename) if sanitize else filename
with open(filename, 'wb' if binary_encoding else 'w') as ff:
if binary_encoding:
data = data.encode('utf8')
ff.write(data)
return None
def save_lyrics(self,
filename=None,
extension='json',
overwrite=False,
binary_encoding=False,
sanitize=True,
verbose=True):
"""Saves all lyrics within an Artist object to a single file."""
extension = extension.lstrip(".").lower()
assert (extension == 'json') or (extension == 'txt'), "extension must be JSON or TXT"
# Determine the filename
if not filename:
filename = 'Lyrics_' + self.name.replace(' ', '') + '.' + extension
filename = sanitize_filename(filename) if sanitize else filename
# Check if file already exists
write_file = False
if overwrite or not os.path.isfile(filename):
write_file = True
elif verbose:
if input("{} already exists. Overwrite?\n(y/n): ".format(filename)).lower() == 'y':
write_file = True
# Exit if we won't be saving a file
if not write_file:
if verbose:
print('Skipping file save.\n')
return
# Save the lyrics to a file