music-organizer/main.py

41 lines
1.6 KiB
Python
Raw Normal View History

2023-05-07 17:38:03 +00:00
import os, eyed3, re, shutil, sys, getopt
2023-05-07 01:43:20 +00:00
2023-05-07 17:38:03 +00:00
def organizeFiles(path, dest):
with os.scandir(path) as dir_entries:
2023-05-07 01:43:20 +00:00
for entry in dir_entries:
if bool(re.search(r'.*\.mp3', entry.name)) is True:
file = eyed3.load(entry)
artist = file.tag.artist
artist = re.sub(r'(\/)|(\\)|(\:)|(\*)|(\?)|(\")|(\<)|(\>)|(\|)|(\ )$', ' ', artist)
album = file.tag.album
album = re.sub(r'(\/)|(\\)|(\:)|(\*)|(\?)|(\")|(\<)|(\>)|(\|)|(\ )$', ' ', album)
artistIsExist = os.path.exists(f'{dest}\\{artist}')
if not artistIsExist:
os.makedirs(f'{dest}\\{artist}')
albumIsExist = os.path.exists(f'{dest}\\{artist}\\{album}')
if not albumIsExist:
os.makedirs(f'{dest}\\{artist}\\{album}')
song = entry.name
song = re.sub(r'(\/)|(\\)|(\:)|(\*)|(\?)|(\")|(\<)|(\>)|(\-)|(\|)|(\ )$', '', song)
song = re.sub(r'\ +', ' ', song)
try:
2023-05-07 17:38:03 +00:00
shutil.move(entry.path, str(f'{dest}\\{artist}\\{album}\{song}'))
2023-05-07 01:43:20 +00:00
except:
print(song)
2023-05-07 17:38:03 +00:00
def main(argv):
if len(argv) == 0:
print('Usage: main.py -s SOURCE_DIR -d DEST_DIR')
else:
opts, args = getopt.getopt(argv, "s:d:")
for opt, arg in opts:
if opt == '-s':
path = arg
elif opt == '-d':
dest = arg
try:
organizeFiles(path, dest)
except:
print('Arguments not entered correctly')
if __name__ == "__main__":
main(sys.argv[1:])