Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update track.py (fix "bad escape \M") #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions zotify/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,17 @@ def get_song_duration(song_id: str) -> float:

return duration

# Function to sanitize strings for filenames or directories
def sanitize_string(s):
# Check if the input is a string or bytes-like object
if isinstance(s, (str, bytes)):
# Replace invalid characters with underscores
return re.sub(r'[\/:*?"<>|]', '_', s)
else:
# Handle cases where the input is not a string or bytes-like object
return str(s)



def download_track(mode: str, track_id: str, extra_keys=None, disable_progressbar=False) -> None:
""" Downloads raw song audio from Spotify """
Expand Down Expand Up @@ -187,12 +198,17 @@ def download_track(mode: str, track_id: str, extra_keys=None, disable_progressba

# a song with the same name is installed
if not check_id and check_name:
c = len([file for file in Path(filedir).iterdir() if re.search(f'^{filename}_', str(file))]) + 1
c = len([file for file in Path(filedir).iterdir() if re.search(re.escape(f'{filename}_'), str(file))]) + 1

fname = PurePath(PurePath(filename).name).parent
ext = PurePath(PurePath(filename).name).suffix

filename = PurePath(filedir).joinpath(f'{fname}_{c}{ext}')
# Sanitize the strings before constructing the file path
sanitized_fname = sanitize_string(fname)
sanitized_ext = sanitize_string(ext)

filename = PurePath(filedir).joinpath(f'{sanitized_fname}_{c}{sanitized_ext}')


except Exception as e:
Printer.print(PrintChannel.ERRORS, '### SKIPPING SONG - FAILED TO QUERY METADATA ###')
Expand Down Expand Up @@ -267,7 +283,8 @@ def download_track(mode: str, track_id: str, extra_keys=None, disable_progressba
Printer.print(PrintChannel.ERRORS, "Unable to write metadata, ensure ffmpeg is installed and added to your PATH.")

if filename_temp != filename:
Path(filename_temp).rename(filename)
import shutil
shutil.move(filename_temp, filename)

time_finished = time.time()

Expand Down