forked from mdhiggins/sickbeard_mp4_automator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_processor.py
64 lines (54 loc) · 2.5 KB
/
post_processor.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
import os
import logging
import json
from subprocess import Popen, PIPE
from extensions import bad_post_files, bad_post_extensions
class PostProcessor:
def __init__(self, files, logger=None):
# Setup Logging
if logger:
self.log = logger
else:
self.log = logging.getLogger(__name__)
self.log.debug("Output: %s." % files)
self.set_script_environment(files)
self.scripts = self.gather_scripts()
def set_script_environment(self, files):
self.log.debug("Setting script environment.")
self.post_process_environment = os.environ.copy()
self.post_process_environment['MH_FILES'] = json.dumps(files)
def gather_scripts(self):
self.log.debug("Gathering scripts.")
current_directory = os.path.dirname(os.path.realpath(__file__))
post_process_directory = os.path.join(current_directory, 'post_process')
scripts = []
for script in os.listdir(post_process_directory):
if os.path.splitext(script)[1] in bad_post_extensions or os.path.isdir(script) or script in bad_post_files:
self.log.debug("Skipping %s." % script)
continue
else:
self.log.debug("Script added: %s." % script)
scripts.append(os.path.join(post_process_directory, script))
return scripts
def setTV(self, tvdbid, season, episode):
self.log.debug("Setting TV metadata.")
self.post_process_environment['MH_TVDBID'] = str(tvdbid)
self.post_process_environment['MH_SEASON'] = str(season)
self.post_process_environment['MH_EPISODE'] = str(episode)
def setMovie(self, imdbid):
self.log.debug("Setting movie metadata.")
self.post_process_environment['MH_IMDBID'] = str(imdbid)
def run_scripts(self):
self.log.debug("Running scripts.")
for script in self.scripts:
try:
command = self.run_script_command(script)
self.log.info("Running script '%s'." % (script))
stdout, stderr = command.communicate()
self.log.debug("Stdout: %s." % stdout)
self.log.debug("Stderr: %s." % stderr)
except Exception as e:
self.log.exception("Failed to execute script %s." % script)
def run_script_command(self, script):
return Popen([str(script)], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, env=self.post_process_environment,
close_fds=(os.name != 'nt'))