Skip to content

Commit

Permalink
Merge pull request #47 from pymedusa/pre-subs
Browse files Browse the repository at this point in the history
Add pre-scripts for subtitles
  • Loading branch information
medariox committed Feb 28, 2016
2 parents ff52ecc + 12cfbac commit e1a1687
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
11 changes: 11 additions & 0 deletions gui/slick/views/config_subtitles.mako
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,17 @@ $('#subtitles_dir').fileBrowser({ title: 'Select Subtitles Download Directory' }
</span>
</label>
</div>
<div class="field-pair">
<label>
<span class="component-title">Pre-Scripts</span>
<input type="text" name="subtitles_pre_scripts" value="${'|'.join(sickbeard.SUBTITLES_PRE_SCRIPTS)}" class="form-control input-sm input350" autocapitalize="off" />
</label>
<label>
<span class="component-desc">
<p>Show's media filename is passed as argument for the pre-scripts. Pre-scripts are executed before trying to find subtitles from usual sources.</p>
</span>
</label>
</div>
<div class="field-pair">
<label>
<span class="component-title">Extra Scripts</span>
Expand Down
5 changes: 4 additions & 1 deletion sickbeard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@
SUBTITLES_FINDER_FREQUENCY = 1
SUBTITLES_MULTI = False
SUBTITLES_EXTRA_SCRIPTS = []
SUBTITLES_PRE_SCRIPTS = []
SUBTITLES_DOWNLOAD_IN_PP = False
SUBTITLES_KEEP_ONLY_WANTED = False

Expand Down Expand Up @@ -634,7 +635,7 @@ def initialize(consoleLogging=True): # pylint: disable=too-many-locals, too-man
GUI_NAME, HOME_LAYOUT, HISTORY_LAYOUT, DISPLAY_SHOW_SPECIALS, COMING_EPS_LAYOUT, COMING_EPS_SORT, COMING_EPS_DISPLAY_PAUSED, COMING_EPS_MISSED_RANGE, FUZZY_DATING, TRIM_ZERO, DATE_PRESET, TIME_PRESET, TIME_PRESET_W_SECONDS, THEME_NAME, \
POSTER_SORTBY, POSTER_SORTDIR, HISTORY_LIMIT, CREATE_MISSING_SHOW_DIRS, ADD_SHOWS_WO_DIR, \
METADATA_WDTV, METADATA_TIVO, METADATA_MEDE8ER, IGNORE_WORDS, PREFERRED_WORDS, UNDESIRED_WORDS, TRACKERS_LIST, IGNORED_SUBS_LIST, REQUIRE_WORDS, CALENDAR_UNPROTECTED, CALENDAR_ICONS, NO_RESTART, \
USE_SUBTITLES, SUBTITLES_LANGUAGES, SUBTITLES_DIR, SUBTITLES_SERVICES_LIST, SUBTITLES_SERVICES_ENABLED, SUBTITLES_HISTORY, SUBTITLES_FINDER_FREQUENCY, SUBTITLES_MULTI, SUBTITLES_DOWNLOAD_IN_PP, SUBTITLES_KEEP_ONLY_WANTED, EMBEDDED_SUBTITLES_ALL, SUBTITLES_EXTRA_SCRIPTS, SUBTITLES_PERFECT_MATCH, subtitlesFinderScheduler, \
USE_SUBTITLES, SUBTITLES_LANGUAGES, SUBTITLES_DIR, SUBTITLES_SERVICES_LIST, SUBTITLES_SERVICES_ENABLED, SUBTITLES_HISTORY, SUBTITLES_FINDER_FREQUENCY, SUBTITLES_MULTI, SUBTITLES_DOWNLOAD_IN_PP, SUBTITLES_KEEP_ONLY_WANTED, EMBEDDED_SUBTITLES_ALL, SUBTITLES_EXTRA_SCRIPTS, SUBTITLES_PRE_SCRIPTS, SUBTITLES_PERFECT_MATCH, subtitlesFinderScheduler, \
SUBTITLES_HEARING_IMPAIRED, ADDIC7ED_USER, ADDIC7ED_PASS, LEGENDASTV_USER, LEGENDASTV_PASS, OPENSUBTITLES_USER, OPENSUBTITLES_PASS, \
USE_FAILED_DOWNLOADS, DELETE_FAILED, ANON_REDIRECT, LOCALHOST_IP, DEBUG, DBDEBUG, DEFAULT_PAGE, PROXY_SETTING, PROXY_INDEXERS, \
AUTOPOSTPROCESSER_FREQUENCY, SHOWUPDATE_HOUR, \
Expand Down Expand Up @@ -1191,6 +1192,7 @@ def path_leaf(path):
SUBTITLES_DOWNLOAD_IN_PP = bool(check_setting_int(CFG, 'Subtitles', 'subtitles_download_in_pp', 0))
SUBTITLES_KEEP_ONLY_WANTED = bool(check_setting_int(CFG, 'Subtitles', 'subtitles_keep_only_wanted', 0))
SUBTITLES_EXTRA_SCRIPTS = [x.strip() for x in check_setting_str(CFG, 'Subtitles', 'subtitles_extra_scripts', '').split('|') if x.strip()]
SUBTITLES_PRE_SCRIPTS = [x.strip() for x in check_setting_str(CFG, 'Subtitles', 'subtitles_pre_scripts', '').split('|') if x.strip()]

ADDIC7ED_USER = check_setting_str(CFG, 'Subtitles', 'addic7ed_username', '', censor_log=True)
ADDIC7ED_PASS = check_setting_str(CFG, 'Subtitles', 'addic7ed_password', '', censor_log=True)
Expand Down Expand Up @@ -2146,6 +2148,7 @@ def save_config(): # pylint: disable=too-many-statements, too-many-branches
new_config['Subtitles']['subtitles_finder_frequency'] = int(SUBTITLES_FINDER_FREQUENCY)
new_config['Subtitles']['subtitles_multi'] = int(SUBTITLES_MULTI)
new_config['Subtitles']['subtitles_extra_scripts'] = '|'.join(SUBTITLES_EXTRA_SCRIPTS)
new_config['Subtitles']['subtitles_pre_scripts'] = '|'.join(SUBTITLES_PRE_SCRIPTS)
new_config['Subtitles']['subtitles_download_in_pp'] = int(SUBTITLES_DOWNLOAD_IN_PP)
new_config['Subtitles']['subtitles_keep_only_wanted'] = int(SUBTITLES_KEEP_ONLY_WANTED)
new_config['Subtitles']['addic7ed_username'] = ADDIC7ED_USER
Expand Down
36 changes: 20 additions & 16 deletions sickbeard/subtitles.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ def download_subtitles(subtitles_info): # pylint: disable=too-many-locals, too-
subtitle_path = subliminal.subtitle.get_subtitle_path(video.name,
None if not sickbeard.SUBTITLES_MULTI else
subtitle.language)
if subtitles_path is not None:
if subtitles_path:
subtitle_path = os.path.join(subtitles_path, os.path.split(subtitle_path)[1])

sickbeard.helpers.chmodAsParent(subtitle_path)
Expand All @@ -222,7 +222,7 @@ def download_subtitles(subtitles_info): # pylint: disable=too-many-locals, too-
subtitles_info['episode'], subtitles_info['status'], subtitle)

if sickbeard.SUBTITLES_EXTRA_SCRIPTS and isMediaFile(video_path) and not sickbeard.EMBEDDED_SUBTITLES_ALL:
run_subs_extra_scripts(subtitles_info, subtitle, video, single=not sickbeard.SUBTITLES_MULTI)
run_subs_scripts(subtitles_info, subtitle.language, subtitle_path, video.name)

new_subtitles = sorted({subtitle.language.opensubtitles for subtitle in found_subtitles})
current_subtitles = sorted({subtitle for subtitle in new_subtitles + existing_subtitles}) if existing_subtitles else new_subtitles
Expand Down Expand Up @@ -390,6 +390,9 @@ def subtitles_download_in_pp(): # pylint: disable=too-many-locals, too-many-bra
logger.log(u"Couldn't delete subtitle: {}. Error: {}".format(filename, ex(error)), logger.DEBUG)

if isMediaFile(filename) and processTV.subtitles_enabled(filename):
if sickbeard.SUBTITLES_PRE_SCRIPTS and not sickbeard.EMBEDDED_SUBTITLES_ALL:
run_subs_scripts(None, None, None, filename, is_pre=True)

try:
video = subliminal.scan_video(os.path.join(root, filename),
subtitles=False, embedded_subtitles=False)
Expand Down Expand Up @@ -429,7 +432,7 @@ def subtitles_download_in_pp(): # pylint: disable=too-many-locals, too-many-bra
subtitle_path = subliminal.subtitle.get_subtitle_path(video.name,
None if subtitles_multi else
subtitle.language)
if root is not None:
if root:
subtitle_path = os.path.join(root, os.path.split(subtitle_path)[1])
sickbeard.helpers.chmodAsParent(subtitle_path)
sickbeard.helpers.fixSetGroupID(subtitle_path)
Expand Down Expand Up @@ -525,12 +528,12 @@ def dhm(td):
if not force:
now = datetime.datetime.now()
days = int(ep_to_sub['age'])
delay_time = datetime.timedelta(hours=8 if days < 10 else 7 * 24 if days < 30 else 30 * 24)
delay_time = datetime.timedelta(hours=1 if days <= 10 else 8 if days <= 30 else 30 * 24)

# Search every hour for the first 24 hours since aired, then every 8 hours until 10 days passes
# After 10 days, search every 7 days, after 30 days search once a month
# Will always try an episode regardless of age at least 2 times
if lastsearched + delay_time > now and int(ep_to_sub['searchcount']) > 2 and days:
# Search every hour until 10 days pass
# After 10 days, search every 8 hours, after 30 days search once a month
# Will always try an episode regardless of age for 3 times
if lastsearched + delay_time > now and int(ep_to_sub['searchcount']) > 2:
logger.log(u'Subtitle search for {} {} delayed for {}'.format
(ep_to_sub['show_name'], episode_num(ep_to_sub['season'], ep_to_sub['episode']) or
episode_num(ep_to_sub['season'], ep_to_sub['episode'], numbering='absolute'),
Expand Down Expand Up @@ -576,18 +579,19 @@ def dhm(td):
self.amActive = False


def run_subs_extra_scripts(episode_object, subtitle, video, single=False):
def run_subs_scripts(subtitles_info, subtitle_language, subtitle_path, video_filename, is_pre=None):
for script_name in sickbeard.SUBTITLES_EXTRA_SCRIPTS:
script_cmd = [piece for piece in re.split("( |\\\".*?\\\"|'.*?')", script_name) if piece.strip()]
script_cmd[0] = os.path.abspath(script_cmd[0])
logger.log(u'Absolute path to script: {}'.format(script_cmd[0]), logger.DEBUG)

subtitle_path = subliminal.subtitle.get_subtitle_path(video.name, None if single else subtitle.language)

inner_cmd = script_cmd + [video.name, subtitle_path, subtitle.language.opensubtitles,
episode_object['show_name'], str(episode_object['season']),
str(episode_object['episode']), episode_object['name'],
str(episode_object['show_indexerid'])]
if is_pre:
inner_cmd = script_cmd + ['/'.join([sickbeard.TV_DOWNLOAD_DIR, video_filename])]
else:
inner_cmd = script_cmd + [video_filename, subtitle_path, subtitle_language.opensubtitles,
subtitles_info['show_name'], str(subtitles_info['season']),
str(subtitles_info['episode']), subtitles_info['name'],
str(subtitles_info['show_indexerid'])]

# use subprocess to run the command and capture output
logger.log(u'Executing command: {}'.format(inner_cmd))
Expand All @@ -598,4 +602,4 @@ def run_subs_extra_scripts(episode_object, subtitle, video, single=False):
logger.log(u'Script result: {}'.format(out), logger.DEBUG)

except Exception as error:
logger.log(u'Unable to run subs_extra_script: {}'.format(ex(error)))
logger.log(u'Unable to run subtitles script: {}'.format(ex(error)))
3 changes: 2 additions & 1 deletion sickbeard/webserve.py
Original file line number Diff line number Diff line change
Expand Up @@ -5099,7 +5099,7 @@ def index(self):

def saveSubtitles(self, use_subtitles=None, subtitles_plugins=None, subtitles_languages=None, subtitles_dir=None, subtitles_perfect_match=None,
service_order=None, subtitles_history=None, subtitles_finder_frequency=None,
subtitles_multi=None, embedded_subtitles_all=None, subtitles_extra_scripts=None, subtitles_hearing_impaired=None,
subtitles_multi=None, embedded_subtitles_all=None, subtitles_extra_scripts=None, subtitles_pre_scripts=None, subtitles_hearing_impaired=None,
addic7ed_user=None, addic7ed_pass=None, legendastv_user=None, legendastv_pass=None, opensubtitles_user=None, opensubtitles_pass=None,
subtitles_download_in_pp=None, subtitles_keep_only_wanted=None):

Expand All @@ -5118,6 +5118,7 @@ def saveSubtitles(self, use_subtitles=None, subtitles_plugins=None, subtitles_la
sickbeard.SUBTITLES_DOWNLOAD_IN_PP = config.checkbox_to_value(subtitles_download_in_pp)
sickbeard.SUBTITLES_KEEP_ONLY_WANTED = config.checkbox_to_value(subtitles_keep_only_wanted)
sickbeard.SUBTITLES_EXTRA_SCRIPTS = [x.strip() for x in subtitles_extra_scripts.split('|') if x.strip()]
sickbeard.SUBTITLES_PRE_SCRIPTS = [x.strip() for x in subtitles_pre_scripts.split('|') if x.strip()]

# Subtitles services
services_str_list = service_order.split()
Expand Down

0 comments on commit e1a1687

Please sign in to comment.