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

Open
ray2301 wants to merge 1 commit from ray2301/main into main
ray2301 commented 2023-12-05 01:46:27 -05:00 (Migrated from github.com)
  • added a sanitize_string function to sanitize strings for filenames or directories
  • used this function to sanitize strings before constructing file paths
  • modify the regular expression to correctly match filenames that start with the sanitized filename
  • changed the file renaming process to use shutil.move instead of Path(filename_temp).rename(filename) to handle potential issues when the source and destination are in the same directory (files being renamed to "._1")

needs to be tested more, but it fixes the problem from the title.

- added a sanitize_string function to sanitize strings for filenames or directories - used this function to sanitize strings before constructing file paths - modify the regular expression to correctly match filenames that start with the sanitized filename - changed the file renaming process to use shutil.move instead of Path(filename_temp).rename(filename) to handle potential issues when the source and destination are in the same directory (files being renamed to "._1") needs to be tested more, but it fixes the problem from the title.
shinji257 commented 2023-12-29 14:43:32 -05:00 (Migrated from github.com)

I know you said it still needs testing but I hit an album where I can consistently reproduce and I get the error with this one even with your patch.

###   SKIPPING SONG - FAILED TO QUERY METADATA   ###
Track_ID: 7aA5ODlMMfksbG1v4rXzmS
album_num: 01
artist: AmaLee
album: STYX HELIX (From _Re_Zero_)
album_id: 4vkdPM6LjXRpLw9rMaGM6v


bad escape \M at position 3

Traceback (most recent call last):
  File "C:\Users\shinj\AppData\Local\pipx\pipx\venvs\zotify\Lib\site-packages\zotify\track.py", line 201, in download_track
    c = len([file for file in Path(filedir).iterdir() if re.search(f'^{filename}_', str(file))]) + 1
                                                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\__init__.py", line 177, in search
    return _compile(pattern, flags).search(string)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\__init__.py", line 307, in _compile
    p = _compiler.compile(pattern, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_compiler.py", line 745, in compile
    p = _parser.parse(p, flags)
        ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_parser.py", line 979, in parse
    p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_parser.py", line 460, in _parse_sub
    itemsappend(_parse(source, state, verbose, nested + 1,
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_parser.py", line 544, in _parse
    code = _escape(source, this, state)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_parser.py", line 443, in _escape
    raise source.error("bad escape %s" % escape, len(escape))
re.error: bad escape \M at position 3
I know you said it still needs testing but I hit an album where I can consistently reproduce and I get the error with this one even with your patch. ``` ### SKIPPING SONG - FAILED TO QUERY METADATA ### Track_ID: 7aA5ODlMMfksbG1v4rXzmS album_num: 01 artist: AmaLee album: STYX HELIX (From _Re_Zero_) album_id: 4vkdPM6LjXRpLw9rMaGM6v bad escape \M at position 3 Traceback (most recent call last): File "C:\Users\shinj\AppData\Local\pipx\pipx\venvs\zotify\Lib\site-packages\zotify\track.py", line 201, in download_track c = len([file for file in Path(filedir).iterdir() if re.search(f'^{filename}_', str(file))]) + 1 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\__init__.py", line 177, in search return _compile(pattern, flags).search(string) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\__init__.py", line 307, in _compile p = _compiler.compile(pattern, flags) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_compiler.py", line 745, in compile p = _parser.parse(p, flags) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_parser.py", line 979, in parse p = _parse_sub(source, state, flags & SRE_FLAG_VERBOSE, 0) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_parser.py", line 460, in _parse_sub itemsappend(_parse(source, state, verbose, nested + 1, ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_parser.py", line 544, in _parse code = _escape(source, this, state) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\shinj\scoop\apps\python\3.12.1\Lib\re\_parser.py", line 443, in _escape raise source.error("bad escape %s" % escape, len(escape)) re.error: bad escape \M at position 3 ```
jteijema commented 2024-02-27 12:25:41 -05:00 (Migrated from github.com)
if not check_id and check_name:
    # Convert filename to a string before escaping
    filename_str = str(PurePath(filename))
    pattern = re.escape(filename_str) + '_'
    
    c = len([file for file in Path(filedir).iterdir() if re.search(f'^{pattern}', str(file))]) + 1

    fname = PurePath(filename).stem
    ext = PurePath(filename).suffix

    # Use the filename_str for pattern matching and then construct the new filename with the original Path object
    filename = PurePath(filedir).joinpath(f'{fname}_{c}{ext}')

Won't this be enough to fix it? It works for me hahaha

```python if not check_id and check_name: # Convert filename to a string before escaping filename_str = str(PurePath(filename)) pattern = re.escape(filename_str) + '_' c = len([file for file in Path(filedir).iterdir() if re.search(f'^{pattern}', str(file))]) + 1 fname = PurePath(filename).stem ext = PurePath(filename).suffix # Use the filename_str for pattern matching and then construct the new filename with the original Path object filename = PurePath(filedir).joinpath(f'{fname}_{c}{ext}') ``` Won't this be enough to fix it? It works for me hahaha
Googolplexed0 commented 2024-06-30 18:49:07 -04:00 (Migrated from github.com)

This was my fix, got the line count down and haven't had any naming issues since. Didn't do any sanitization though.

if not check_id and check_name:
    c = len([file for file in Path(filedir).iterdir() if file.match(filename.stem + "*")])
    filename = PurePath(filedir).joinpath(f'{filename.stem}_{c}{filename.suffix}')

Got around having to use shutil when renaming temps this way.

if filename_temp != filename:
    if Path(filename).exists():
        Path(filename).unlink()
    Path(filename_temp).rename(filename)

Both implemented on my fork and works well enough for me. I have not tested it extensively, though. I will probably come back to this.

This was my fix, got the line count down and haven't had any naming issues since. Didn't do any sanitization though. ```py if not check_id and check_name: c = len([file for file in Path(filedir).iterdir() if file.match(filename.stem + "*")]) filename = PurePath(filedir).joinpath(f'{filename.stem}_{c}{filename.suffix}') ``` Got around having to use shutil when renaming temps this way. ```py if filename_temp != filename: if Path(filename).exists(): Path(filename).unlink() Path(filename_temp).rename(filename) ``` Both implemented on my [fork](https://github.com/Googolplexed0/zotify) and works well enough for me. I have not tested it extensively, though. I will probably come back to this.
booth-w commented 2024-06-30 20:36:52 -04:00 (Migrated from github.com)

There is already a function for sanitising strings for file names. zotify-dev/zotify@fa2156b2a1/zotify/utils.py (L245-L266)

There is already a function for sanitising strings for file names. https://github.com/zotify-dev/zotify/blob/fa2156b2a17627578860ee7ec2732e41d4f25afc/zotify/utils.py#L245-L266
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin ray2301/main:ray2301/main
git switch ray2301/main

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch main
git merge --no-ff ray2301/main
git switch ray2301/main
git rebase main
git switch main
git merge --ff-only ray2301/main
git switch ray2301/main
git rebase main
git switch main
git merge --no-ff ray2301/main
git switch main
git merge --squash ray2301/main
git switch main
git merge --ff-only ray2301/main
git switch main
git merge ray2301/main
git push origin main
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
zotify-dev/zotify!52
No description provided.