-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-sort.py
165 lines (138 loc) · 5.31 KB
/
auto-sort.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
###################################
# Author: Stautyr #
# Date: 1-5-2021 #
# Language: Python 3.6.4 #
# Program: Auto-sort(v. 2.1) #
###################################
#Setup
from mutagen.asf import ASF #
from mutagen.easyid3 import EasyID3 # if an error occurs, you may need to pip install mutagen, shutil, and os
from mutagen.mp4 import MP4 #
import os
import shutil
import send2trash
from pathlib import Path
DEBUG = False
if (DEBUG):
print("IN DEBUGGING MODE")
#User of system
user = "[INSERT USER HERE]"
#Enable if OneDrive is active
OneDrive = True
else:
OneDrive = True
pathList = str(Path.cwd()).split("\\")
user = pathList[2]
# if (pathList[3] == "OneDrive"):
# OneDrive = True
#OneDrive check
startPath = Path("/Users/"+user)
if(OneDrive == False):
DocPath = startPath/"Documents"
else:
DocPath = startPath/"OneDrive/Documents"
#Path shortcuts
docSort = DocPath/"Download Sort"
downloads = startPath/"downloads"
musicSort = startPath/"Music"
picSort = startPath/"Pictures/Camera Roll"
videoSort = startPath/"Videos"
#folder assignment
folders = {
"doc" : docSort/"DOC FILES",
"exe" : docSort/"EXE FILES",
"xlsx": docSort/"EXCEL FILES",
"jpg" : picSort/"JPG",
"mov" : videoSort/"Captures",
"mp4" : musicSort/"MP4",
"other" : docSort/"OTHER",
"pdf" : docSort/"PDFs",
"png" : picSort/"PNG",
"pptx" : docSort/"PPTX FILES",
"txt" : docSort/"TXT FILES",
"wav" : musicSort/"WAV",
"zip" : docSort/"ZIP FILES"}
#Checks for folders and creates them if they do not exist
def check():
for count in folders:
if Path(folders[count]).is_dir():
if(DEBUG):
print ("{} exist".format(folders[count]))
pass
else:
if(DEBUG):
print ("{} does not exist".format(folders[count]))
print ("Creating {}".format(folders[count]))
os.mkdir(folders[count])
def songSort(song, ext):
if(ext == "mp3"):
metadata = EasyID3(song)
album = str(metadata['album'])
elif(ext == "m4a"):
metadata = MP4(song)
album = str(metadata["\xa9alb"])
elif(ext == "wma"):
metadata = ASF(song)
album = str(metadata["WM/AlbumTitle"])
album = album[22:-3]
else:
album = album[2:-2]
albumList = os.listdir(musicSort/"Albums")
#album folder check
if (album not in albumList):
if(DEBUG):
print(song)
print(album + " not found")
os.mkdir(musicSort/"Albums"/album)
return(musicSort/"Albums"/album)
#Sorting functions
def send(file, ext):
shortExt = str(ext[1:])
try:
if(shortExt == "docx"):
folder = folders["doc"]
elif(shortExt == "mp3" or shortExt == "m4a" or shortExt == "wma"):
folder = songSort(startFolder/file, shortExt)
else:
folder = folders[shortExt]
except:
if(shortExt == "mp3" or shortExt == "m4a" or shortExt == "wma"):
folder = songSort(startFolder/file, shortExt)
else:
print ("Other file type found: " + shortExt)
folder = folders["other"]
if (DEBUG):
print("Sending {} to {}".format(file,folder))
try:
shutil.move(startFolder/file, folder/file)
except:
pass
else:
if (DEBUG):
print("Sending {} to {}".format(file,folder))
shutil.move(startFolder/file, folder/file)
def delEmpty(startFolder):
for folderName, subfolders, filenames in os.walk(startFolder):
folderSize = os.path.getsize(folderName)
if (folderSize == 0):
if (DEBUG):
print(folderName + " is empty \n deleting " + folderName)
send2trash.send2trash(folderName)
###########################################################################################
# MAIN PROGRAM #
###########################################################################################
#Checks for valid folders
check()
#starting point
startFolder = downloads
#Sorting method
o = os.listdir(startFolder) #creates list of all files in starting folder
for x in range(len(o)):
file = o[x]
ext = os.path.splitext(file)[-1].lower() #isolates file extension
if(DEBUG):
print(ext)
try:
send(file, ext) #sorts file
except PermissionError:
pass