Browse Source

Proper subtitle support

pull/110/head
Ruud 13 years ago
parent
commit
be8749e472
  1. 15
      couchpotato/core/plugins/renamer/main.py
  2. 45
      couchpotato/core/plugins/scanner/main.py
  3. 27
      couchpotato/core/plugins/subtitle/main.py
  4. 0
      couchpotato/core/providers/subtitle/__init__.py
  5. 13
      couchpotato/core/providers/subtitle/base.py
  6. 6
      couchpotato/core/providers/subtitle/subliminal/__init__.py
  7. 22
      couchpotato/core/providers/subtitle/subliminal/main.py

15
couchpotato/core/plugins/renamer/main.py

@ -192,11 +192,16 @@ class Renamer(Plugin):
if file_type is 'leftover': if file_type is 'leftover':
if self.conf('move_leftover'): if self.conf('move_leftover'):
rename_files[current_file] = os.path.join(destination, final_folder_name, os.path.basename(current_file)) rename_files[current_file] = os.path.join(destination, final_folder_name, os.path.basename(current_file))
else: elif file_type not in ['subtitle']:
rename_files[current_file] = os.path.join(destination, final_folder_name, final_file_name) rename_files[current_file] = os.path.join(destination, final_folder_name, final_file_name)
# Check for extra subtitle files # Check for extra subtitle files
if file_type is 'subtitle': if file_type is 'subtitle':
# rename subtitles with or without language
#rename_files[current_file] = os.path.join(destination, final_folder_name, final_file_name)
sub_langs = group['subtitle_language'].get(current_file)
rename_extras = self.getRenameExtras( rename_extras = self.getRenameExtras(
extra_type = 'subtitle_extra', extra_type = 'subtitle_extra',
replacements = replacements, replacements = replacements,
@ -206,6 +211,14 @@ class Renamer(Plugin):
group = group, group = group,
current_file = current_file current_file = current_file
) )
# Don't add language if multiple languages in 1 file
if len(sub_langs) > 1:
rename_files[current_file] = os.path.join(destination, final_folder_name, final_file_name)
elif len(sub_langs) == 1:
sub_name = final_file_name.replace(replacements['ext'], '%s.%s' % (sub_langs[0], replacements['ext']))
rename_files[current_file] = os.path.join(destination, final_folder_name, sub_name)
rename_files = mergeDicts(rename_files, rename_extras) rename_files = mergeDicts(rename_files, rename_extras)
# Filename without cd etc # Filename without cd etc

45
couchpotato/core/plugins/scanner/main.py

@ -7,6 +7,7 @@ from couchpotato.core.plugins.base import Plugin
from couchpotato.core.settings.model import File from couchpotato.core.settings.model import File
from couchpotato.environment import Env from couchpotato.environment import Env
from enzyme.exceptions import NoParserError, ParseError from enzyme.exceptions import NoParserError, ParseError
from subliminal.videos import scan
import enzyme import enzyme
import logging import logging
import os import os
@ -208,7 +209,7 @@ class Scanner(Plugin):
for identifier, group in movie_files.iteritems(): for identifier, group in movie_files.iteritems():
if identifier not in group['identifiers'] and len(identifier) > 0: group['identifiers'].append(identifier) if identifier not in group['identifiers'] and len(identifier) > 0: group['identifiers'].append(identifier)
log.debug('Grouping files for: %s' % identifier) log.debug('Grouping files: %s' % identifier)
for file_path in group['unsorted_files']: for file_path in group['unsorted_files']:
wo_ext = file_path[:-(len(getExt(file_path)) + 1)] wo_ext = file_path[:-(len(getExt(file_path)) + 1)]
@ -233,7 +234,7 @@ class Scanner(Plugin):
# Group the files based on the identifier # Group the files based on the identifier
for identifier, group in movie_files.iteritems(): for identifier, group in movie_files.iteritems():
log.debug('Grouping files for: %s' % identifier) log.debug('Grouping files on identifier: %s' % identifier)
found_files = set(self.path_identifiers.get(identifier, [])) found_files = set(self.path_identifiers.get(identifier, []))
group['unsorted_files'].extend(found_files) group['unsorted_files'].extend(found_files)
@ -262,7 +263,7 @@ class Scanner(Plugin):
file_too_new = tryInt(time.time() - file_time) file_too_new = tryInt(time.time() - file_time)
break break
if file_too_new: if file_too_new and not Env.get('dev'):
log.info('Files seem to be still unpacking or just unpacked (created on %s), ignoring for now: %s' % (time.ctime(file_time), identifier)) log.info('Files seem to be still unpacking or just unpacked (created on %s), ignoring for now: %s' % (time.ctime(file_time), identifier))
continue continue
@ -291,6 +292,9 @@ class Scanner(Plugin):
log.debug('Getting metadata for %s' % identifier) log.debug('Getting metadata for %s' % identifier)
group['meta_data'] = self.getMetaData(group) group['meta_data'] = self.getMetaData(group)
# Subtitle meta
group['subtitle_language'] = self.getSubtitleLanguage(group)
# Get parent dir from movie files # Get parent dir from movie files
for movie_file in group['files']['movie']: for movie_file in group['files']['movie']:
group['parentdir'] = os.path.dirname(movie_file) group['parentdir'] = os.path.dirname(movie_file)
@ -381,6 +385,41 @@ class Scanner(Plugin):
return {} return {}
def getSubtitleLanguage(self, group):
detected_languages = {}
# Subliminal scanner
try:
paths = group['files']['movie']
scan_result = []
for p in paths:
scan_result.extend(scan(p))
for video, detected_subtitles in scan_result:
for s in detected_subtitles:
if s.language and s.path not in paths:
detected_languages[s.path] = [s.language]
except:
log.error('Failed parsing subtitle languages for %s: %s' % (paths, traceback.format_exc()))
# IDX
for extra in group['files']['subtitle_extra']:
try:
if os.path.isfile(extra):
output = open(extra, 'r')
txt = output.read()
output.close()
idx_langs = re.findall('\nid: (\w+)', txt)
sub_file = '%s.sub' % os.path.splitext(extra)[0]
if len(idx_langs) > 0 and os.path.isfile(sub_file):
detected_languages[sub_file] = idx_langs
except:
log.error('Failed parsing subtitle idx for %s: %s' % (extra, traceback.format_exc()))
return detected_languages
def determineMovie(self, group): def determineMovie(self, group):
imdb_id = None imdb_id = None

27
couchpotato/core/plugins/subtitle/main.py

@ -3,15 +3,18 @@ from couchpotato.core.event import addEvent, fireEvent
from couchpotato.core.logger import CPLog from couchpotato.core.logger import CPLog
from couchpotato.core.plugins.base import Plugin from couchpotato.core.plugins.base import Plugin
from couchpotato.core.settings.model import Library, FileType from couchpotato.core.settings.model import Library, FileType
from couchpotato.environment import Env
import subliminal
log = CPLog(__name__) log = CPLog(__name__)
class Subtitle(Plugin): class Subtitle(Plugin):
services = ['opensubtitles', 'thesubdb', 'subswiki']
def __init__(self): def __init__(self):
pass addEvent('renamer.before', self.searchSingle)
#addEvent('renamer.before', self.searchSingle)
def searchLibrary(self): def searchLibrary(self):
@ -33,20 +36,22 @@ class Subtitle(Plugin):
files.append(file.path); files.append(file.path);
# get subtitles for those files # get subtitles for those files
subtitles = fireEvent('subtitle.search', files = files, languages = self.getLanguages(), merge = True) subliminal.list_subtitles(files, cache_dir = Env.get('cache_dir'), multi = True, languages = self.getLanguages(), services = self.services)
# do something with the returned subtitles
print subtitles
def searchSingle(self, group): def searchSingle(self, group):
if self.isDisabled(): return if self.isDisabled(): return
subtitles = fireEvent('subtitle.search', files = group['files']['movie'], languages = self.getLanguages(), merge = True) available_languages = sum(group['subtitle_language'].itervalues(), [])
downloaded = []
for lang in self.getLanguages():
if lang not in available_languages:
download = subliminal.download_subtitles(group['files']['movie'], multi = True, force = False, languages = [lang], services = self.services, cache_dir = Env.get('cache_dir'))
downloaded.extend(download)
# do something with the returned subtitles for d_sub in downloaded:
print subtitles group['files']['subtitle'].add(d_sub.path)
group['subtitle_language'][d_sub.path] = [d_sub.language]
def getLanguages(self): def getLanguages(self):
return self.conf('languages').split(',') return [x.strip() for x in self.conf('languages').split(',')]

0
couchpotato/core/providers/subtitle/__init__.py

13
couchpotato/core/providers/subtitle/base.py

@ -1,13 +0,0 @@
from couchpotato.core.event import addEvent
from couchpotato.core.providers.base import Provider
class SubtitleProvider(Provider):
type = 'subtitle'
def __init__(self):
addEvent('subtitle.search', self.search)
def search(self, group):
pass

6
couchpotato/core/providers/subtitle/subliminal/__init__.py

@ -1,6 +0,0 @@
from .main import SubliminalProvider
def start():
return SubliminalProvider()
config = []

22
couchpotato/core/providers/subtitle/subliminal/main.py

@ -1,22 +0,0 @@
from couchpotato.core.logger import CPLog
from couchpotato.core.providers.subtitle.base import SubtitleProvider
from couchpotato.environment import Env
from libs import subliminal
log = CPLog(__name__)
class SubliminalProvider(SubtitleProvider):
plugins = ['OpenSubtitles', 'TheSubDB', 'SubsWiki']
def search(self, files = [], languages = []):
# download subtitles
with subliminal.Subliminal(cache_dir = Env.get('cache_dir'), multi = True,
languages = self.getLanguages(), plugins = self.plugins) as subli:
subtitles = subli.downloadSubtitles(files)
print subtitles
return subtitles
Loading…
Cancel
Save