-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_renamer.py
executable file
·199 lines (172 loc) · 7.37 KB
/
file_renamer.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
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
#! /usr/bin/env python
import sys
from datetime import datetime
import os
import shutil
import sqlite3
import subprocess
import fnmatch
import logging
import gomXBMCTools
##########
print('os.cwd:')
print(os.getcwd())
print('file (arg[0]):')
print(sys.argv[0])
# its [0] of the split statement
print('file dir (arg[0]):')
print(os.path.split(sys.argv[0]))
##########
dbfile = "/home/gom/code/python/spider_bro/spiderbro.db"
logdir= "/home/gom/log/spiderbro"
target = "/media/nasGom/video/tv/"
id = sys.argv[1]
name = sys.argv[2]
path = sys.argv[3]
start_time = str(datetime.today()).split(".")[0].replace(" ", "_")
start_day = str(datetime.today()).split(" ")[0]
logger = logging.getLogger('filerenamer')
formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] %(message)s')
handler_stream = logging.StreamHandler()
handler_stream.setFormatter(formatter)
handler_stream.setLevel(logging.CRITICAL)
logger.addHandler(handler_stream)
handler_file = logging.FileHandler('%s/spiderBro_%s.log' % (logdir, start_day))
handler_file.setFormatter(formatter)
logger.addHandler(handler_file)
logger.setLevel(logging.INFO)
logger.info('')
logger.info('File Renamer Started...')
logger.info('Target Dir to write files is: %s' % target)
logger.info('Torrent id is: %s' % sys.argv[1])
logger.info('Torrent name is: %s' % sys.argv[2])
logger.info('Torrent save path is: %s' % sys.argv[3])
#sys.exit()
def is_video_file(filename, extensions=['.avi', '.mkv', '.mp4', '.flv', '.divx', '.mpg', '.mpeg', '.wmv']):
return any(filename.lower().endswith(e) for e in extensions)
def findInPath(prog):
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, prog)
if os.path.exists(exe_file) and os.access(exe_file, os.X_OK):
return exe_file
return False
def unrar(filePath):
unrarprog = "unrar"
if unrarprog:
rardir=os.path.dirname(filePath)
command=[unrarprog,'e',filePath]
logger.info("Unrar: Extracting %s" % filePath)
r = subprocess.Popen(command, cwd=rardir,shell=False).wait()
if r == 0:
logger.info("Unrar: Extracted %s" % filePath)
else:
logger.info("Unrar: Error exctracting %s" % filePath)
else:
logger.info("Unrar: Unable to find unrar executable")
if (not os.path.exists(dbfile)):
logger.info("cannot find db...")
sys.exit()
else:
files_to_copy = []
# spiderbro stores path
conn = sqlite3.connect(dbfile)
cur = conn.cursor()
sname = path
select = "select * from urls_seen where savepath like \"%" + sname + "%\""
logger.info( "Retrieving ep info with query: %s" % (select))
for c in(cur.execute(select)):
file_path = path+"/"+name
file_to_copy = path+"/"+name
logger.info("Filepath: %s" % (file_path))
if(not os.path.isdir(file_path)):
# we just have a single video file, copy
files_to_copy.append(file_path)
else:
# we have a dir and are not sure whats in it, do some file manipulation before copying
# 'Sample' files screw up logic if they are not deleted first
logger.info( "Checking for sample...")
for root, dirs, files in os.walk(file_path):
for dirname in fnmatch.filter(dirs, "*Sample*"):
logger.info( "Sample dir detected, deleting... ", dirname)
shutil.rmtree(os.path.join(root,dirname))
# walk dir, find any zip or rar
logger.info( "Checking for archives...")
for pattern in ("*.zip", "*.rar"):
for root, dirs, files in os.walk(file_path):
for filename in fnmatch.filter(files, pattern):
# extract files
archive_file = ( os.path.join(root, filename))
logger.info( archive_file)
if pattern == "*.rar":
logger.info( "trying to unrar...")
unrar(archive_file)
else:
# TODO: implement unzip
logger.info( "trying to unzip")
logger.info( "Checking for video files...")
# walk dir, find any video files
for root, dirs, files in os.walk(file_path):
for vfilename in filter(is_video_file, files):
logger.info( "Found file: %s" % (vfilename))
files_to_copy.append( os.path.join(root, vfilename))
# use creation logic from backup_tv.py to mv and rename episode
s = "0" + str(c[1]) if(int(c[1])<10) else str(c[1])
season_dir = "season_" + s
series_name = gomXBMCTools.normaliseTVShowName(str(c[0]))
if not os.path.isdir(target + series_name):
logger.info( "\tDirectory: %s does not exist, creating..." % (series_name))
os.mkdir(target+series_name)
if not os.path.isdir(target + series_name + "/" + season_dir):
logger.info( "\tDirectory: %s does not exist, creating..." % (season_dir))
os.mkdir(target + series_name + "/" + season_dir)
rmdir = True
for f in files_to_copy:
e = "e0" + str(c[2]) if(int(c[2])<10) else "e" + str(c[2])
# TODO: this is broken
if( c[2] == -1):
e = gomXBMCTools.getEpisodeNumFromFilename(f, s)
if( e != "e-1" ):
fileName, fileExtension = os.path.splitext(f)
ftarget = target + series_name + "/" + season_dir + "/" + series_name + "_s"+ s + e + fileExtension
logger.info( "---> Copying File: %s to %s" % (f, ftarget))
shutil.copy2(f, ftarget)
else:
logger.info("Copying file without renaming: %s" % (f))
ftarget = target + series_name + "/" + season_dir + "/"
shutil.copy2(f, ftarget)
#rmdir = False
if rmdir:
logger.info("Cleaning up temporary dir %s" % (path))
shutil.rmtree(path)
from deluge.ui.client import client
from twisted.internet import reactor, defer
from deluge.log import setupLogger
setupLogger()
def printSuccess(dresult, is_success, smsg):
print "[+]", smsg
def printError(emsg):
print "[e]", emsg
def printReport(rresult):
print rresult
def dl_finish(result):
print "All deferred calls have fired, exiting program..."
client.disconnect()
# Stop the twisted main loop and exit
reactor.stop()
def on_connect_fail(result):
print "Connection failed!"
print "result:", result
def on_connect_success(result):
print "Connection was successful!"
torrent_id = id
tlist = []
successmsg = " Removed"
errormsg = "Error removing"
do_remove_data = False
tlist.append(client.core.remove_torrent(torrent_id, do_remove_data).addCallbacks(printSuccess, printError, callbackArgs = (True, successmsg), errbackArgs = (errormsg)))
defer.DeferredList(tlist).addCallback(printReport)
defer.DeferredList(tlist).addCallback(dl_finish)
d = client.connect()
d.addCallback(on_connect_success)
d.addErrback(on_connect_fail)
reactor.run()