-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapct.py
More file actions
executable file
·203 lines (181 loc) · 5.04 KB
/
apct.py
File metadata and controls
executable file
·203 lines (181 loc) · 5.04 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
import os
import sys
import re
import shutil
import httplib
import pdb
class myDirecory:
Name = ""
Path = ""
lowCaseName = ""
class myFile:
"file support class"
Name = ""
lowCaseName = ""
Path = ""
def CopyTo(self, targetPath, OverWrite):
fileNameDest = os.path.join(targetPath,self.Name)
print "target patch " + targetPath
fileSize = os.stat(self.Path).st_size
print "file size" + str (fileSize)
FreeSpaceInTargetDevice = os.statvfs(targetPath).f_favail
if(FreeSpaceInTargetDevice < fileSize):
print "No free space available"
return;
if os.path.isfile(fileNameDest):
if OverWrite:
os.remove(fileNameDest)
else:
print "File already exist"
return
try:
shutil.copy( self.Path , fileNameDest)
print "copy " + self.Path + " -> " + fileNameDest
except:
print "Error in File IO operation"
return
def MoveTo(self, targetPath, OverWrite):
#print "target patch " + targetPath
#print "self.Name " + self.Name
fileNameDest = os.path.join(targetPath,self.Name)
#print "fileNameDest " + fileNameDest
if os.path.isfile(fileNameDest):
if OverWrite:
os.remove(fileNameDest)
else:
print "File already exist"
return
try:
print "Move {0} -> {1}".format( self.Path, fileNameDest)
shutil.move( self.Path , fileNameDest)
except:
print "Error in File IO operation"
return
#
# get list of dir
#
def listdirs(folder):
"get list of folder in a particular directory"
return [d for d in (os.path.join(folder, d1) for d1 in os.listdir(folder))
if os.path.isdir(d)]
#
# get list of file
#
def listfiles(folder):
"get list of file in a particular directory"
return [d for d in (os.path.join(folder, d1) for d1 in os.listdir(folder))
if os.path.isfile(d)]
#
# splace not alpha caraceter
#
def strClean(str):
"replace all characters not alphabetic or numeric"
c = ""
for index in range (len(str)):
if ( (str[index].isalnum())==False):
c += ' '
else:
c += str[index]
while (c.find(" ") > -1):
c = c.replace (" "," ")
return c
def ScanInputPath(path):
print "Scanning input patch " + path
fileList = list()
regex = re.compile("[sS]?\d?\d[xXeEsS]\d\d")# re 00x11 or 0x11 or 00X11 or 0X11 or S01E01 or s01s02
# scan file
for fileNamePath in listfiles(path):
#fileName = fileNamePath.replace(path,"") #remove base path
fileName = os.path.basename(fileNamePath) #remove base path
match = regex.findall(fileName)
if(len(match) >0):
t = myFile()
t.Name = fileName
t.Path = fileNamePath
#remove all chare befoar s01x02
print "Input file " + fileName
t.lowCaseName = strClean(fileName[0:fileName.index(match[0])]).lower().strip()
fileList.append(t)
return fileList
def ScanOutPath(path):
OutPathList = list()
print "Scanning out path " + path
# Load directory and clean
for dirName in listdirs(path):
if (dirName.find('./.') == -1):
t = myDirecory() # make data container
t.Name = dirName.replace(path," ") # remove base path
t.Path = dirName # complete path
t.lowCaseName = strClean(t.Name.lower()).strip()
OutPathList.append(t)
return OutPathList
def ShowHelp():
print 'APCT - Anti Paranoya Copy Tool'
print 'operand:'
print '\t-i <Input_Path>'
print '\t-o <Output_Path>'
print '\t--help or -h Help'
print ''
print 'example:'
print '\tapct.py -i /home/user/download/ /home/xbmc/TvShows/'
#
# Main Code
#
def main(argv):
outDirecotryList = list()
inListFile = list()
if(len(argv) == 0):
print 'apct: Missing file operand'
ShowHelp()
return
index = 0
while (index < len(argv)):
# scan parameters
increase_step = 1
# input directory
if (argv[index] == '-i'):
if(os.path.isdir(argv[index+1]) == False):
print "Error: " + argv[index+1] + " is not a directory"
return
myDir = os.path.abspath(argv[index+1])
inListFile += ScanInputPath(myDir)
increase_step = 2
# -o parametrers
if (argv[index] == '-o'):
if(os.path.isdir(argv[index+1]) == False):
print "Error: " + argv[index+1] + " is not a directory"
return
myDir = os.path.abspath(argv[index+1])
outDirecotryList += ScanOutPath(myDir)
increase_step = 2
if(argv[index] == '--help'):
ShowHelp()
return
if(argv[index] == '-h'):
ShowHelp()
return
index += increase_step
print "Moving or Coping ##################################"
counter = 0
for file in inListFile:
targerDir = None
for dir in outDirecotryList:
##rc = dir.lowCaseName.find(file.lowCaseName)
##if(rc != -1):
if(dir.lowCaseName == file.lowCaseName):
targerDir = dir
if(targerDir != None):
counter += 1
file.MoveTo(targerDir.Path, True)
if(counter > 0):
# require update via json
params = '{"jsonrpc":"2.0","id":2,"method":"VideoLibrary.Scan"}'
headers = {"Content-type": "application/json","Accept": "text/plain, application/json, text/javascript"}
conn = httplib.HTTPConnection('localhost', 8080)
conn.request("POST", "/jsonrpc?awx", params, headers)
response = conn.getresponse()
data = response.read()
conn.close()
if __name__ == "__main__":
main(sys.argv[1:])