diff --git a/app/replace_files.py b/app/replace_files.py index 54c25a8..c0d8a22 100755 --- a/app/replace_files.py +++ b/app/replace_files.py @@ -32,6 +32,12 @@ from thefuzz import process from string import ascii_lowercase as letters from typing import List +# ###################### SETTINGS ######################### +process_multiple_matches = True +do_processing = True +process_no_matches = True +# ######################################################### + def insensitive_glob(pattern): def either(c): @@ -44,10 +50,6 @@ source_dir = '/home/kae/music/Singles/tmp' # os.getcwd() parent_dir = os.path.dirname(source_dir) assert source_dir != parent_dir -process_multiple_matches = True -do_processing = False -process_no_matches = True - name_and_tags: List[str] = [] name_not_tags: List[str] = [] tags_not_name: List[str] = [] @@ -89,33 +91,47 @@ def main(): matches = insensitive_glob(os.path.join(parent_dir, stem) + '*') match_count = len(matches) if match_count == 0: - no_match.append(f"{fname}, {us_t=}, {us_a=}") - print(f"\n file={fname}\n title={us_t}\n artist={us_a}\n") - # Try fuzzy search - d = {} - while True: - for i, match in enumerate( + if process_no_matches: + print(f"\n file={fname}\n title={us_t}\n artist={us_a}\n") + # Try fuzzy search + d = {} + while True: + for i, match in enumerate( [a[0] for a in process.extract(fname, tracks, limit=5)] - ): - d[i] = match - for k, v in d.items(): - print(f"{k}: {v}") - data = input("pick one, return to quit: ") - if data == "": - break - try: - key = int(data) - except ValueError: - continue - if key in d: - print("***KAE confirm with tags") - dst = d[key] - process_track(new_file, dst, us_t, us_a) - break - else: + ): + d[i] = match + for k, v in d.items(): + print(f"{k}: {v}") + data = input("pick one, return to quit: ") + if data == "": + no_match.append(f"{fname}, {us_t=}, {us_a=}") + break + try: + key = int(data) + except ValueError: + continue + if key in d: + old_file = os.path.join(parent_dir, d[key]) + oldtags = get_tags(old_file) + old_title = oldtags['title'] + old_artist = oldtags['artist'] + print() + print(f" Title tag will change {old_title} → {us_t}") + print(f" Artist tag will change {old_artist} → {us_a}") + print() + data = input("Go ahead (y to accept)? ") + if data == "y": + process_track(new_file, old_file, us_t, us_a) + break + else: + no_match.append(f"{fname}, {us_t=}, {us_a=}") + continue + no_match.append(f"{fname}, {us_t=}, {us_a=}") continue - continue # from break after testing for "" in data - continue + else: + no_match.append(f"{fname}, {us_t=}, {us_a=}") + continue + if match_count > 1: multiple_similar.append(fname + "\n " + "\n ".join(matches)) if match_count <= 26 and process_multiple_matches: @@ -204,7 +220,10 @@ def process_track(src, dst, title, artist): os.unlink(dst) shutil.move(src, new_path) - track = Tracks.get_by_path(session, new_path) + try: + track = Tracks.get_by_path(session, new_path) + except: + import ipdb; ipdb.set_trace() if track: track.rescan(session) else: diff --git a/poetry.lock b/poetry.lock index 4cd654d..a0c289a 100644 --- a/poetry.lock +++ b/poetry.lock @@ -366,6 +366,14 @@ category = "dev" optional = false python-versions = ">=3.8,<4.0" +[[package]] +name = "pyfzf" +version = "0.3.1" +description = "Python wrapper for junegunn's fuzzyfinder (fzf)" +category = "main" +optional = false +python-versions = "*" + [[package]] name = "pygments" version = "2.11.2" @@ -481,6 +489,14 @@ pytest = ">=3.0.0" dev = ["pre-commit", "tox"] doc = ["sphinx", "sphinx-rtd-theme"] +[[package]] +name = "python-levenshtein" +version = "0.12.2" +description = "Python extension for computing string edit distances and similarities." +category = "main" +optional = false +python-versions = "*" + [[package]] name = "python-slugify" version = "6.1.2" @@ -655,7 +671,7 @@ python-versions = "*" [metadata] lock-version = "1.1" python-versions = "^3.9" -content-hash = "f56b509dd26d76adcd2c01ee04f9e80ed736fb5189b27bcb5e005fc50ba773c9" +content-hash = "b181eb743e8b6c9cb7e03c4db0bcef425fe410d2ec3c4c801ce20e448a26f166" [metadata.files] alembic = [ @@ -928,6 +944,7 @@ pydub = [ {file = "pydub-0.25.1.tar.gz", hash = "sha256:980a33ce9949cab2a569606b65674d748ecbca4f0796887fd6f46173a7b0d30f"}, ] pydub-stubs = [] +pyfzf = [] pygments = [ {file = "Pygments-2.11.2-py3-none-any.whl", hash = "sha256:44238f1b60a76d78fc8ca0528ee429702aae011c265fe6a8dd8b63049ae41c65"}, {file = "Pygments-2.11.2.tar.gz", hash = "sha256:4e426f72023d88d03b2fa258de560726ce890ff3b630f88c21cbb8b2503b8c6a"}, @@ -997,6 +1014,7 @@ pytest-qt = [ {file = "pytest-qt-4.0.2.tar.gz", hash = "sha256:dfc5240dec7eb43b76bcb5f9a87eecae6ef83592af49f3af5f1d5d093acaa93e"}, {file = "pytest_qt-4.0.2-py2.py3-none-any.whl", hash = "sha256:e03847ac02a890ccaac0fde1748855b9dce425aceba62005c6cfced6cf7d5456"}, ] +python-levenshtein = [] python-slugify = [] python-vlc = [ {file = "python-vlc-3.0.16120.tar.gz", hash = "sha256:92f98fee088f72bd6d063b3b3312d0bd29b37e7ad65ddeb3a7303320300c2807"}, diff --git a/pyproject.toml b/pyproject.toml index 525544a..db24b5a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -20,6 +20,8 @@ PyQt5-sip = "^12.9.1" types-psutil = "^5.8.22" python-slugify = "^6.1.2" thefuzz = "^0.19.0" +python-Levenshtein = "^0.12.2" +pyfzf = "^0.3.1" [tool.poetry.dev-dependencies] ipdb = "^0.13.9"