-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
113 lines (82 loc) · 3.33 KB
/
main.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import eyed3
import logging
import os
import shutil
# Stupid errors need stupid comments
logging.getLogger("eyed3.mp3.headers").setLevel(logging.CRITICAL)
# Set path
path = 'media'
counter = 0
# List directory
files = os.listdir(path)
# Illegal characters array
sp_chars = [';', ':', '!', "*", "/", "|", '"', "<", ">", "&", "?"]
# Cleanup function
# cleanup.cleanup_directory()
class MP3Track:
def __init__(self,
mp3_artist,
mp3_album,
mp3_title):
# Input validation for artist
if mp3_artist == ' ' or mp3_artist == '' or mp3_artist is None:
mp3_artist = "Unknown Artist"
for i in sp_chars:
mp3_artist = mp3_artist.replace(i, "")
mp3_artist = mp3_artist.strip()
# Input validation for album
if mp3_album == ' ' or mp3_album == '' or mp3_album is None:
mp3_album = "Unknown Album"
for i in sp_chars:
mp3_album = mp3_album.replace(i, "")
# Input validation for title
for i in sp_chars:
mp3_title = mp3_title.replace(i, "")
# Setters for object
self.artist = mp3_artist
self.album = mp3_album
self.title = mp3_title
def __str__(self):
return f"\n" \
f"Artist: {self.artist}\n" \
f"Album: {self.album}\n" \
f"Title: {self.title}\n"
def create_artist_directory():
if not os.path.isdir(os.path.join(root, new_mp3_track.artist)):
os.makedirs(os.path.join(root, new_mp3_track.artist))
print(f"Directory '{new_mp3_track.artist}' didn't exist, making it now.")
def create_album_directory():
if not os.path.isdir(os.path.join(root, new_mp3_track.artist, new_mp3_track.album).replace("\\", "/")):
os.makedirs(os.path.join(root, new_mp3_track.artist, new_mp3_track.album).replace("\\", "/"))
return f"Directory '{new_mp3_track.artist} / {new_mp3_track.album}' didn't exist, making it now."
# OS Walk through path
for root, directories, files in os.walk(path, topdown=False):
# Loop through files
for name in files:
if name.endswith('mp3'):
# Load MP3 file
audio_file = eyed3.load(os.path.join(root, name))
# Increment counter
counter += 1
# Set Variables
new_mp3_track = MP3Track(audio_file.tag.artist, audio_file.tag.album, audio_file.tag.title)
print(f"Loaded {new_mp3_track}")
# Set files and directories
original_path = os.path.join(root, name)
destination_path = os.path.join(root, new_mp3_track.artist, new_mp3_track.album,
new_mp3_track.title) + ".mp3"
# Get Size
size = str(os.path.getsize(original_path))
# Create album directory if it doesn't exist
create_artist_directory()
# Create artist directory if it doesn't exist
create_album_directory()
# Move file
shutil.move(original_path, destination_path)
# Print out
print(f"Moved {original_path} to {destination_path} size: {size}")
else:
print("Error: wrong file format")
print(f"Total songs moved: {counter}")
def __main__():
return "Hello world"