diff --git a/README.md b/README.md index fccceb4d0..06a306f1e 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ If you need something that is not already part of Bazarr, feel free to create a * Hosszupuska * LegendasTV * Napiprojekt +* Napisy24 * OpenSubtitles * Podnapisi * Subs.sab.bz diff --git a/bazarr/config.py b/bazarr/config.py index 2bfdcf2bf..317a849e9 100644 --- a/bazarr/config.py +++ b/bazarr/config.py @@ -107,6 +107,10 @@ defaults = { 'deathbycaptcha': { 'username': '', 'password': '' + }, + 'napisy24': { + 'username': '', + 'password': '' } } diff --git a/bazarr/get_providers.py b/bazarr/get_providers.py index 1dafee00c..c87c38674 100644 --- a/bazarr/get_providers.py +++ b/bazarr/get_providers.py @@ -109,7 +109,10 @@ def get_providers_auth(): 'xsubs': {'username': settings.xsubs.username, 'password': settings.xsubs.password, }, - 'assrt': {'token': settings.assrt.token, } + 'assrt': {'token': settings.assrt.token, }, + 'napisy24': {'username': settings.napisy24.username, + 'password': settings.napisy24.password, + } } return providers_auth diff --git a/bazarr/main.py b/bazarr/main.py index 7d49ee4a2..c74225df7 100644 --- a/bazarr/main.py +++ b/bazarr/main.py @@ -391,6 +391,8 @@ def save_wizard(): settings.opensubtitles.skip_wrong_fps = text_type(settings_opensubtitles_skip_wrong_fps) settings.xsubs.username = request.forms.get('settings_xsubs_username') settings.xsubs.password = request.forms.get('settings_xsubs_password') + settings.napisy24.username = request.forms.get('settings_napisy24_username') + settings.napisy24.password = request.forms.get('settings_napisy24_password') settings_subliminal_languages = request.forms.getall('settings_subliminal_languages') c.execute("UPDATE table_settings_languages SET enabled = 0") @@ -1494,6 +1496,8 @@ def save_settings(): settings.opensubtitles.skip_wrong_fps = text_type(settings_opensubtitles_skip_wrong_fps) settings.xsubs.username = request.forms.get('settings_xsubs_username') settings.xsubs.password = request.forms.get('settings_xsubs_password') + settings.napisy24.username = request.forms.get('settings_napisy24_username') + settings.napisy24.password = request.forms.get('settings_napisy24_password') settings_subliminal_languages = request.forms.getall('settings_subliminal_languages') c.execute("UPDATE table_settings_languages SET enabled = 0") diff --git a/libs/subliminal_patch/providers/napisy24.py b/libs/subliminal_patch/providers/napisy24.py new file mode 100644 index 000000000..e9d4b2eec --- /dev/null +++ b/libs/subliminal_patch/providers/napisy24.py @@ -0,0 +1,124 @@ +import logging +import os +from io import BytesIO +from zipfile import ZipFile + +from babelfish import Language +from requests import Session + +from subliminal_patch.subtitle import Subtitle +from subliminal_patch.providers import Provider +from subliminal import __short_version__ +from subliminal.exceptions import AuthenticationError, ConfigurationError +from subliminal.subtitle import fix_line_ending + +logger = logging.getLogger(__name__) + + +class Napisy24Subtitle(Subtitle): + '''Napisy24 Subtitle.''' + provider_name = 'napisy24' + + def __init__(self, language, hash, imdb_id, napis_id): + super(Napisy24Subtitle, self).__init__(language) + self.hash = hash + self.imdb_id = imdb_id + self.napis_id = napis_id + + @property + def id(self): + return self.hash + + def get_matches(self, video): + matches = set() + + # hash + if 'opensubtitles' in video.hashes and video.hashes['opensubtitles'] == self.hash: + matches.add('hash') + + # imdb_id + if video.imdb_id and self.imdb_id == video.imdb_id: + matches.add('imdb_id') + + return matches + + +class Napisy24Provider(Provider): + '''Napisy24 Provider.''' + languages = {Language(l) for l in ['pol']} + required_hash = 'opensubtitles' + api_url = 'http://napisy24.pl/run/CheckSubAgent.php' + + def __init__(self, username=None, password=None): + if all((username, password)): + self.username = username + self.password = password + else: + self.username = 'subliminal' + self.password = 'lanimilbus' + + self.session = None + + def initialize(self): + self.session = Session() + self.session.headers['User-Agent'] = 'Subliminal/%s' % __short_version__ + self.session.headers['Content-Type'] = 'application/x-www-form-urlencoded' + + def terminate(self): + self.session.close() + + def query(self, language, size, name, hash): + params = { + 'postAction': 'CheckSub', + 'ua': self.username, + 'ap': self.password, + 'fs': size, + 'fh': hash, + 'fn': os.path.basename(name), + 'n24pref': 1 + } + + response = self.session.post(self.api_url, data=params, timeout=10) + response.raise_for_status() + + response_content = response.content.split(b'||', 1) + n24_data = response_content[0].decode() + + if n24_data[:2] != 'OK': + if n24_data[:11] == 'login error': + raise AuthenticationError('Login failed') + logger.error('Unknown response: %s', response.content) + return None + + n24_status = n24_data[:4] + if n24_status == 'OK-0': + logger.info('No subtitles found') + return None + + subtitle_info = dict(p.split(':', 1) for p in n24_data.split('|')[1:]) + logger.debug('Subtitle info: %s', subtitle_info) + + if n24_status == 'OK-1': + logger.info('No subtitles found but got video info') + return None + elif n24_status == 'OK-2': + logger.info('Found subtitles') + elif n24_status == 'OK-3': + logger.info('Found subtitles but not from Napisy24 database') + return None + + subtitle_content = response_content[1] + + subtitle = Napisy24Subtitle(language, hash, 'tt%s' % subtitle_info['imdb'].zfill(7), subtitle_info['napisId']) + with ZipFile(BytesIO(subtitle_content)) as zf: + subtitle.content = fix_line_ending(zf.open(zf.namelist()[0]).read()) + + return subtitle + + def list_subtitles(self, video, languages): + subtitles = [self.query(l, video.size, video.name, video.hashes['opensubtitles']) for l in languages] + return [s for s in subtitles if s is not None] + + def download_subtitle(self, subtitle): + # there is no download step, content is already filled from listing subtitles + pass diff --git a/views/settings.tpl b/views/settings.tpl index 04e21e885..f282b843c 100644 --- a/views/settings.tpl +++ b/views/settings.tpl @@ -1576,6 +1576,54 @@ +
+
+ +
+
+
+ + +
+
+ +
+
+
+
+ +
+
+
+ +
+
+ +
+
+
+ +
+
+
+ +
+
+
+
+
diff --git a/views/wizard.tpl b/views/wizard.tpl index dd6c9ae1d..980cd523f 100644 --- a/views/wizard.tpl +++ b/views/wizard.tpl @@ -646,6 +646,54 @@
+
+
+ +
+
+
+ + +
+
+ +
+
+
+
+ +
+
+
+ +
+
+ +
+
+
+ +
+
+
+ +
+
+
+
+