forked from mdhiggins/sickbeard_mp4_automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelugePostProcess.py
executable file
·157 lines (135 loc) · 5.32 KB
/
delugePostProcess.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
#!/usr/bin/env python
import os
import sys
from log import getLogger
from autoprocess import autoProcessTV, autoProcessMovie, autoProcessTVSR, sonarr, radarr
from readSettings import ReadSettings
from mkvtomp4 import MkvtoMp4
from deluge_client import DelugeRPCClient
import logging
import shutil
from logging.config import fileConfig
log = getLogger("DelugePostProcess")
log.info("Deluge post processing started.")
settings = ReadSettings()
categories = [settings.deluge['sb'], settings.deluge['cp'], settings.deluge['sonarr'], settings.deluge['radarr'], settings.deluge['sr'], settings.deluge['bypass']]
remove = settings.deluge['remove']
if len(sys.argv) < 4:
log.error("Not enough command line parameters present, are you launching this from deluge?")
sys.exit()
path = str(sys.argv[3])
torrent_name = str(sys.argv[2])
torrent_id = str(sys.argv[1])
delete_dir = None
log.debug("Path: %s." % path)
log.debug("Torrent: %s." % torrent_name)
log.debug("Hash: %s." % torrent_id)
client = DelugeRPCClient(host=settings.deluge['host'], port=int(settings.deluge['port']), username=settings.deluge['user'], password=settings.deluge['pass'])
client.connect()
if client.connected:
log.info("Successfully connected to Deluge")
else:
log.error("Failed to connect to Deluge")
sys.exit()
torrent_data = client.call('core.get_torrent_status', torrent_id, ['files', 'label'])
try:
torrent_files = torrent_data[b'files']
category = torrent_data[b'label'].lower().decode()
except:
torrent_files = torrent_data['files']
category = torrent_data['label'].lower()
files = []
log.debug("List of files in torrent:")
for contents in torrent_files:
try:
files.append(contents[b'path'].decode())
log.debug(contents[b'path'].decode())
except:
files.append(contents['path'])
log.debug(contents['path'])
if category not in categories:
log.error("No valid category detected.")
sys.exit()
if len(categories) != len(set(categories)):
log.error("Duplicate category detected. Category names must be unique.")
sys.exit()
try:
if settings.deluge['convert']:
# Check for custom Deluge output_dir
if settings.deluge['output_dir']:
settings.output_dir = settings.deluge['output_dir']
log.debug("Overriding output_dir to %s." % settings.deluge['output_dir'])
# Perform conversion.
settings.delete = False
if not settings.output_dir:
suffix = "convert"
settings.output_dir = os.path.join(path, ("%s-%s" % (torrent_name, suffix)))
else:
settings.output_dir = os.path.join(settings.output_dir, torrent_name)
if not os.path.exists(settings.output_dir):
try:
os.makedirs(settings.output_dir)
delete_dir = settings.output_dir
except:
log.exception("Unable to make output directory %s." % settings.output_dir)
converter = MkvtoMp4(settings)
if len(files) < 1:
log.error("No files provided by torrent")
for filename in files:
inputfile = os.path.join(path, filename)
info = converter.isValidSource(inputfile)
if info:
log.info("Converting file %s at location %s." % (inputfile, settings.output_dir))
try:
output = converter.process(inputfile, info=info)
except:
log.exception("Error converting file %s." % inputfile)
path = settings.output_dir
else:
suffix = "copy"
newpath = os.path.join(path, ("%s-%s" % (torrent_name, suffix)))
if not os.path.exists(newpath):
try:
os.mkdir(newpath)
except:
log.exception("Unable to make copy directory %s." % newpath)
for filename in files:
inputfile = os.path.join(path, filename)
log.info("Copying file %s to %s." % (inputfile, newpath))
shutil.copy(inputfile, newpath)
path = newpath
delete_dir = newpath
except:
log.exception("Error occurred handling file")
# Send to Sickbeard
if (category == categories[0]):
log.info("Passing %s directory to Sickbeard." % path)
autoProcessTV.processEpisode(path, settings)
# Send to CouchPotato
elif (category == categories[1]):
log.info("Passing %s directory to Couch Potato." % path)
autoProcessMovie.process(path, settings, torrent_name)
# Send to Sonarr
elif (category == categories[2]):
log.info("Passing %s directory to Sonarr." % path)
sonarr.processEpisode(path, settings)
elif (category == categories[3]):
log.info("Passing %s directory to Radarr." % path)
radarr.processMovie(path, settings)
elif (category == categories[4]):
log.info("Passing %s directory to Sickrage." % path)
autoProcessTVSR.processEpisode(path, settings)
elif (category == categories[5]):
log.info("Bypassing any further processing as per category.")
if delete_dir:
if os.path.exists(delete_dir):
try:
os.rmdir(delete_dir)
log.debug("Successfully removed tempoary directory %s." % delete_dir)
except:
log.exception("Unable to delete temporary directory.")
if remove:
try:
client.call('core.remove_torrent', torrent_id, True)
except:
log.exception("Unable to remove torrent from deluge.")