-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.py
executable file
·137 lines (125 loc) · 4.3 KB
/
manager.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
import os
import glob
import shutil
import guessit
from logger import Logger
from pprint import pprint
try:
from pwd import getpwnam
except:
getpwnam = lambda x: (0,0,0)
os.chown = lambda x, y, z: True
os.chmod = lambda x, y: True
os.fchown = os.chown
os.fchmod = os.chmod
import argparse
class Manager():
"""docstring for Manager"""
def __init__(self, downloadPath, moviePath, showPath, extentions):
p = argparse.ArgumentParser(description='Process downloaded files')
p.add_argument('-v', '--verbose', action='store_true', default=False);
args = p.parse_args()
self.downloadPath = downloadPath
self.nonProcessedPath = os.path.join(self.downloadPath, 'Non-Processed-Files');
self.moviePath = moviePath
self.showPath = showPath
self.extentions = extentions
self.subtitle = False
self.l = Logger('Manager', self.downloadPath, args).logger
self.l.info("started")
def manage(self):
self.l.info('managing files in %s' % self.downloadPath)
length = len(self.downloadPath)
for extention in self.extentions:
for _file in glob.glob( os.path.join(self.downloadPath, extention) ):
self.fullPath = _file
self.fileName = _file[(length+1):]
for nonsense in ['DDLValley.eu_', 'ReleaseThread_', 'RlsThread_', 'RelT_']:
if self.fileName.startswith(nonsense):
oldPath = os.getcwd()
os.chdir(self.downloadPath)
newName = self.fileName[len(nonsense):]
os.rename(self.fileName, newName)
self.fileName = newName
if os.path.isfile(newName):
os.chdir(oldPath)
else:
os.chdir(oldPath)
self.l.error("Something went wrong while trying to rename %s" % self.fileName)
self.l.info('file name is: %s' % self.fileName);
self.move()
self.l.info("done")
def move(self):
info = self.getInfo(self.fileName)
path = self.getMoveToPath(info)
if path:
if self.subtitle:
self.fileName = self.renameFile()
if not self.fileName:
return False
self.makeDest(path)
self.l.info("moving %s to %s" % (self.fileName, path))
completePath = os.path.join(path, self.fileName)
self.fileName = os.path.join(self.downloadPath, self.fileName)
if not os.path.isfile(completePath):
shutil.move(self.fileName, path)
else:
self.l.info("File Already exsists")
self.makeDest(self.nonProcessedPath)
self.l.info('moving %s to %s' % (self.fileName, self.nonProcessedPath))
shutil.move(self.fileName, self.nonProcessedPath);
else:
self.l.error("No path to move %s to" % self.fileName)
def getInfo(self, media):
return guessit.guess_file_info(media, 'autodetect')
def getMoveToPath(self, fileInfo):
if fileInfo['type'] == 'episode':
self.l.info("It's an episode")
return os.path.join(self.showPath, self.getPathStringEpisode(fileInfo))
elif fileInfo['type'] == 'movie':
self.l.info("I'ts a movie")
return os.path.join(self.moviePath, self.getPathStringMovie(fileInfo))
elif fileInfo['type'] == 'episodesubtitle':
self.l.info("It's a subtitle for a show")
self.subtitle = True
return os.path.join(self.showPath, self.getPathStringEpisode(fileInfo))
elif fileInfo['type'] == 'moviesubtitle':
self.l.info("I'ts a subtitle for a movie")
self.subtitle = True
return os.path.join(self.moviePath, self.getPathStringMovie(fileInfo))
else:
return False
def parseNumber(self, number):
if number < 10:
number = '0'+str(number)
return str(number)
def makeDest(self, path):
if not os.path.exists(path):
os.makedirs(path, 0777)
def getPathStringEpisode(self, fileInfo):
show = fileInfo['series']
e = self.parseNumber(fileInfo['episodeNumber'])
s = fileInfo['season']
season = "Season "+str(s)
if 'year' in fileInfo:
show += " "+ str(fileInfo['year'])
s = self.parseNumber(s)
return os.path.join(show, os.path.join(season, "S"+s+"E"+e))
def getPathStringMovie(self, fileInfo):
title = fileInfo['title']
if 'year' in fileInfo:
title += " "+str(fileInfo['year'])
return title
def renameFile(self):
oldPath = os.getcwd()
os.chdir(self.downloadPath)
fileNameNoEx, ex = os.path.splitext(self.fileName)
newName = fileNameNoEx+".nl"+ex
os.rename(self.fileName, newName)
if os.path.isfile(newName):
os.chdir(oldPath)
return newName
else:
os.chdir(oldPath)
self.l.error("Something went wrong while trying to rename %s" % self.fileName)
return False