# -*- coding: utf-8 -*- import json import logging import os import re import io from babelfish import language_converters from guessit import guessit from requests import Session from subzero.language import Language from subliminal import Movie, Episode, ProviderError, __short_version__ from subliminal.exceptions import AuthenticationError, ConfigurationError, DownloadLimitExceeded, ProviderError from subliminal_patch.subtitle import Subtitle, guess_matches from subliminal.subtitle import fix_line_ending, SUBTITLE_EXTENSIONS from subliminal_patch.providers import Provider logger = logging.getLogger(__name__) server_url = 'https://subtitulamos.tv/' class SubtitulamosTVSubtitle(Subtitle): provider_name = 'subtitulamostv' hash_verifiable = False def __init__(self, language, page_link, download_link, description, title, matches, release_info): super(SubtitulamosTVSubtitle, self).__init__(language, hearing_impaired=False, page_link=page_link) self.download_link = download_link self.description = description.lower() self.title = title self.release_info = release_info self.found_matches = matches @property def id(self): return self.download_link def get_matches(self, video): matches = self.found_matches # release_group if video.release_group and video.release_group.lower() in self.description: matches.add('release_group') # resolution if video.resolution and video.resolution.lower() in self.description: matches.add('resolution') # format if video.format: formats = [video.format.lower()] if formats[0] == "web-dl": formats.append("webdl") formats.append("webrip") formats.append("web ") for frmt in formats: if frmt.lower() in self.description: matches.add('format') break # video_codec if video.video_codec: video_codecs = [video.video_codec.lower()] if video_codecs[0] == "h264": formats.append("x264") elif video_codecs[0] == "h265": formats.append("x265") for vc in formats: if vc.lower() in self.description: matches.add('video_codec') break return matches class SubtitulamosTVProvider(Provider): """Subtitulamostv Provider""" languages = {Language.fromietf(l) for l in ['en','es']} video_types = (Episode,) def initialize(self): self.session = Session() self.session.headers = { 'User-Agent': os.environ.get("SZ_USER_AGENT", "Sub-Zero/2")} def terminate(self): self.session.close() def query(self, languages, video): # query the server result = None year = (" (%d)" % video.year) if video.year else "" q = "%s%s %dx%02d" % (video.series, year, video.season, video.episode) logger.debug('Searching subtitles "%s"', q) res = self.session.get( server_url + 'search/query', params={'q':q}, timeout=10) res.raise_for_status() result = res.json() subtitles = [] for s in [s for s in result if len(s['episodes'])]: for e in s['episodes']: res = self.session.get( server_url + 'episodes/%d' % e['id'], timeout=10) res.raise_for_status() html = res.text for lang_m in re.finditer(r"