-
Notifications
You must be signed in to change notification settings - Fork 1
/
renamer.py
79 lines (65 loc) · 2.66 KB
/
renamer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import os
import re
import mutagen
from mutagen.mp3 import MP3
from mutagen.flac import FLAC
from variables import green
class RENAMER:
def __init__(self, gui):
self.gui = gui
def _lowercase_match(self, match):
"""Lowercase the whole regular expression match group."""
return match.group().lower()
def _title(self, value):
titled = value.title()
titled = re.sub(r"([a-z])'([A-Z])", self._lowercase_match, titled) # Fix Don'T
titled = re.sub(r"\d([A-Z])", self._lowercase_match, titled) # Fix 1St and 2Nd
return titled
def _prettify(self, str):
str = str.strip()
str = self._title(str)
return str
def _rename(self, path, current_name, new_name):
current_file = path + "\\" + current_name
new_file = path + "\\" + new_name
if current_name != new_name:
self.gui.set_output_value(f"Renaming '{current_name}' to '{new_name}'...\n", green)
os.rename(current_file, new_file)
def _handle_mp3(self, path, track, file):
# Artist
if ("TPE1" in track.keys()):
artist = str(track.get("TPE1"))
artist = self._prettify(artist)
# Track Title
if ("TIT2" in track.keys()):
title = str(track.get("TIT2"))
title = self._prettify(title)
if artist and title:
new_name = artist + " - " + title + ".mp3"
self._rename(path, file, new_name)
def _handle_flac(self, path, track, file):
# Artist
if ("artist" in track.keys()):
artist = str(track.get("artist")[0])
artist = self._prettify(artist)
# Track Title
if ("title" in track.keys()):
title = str(track.get("title")[0])
title = self._prettify(title)
if artist and title:
new_name = artist + " - " + title + ".flac"
self._rename(path, file, new_name)
def main(self, file_meta):
for meta in file_meta:
file_name = meta["file_name"]
file_type = meta["file_type"]
file_path = meta["file_path"]
folder_path = meta["folder_path"]
if file_type == mutagen.mp3.MP3:
track = MP3(file_path)
self._handle_mp3(folder_path, track, file_name)
elif file_type == mutagen.flac.FLAC:
track = FLAC(file_path)
self._handle_flac(folder_path, track, file_name)
else:
self.gui.set_output_value(f"File type {file_type} is not supported yet...\n", green)