-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtorrentsort.py
172 lines (161 loc) · 6.93 KB
/
torrentsort.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
166
167
168
169
170
171
172
#!/usr/bin/python3.5
import os, glob, re, shutil, difflib, subprocess, sys
# change these paths if you want
TVSeriespath = "/media/Tough/TVSeries/"
MOVIESpath = "/media/Tough/MOVIES/"
UnsortedSeriespath = "/media/Tough/UnsortedSeries/"
Unsortedpath = "/media/Tough/Unsorted/"
Trashpath = "/media/Tough/Trash/"
logfile = open("torrentsort.log", "a+")
# create a list with "sibling directories"
siblingdirectories = []
for dir in os.listdir(TVSeriespath):
if os.path.isdir(os.path.join("../TVSeries/",dir)):
siblingdirectories.append(dir)
# file extensions of interest
fileext = (".mp4",".avi",".srt",".mkv","m4v")
#file extensions that are going to be deleted
fileextdel = (".jpg",".nfo",".txt",".png")
# simple method to show what's being moved
def printmovemessage(filename, ftype, foldername):
logmessage = "Moving\t" + ftype + "\t" + filename + "\tto\t" + foldername + "\n"
print(logmessage)
logfile.write(logmessage)
# method to determine if a file/folder is a series file/folder
def isitseries(filename):
# search the filename for substring like S**E** (e.g. S01E01 - meaning Series01Episode01)
regexmatch = re.search(r'[sS][0-9]{1,2}[eE][0-9]{1,2}.{1,}',filename)
# remove the S**E** from the filename for better matching using the difflib library
filenameminusseries = re.sub(r'[sS][0-9]{1,2}[eE][0-9]{1,2}.{1,}','',filename)
best_match = difflib.get_close_matches(filenameminusseries,siblingdirectories,1,0.6)
print ('Best match is directory: ',best_match)
for sdir in siblingdirectories:
if best_match and regexmatch:
itseries = 'yes'
matched_dir = best_match[0]
elif not best_match and regexmatch:
itseries = 'regexmatched'
matched_dir = 'UnsortedSeries'
elif best_match and not regexmatch:
itseries = 'diffmatched'
matched_dir = 'Unsorted'
elif not best_match and not regexmatch:
itseries = 'no'
matched_dir = 'MOVIES'
else:
itseries = 'no'
matched_dir = 'MOVIES'
return itseries, matched_dir
# method to determine if a file is a sample file
def isitsample(filename):
samplematch = re.search(r'[sS][aA][mM][pP][lL][eE].{1,}',filename)
if samplematch:
itssample = 'yes'
else:
itssample = 'no'
return itssample
# method to determine what to do with a file of interest
def whatdowithfile(filename):
slashes = filename.count('/')
if slashes == 1:
filename1, filenameE = filename.split('/')
elif slashes == 2:
filename1, filename2, filenameE = filename.split('/')
elif slashes == 3:
filename1, filename2, filename3, filenameE = filename.split('/')
elif slashes == 4:
filename1, filename2, filename3, filename4, filenameE = filename.split('/')
else:
filenameE = filename
print ('this file has too many slashes, it won\'t match well')
# remove weird characters and strings that we do not want for better matching
removechars = ['.','_','WEB-DL','-','[',']',' ','720p','1080p','(',')','eason','pisode','x264','AC3','XviDVD','x265','BRRip','HDRip','WEBRip','DVD','Complete','BluRay','ReEnc','DeeJayAhmed','ShAaNiG','Subs','Torrent','.com','webrip','WEBRIP','YTS']
for ch in removechars:
filenameE = filenameE.replace(ch,'')
print ('The string has been cleaned to: ',filenameE)
# call the isitseries function
(seriesbool, seriesdir) = isitseries(filenameE)
# is this file a series file?
if seriesbool == 'yes':
dest = TVSeriespath+seriesdir
printmovemessage(filenameE, "SERIES FILE", seriesdir)
shutil.move(filename, dest)
# is this file a series file but no sibling directory exists?
elif seriesbool == 'regexmatched':
dest = UnsortedSeriespath
printmovemessage(filenameE, "ORPHAN SERIES FILE", "UnsortedSeries")
shutil.move(filename, dest)
# is this file a series file (probably)?
elif seriesbool == 'diffmatched':
dest = Unsortedpath
printmovemessage(filenameE, "PROBABLY UNSORTED SERIES FILE", "Unsorted")
shutil.move(filename, dest)
# if all above fail then we have a movie file
else:
dest = MOVIESpath
printmovemessage(filenameE, "MOVIES FILE", "MOVIES")
shutil.move(filename, dest)
# search this directory for files
for downloadedfile in glob.glob('./**/*', recursive=True):
print ('\n\n************************************************************')
print('Deciding what to do with: ',downloadedfile,'\n')
## start decision making here ##
# 1] skip all .py files and .log files
if downloadedfile.endswith(".py"):
print ("Skipped PYTHON file")
elif downloadedfile.endswith(".log"):
print ("Skipped LOG file")
# 2] delete "sample" clips and directories
elif isitsample(downloadedfile) == 'yes':
if os.path.isfile(downloadedfile):
printmovemessage(downloadedfile, "SAMPLE FILE", "null")
os.remove(downloadedfile)
elif os.path.isdir(downloadedfile):
printmovemessage(downloadedfile, "SAMPLE DIRECTORY", "null")
shutil.rmtree(downloadedfile)
# 3] delete files with an extension in fileextdel
elif downloadedfile.endswith(tuple(fileextdel)) and os.path.isfile(downloadedfile):
destt = Trashpath
printmovemessage(downloadedfile, "UNWANTED EXTENSION", "null")
os.remove(downloadedfile)
#os.remove(downloadedfile)
# 4] examine files with extension of interest
elif downloadedfile.endswith(tuple(fileext)) and os.path.isfile(downloadedfile):
####### CUT THE DOWNLOADEDFILE STRING AND KEEP THE LAST PART AFTER THE LAST SLASH########### (ALSO DO IT FURTHER DOWN IF NEEDED) ##### REASON: CHECK IF IT FIXES THE MATCHING FOR SERIES FILES
whatdowithfile(downloadedfile)
# 5] delete empty folders
elif os.path.isdir(downloadedfile) and not os.listdir(downloadedfile):
printmovemessage(downloadedfile, "EMPTY FOLDER", "null")
os.rmdir(downloadedfile)
# 6] examine remaining directories
elif os.path.isdir(downloadedfile) and os.listdir(downloadedfile):
for subfile in os.listdir(downloadedfile):
# are the subfiles empty directories?
if os.path.isdir(subfile) and not os.listdir(subfile):
printmovemessage(subfile, "EMPTY FOLDER", "null")
os.rmdir(subfile)
# are the subfiles non empty directories?
elif os.path.isdir(subfile) and os.listdir(subfile):
for subsubfile in os.listdir(subfile):
# are the sub-sub files files?
if os.path.isfile(subsubfile):
whatdowithfile(subsubfile)
# are the sub-sub files empty directories?
elif os.path.isdir(subfile) and not os.listdir(subfile):
printmovemessage(subfile, "EMPTY FOLDER", "null")
os.rmdir(subfile)
# are the sub-sub files non empty directories?
elif os.path.isdir(subfile) and os.listdir(subfile):
printmovemessage(downloadedfile, "NON EMPTY DIRECTORY", "null")
shutil.rmtree(downloadedfile)
# 7] move the remaining files to the Trash directories
elif os.path.isfile(downloadedfile):
destt = Trashpath
printmovemessage(downloadedfile, "REMAINING FILES", "Trash")
shutil.move(downloadedfile, destt)
#os.remove(downloadedfile)
print('************************************************************')
# run the script again for deep clean
repeat = input("Would you like to run again? (y/n) ")
if repeat == 'y':
os.execv(sys.executable, ['python3.5'] + sys.argv)