Skip to content
Snippets Groups Projects
Commit 6902898d authored by Anton Sarukhanov's avatar Anton Sarukhanov
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
Pipeline #116 passed with stage
in 37 seconds
.mypy_cache
image: python:3-buster
before_script:
- pip install -r requirements-dev.txt
test:
script:
- bandit *.py
- flake8 *.py
- mypy *.py
- pydocstyle *.py
- pylint *.py
# Karaoke Cleaner
Removes redundant files from a karaoke collection.
A collection may include multiple versions of the same song, from multiple
karaoke studios/labels. This script finds such songs, and moves the less-desired
copies into a different directory.
#!/bin/env python3
"""Karaoke collection sorting tool."""
from collections import defaultdict
import os
import re
import sys
KARAOKE_DIR = "/media/anton/Karaoke/Karaoke"
EXTRA_DIR = "/media/anton/Karaoke/Karaoke_Extra"
FILENAME_REGEX = re.compile(r"(?P<song>.*)\[(?P<label>.*)\](?P<ext>.*)")
PREFERENCES = [
"Karaoke",
"SC Karaoke",
"ME Karaoke",
"CB Karaoke",
"MH Karaoke",
"HK Karaoke",
"SN Karaoke",
"L Karaoke",
"Spanish Karaoke",
"SF Karaoke",
"MM Karaoke",
"EK Karaoke",
"AS Karaoke",
"KV Karaoke",
"PS Karaoke",
"VS Karaoke",
"Z Karaoke",
"DC Karaoke",
]
def karaoke_cleanup():
"""Move redundant karaoke tracks into a separate directory."""
# Scan all songs
all_songs = defaultdict(list)
for filename in os.listdir(KARAOKE_DIR):
match = FILENAME_REGEX.search(filename)
if match:
all_songs[match.group("song")].append(filename)
songs_total = len(all_songs)
# Remove non-preferred labels whenever preferred exists.a
songs_done = 0
total_removed = 0
for files in all_songs.values():
dupes_removed = 0
for preferred_label in PREFERENCES:
preferred_string = "[{}]".format(preferred_label)
if any([preferred_string in filename for filename in files]):
# Found it; remove all others.
for filename in files:
if preferred_string not in filename:
_remove_duplicate(filename)
dupes_removed += 1
total_removed += 1
break
if (len(files) - dupes_removed) > 2:
_print_need_label_preference(files)
_print_progress(songs_done, songs_total, dupes_removed)
songs_done += 1
return (songs_done, songs_total, total_removed)
def _remove_duplicate(filename):
old = os.path.join(KARAOKE_DIR, filename)
new = os.path.join(EXTRA_DIR, filename)
os.rename(old, new)
def _print_progress(count_done, count_total, dupes):
percent = count_done / count_total * 100
sys.stdout.write("\033[K{:2.2f}% done. Removed {} duplicates."
.format(percent, dupes))
def _print_need_label_preference(files):
labels = []
filename = None
for filename in files:
match = FILENAME_REGEX.search(filename)
if match:
labels.append(match.group("label"))
else:
labels.append("")
labels = "\n".join(set(labels))
sys.stdout.write("\033[K")
print("Need label choice. Song: {}\n{}".format(filename, labels))
sys.exit()
def count_songs_by_label():
"""Return a list of Karaoke labels, sorted by number of tracks."""
all_labels = defaultdict(int)
for filename in os.listdir(KARAOKE_DIR):
match = FILENAME_REGEX.search(filename)
song_label = match.group("label") if match else ""
all_labels[song_label] += 1
return sorted(all_labels.items(), key=lambda x: x[1], reverse=True)
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "getlabels":
for (label, count) in count_songs_by_label():
print(label, count)
else:
print("Done. Processed {} songs, removed {} extras."
.format(*karaoke_cleanup()[1:2]))
bandit==1.6.2
flake8==3.7.9
mypy==0.761
mypy-extensions==0.4.3
pydocstyle==5.0.1
pylint==2.4.4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment