Commit 63f98ef0 authored by echel0n's avatar echel0n
Browse files

Cleaned up subtitles code.

parent 1558dcea
# Changelog
- * 158beba - 2018-11-10: Fixed issue with displaying language flags in views
- * fec82ca - 2018-11-11: Cleaned up subtitles code.
- * 1558dce - 2018-11-10: Fixed issue with displaying language flags in views
- * 0173a0f - 2018-11-10: Release v9.4.28
- * c5ea143 - 2018-11-10: Release v9.4.27
- * 0f190e0 - 2018-11-10: Added function to check database index data integrity and rebuild if failed
......@@ -24,14 +24,15 @@ import subprocess
import subliminal
from babelfish import language_converters, Language
from guessit import guessit
from subliminal import save_subtitles
import sickrage
from sickrage.core import makeDir
from sickrage.core.helpers import chmodAsParent
# register provider
from sickrage.core.scene_exceptions import get_scene_exceptions
from sickrage.subtitles.providers.utils import hash_itasa
for provider in ['itasa = sickrage.subtitles.providers.itasa:ItaSAProvider',
'legendastv = subliminal.providers.legendastv:LegendasTVProvider',
'wizdom = sickrage.subtitles.providers.wizdom:WizdomProvider',
......@@ -57,6 +58,7 @@ PROVIDER_URLS = {
subtitle_extensions = ['srt', 'sub', 'ass', 'idx', 'ssa']
episode_refiners = ('metadata', 'release', 'tvepisode', 'tvdb', 'omdb')
def sortedServiceList():
......@@ -112,14 +114,23 @@ def download_subtitles(episode):
return existing_subtitles, None
provider_configs = {
'addic7ed': {'username':,
'itasa': {'username':,
'legendastv': {'username':,
'opensubtitles': {'username':,
'addic7ed': {
'itasa': {
'legendastv': {
'opensubtitles': {
pool = subliminal.ProviderPool(providers=providers, provider_configs=provider_configs)
......@@ -177,7 +188,7 @@ def get_needed_languages(subtitles):
def refresh_subtitles(episode):
video = get_video(episode.location)
video = get_video(episode.location, episode=episode)
if not video:"Exception caught in subliminal.scan_video, subtitles couldn't be refreshed")
return episode.subtitles, None
......@@ -195,6 +206,11 @@ def get_video(video_path, subtitles_path=None, subtitles=True, embedded_subtitle
video = subliminal.scan_video(video_path)
except Exception as error:'Exception: {}'.format(error))
if video.size > 10485760:
video.hashes['itasa'] = hash_itasa(video_path)
# external subtitles
if subtitles:
......@@ -205,19 +221,15 @@ def get_video(video_path, subtitles_path=None, subtitles=True, embedded_subtitle
embedded_subtitles = bool(
not and video_path.endswith('.mkv'))
# Let sickrage add more information to video file, based on the metadata.
if episode:
refine_video(video, episode)
subliminal.refine(video, episode_refiners=episode_refiners, embedded_subtitles=embedded_subtitles,, tv_episode=episode)
subliminal.refine(video, embedded_subtitles=embedded_subtitles)
except Exception as error:'Exception: {}'.format(error))
return None
video.alternative_series = list(get_scene_exceptions(
# remove format metadata
video.format = ""
# remove format metadata
video.format = ""
return video
return video
def get_subtitles_path(video_path):
......@@ -315,40 +327,3 @@ def run_subs_extra_scripts(episode, found_subtitles, video, single=False):
except Exception as e:"Unable to run subs_extra_script: {}".format(e))
def refine_video(video, episode):
# try to enrich video object using information in original filename
if episode.release_name:
guess_ep = subliminal.Episode.fromguess(None, guessit(episode.release_name))
for name in vars(guess_ep):
if getattr(guess_ep, name) and not getattr(video, name):
setattr(video, name, getattr(guess_ep, name))
# Use sickrage metadata
metadata_mapping = {
'episode': 'episode',
'release_group': 'release_group',
'season': 'season',
'series': '',
'series_imdb_id': 'show.imdbid',
'size': 'file_size',
'title': 'name',
'year': 'show.startyear'
def get_attr_value(obj, name):
value = None
for attr in name.split('.'):
if not value:
value = getattr(obj, attr, None)
value = getattr(value, attr, None)
return value
for name in metadata_mapping:
if not getattr(video, name) and get_attr_value(episode, metadata_mapping[name]):
setattr(video, name, get_attr_value(episode, metadata_mapping[name]))
elif and get_attr_value(episode, metadata_mapping[name]):
setattr(video, name, get_attr_value(episode, metadata_mapping[name]))
# Author: echel0n <[email protected]>
# URL:
# This file is part of SickRage.
# SickRage is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# SickRage is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with SickRage. If not, see <>.
from __future__ import unicode_literals
import hashlib
def hash_itasa(video_path):
"""Compute a hash using ItaSA's algorithm.
:param str video_path: path of the video.
:return: the hash.
:rtype: str
readsize = 1024 * 1024 * 10
with open(video_path, 'rb') as f:
data =
return hashlib.md5(data).hexdigest()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment