bazarr/libs/subliminal_patch/providers/subtitulamostv.py

162 lines
6.3 KiB
Python
Raw Normal View History

2019-08-09 14:41:35 +00:00
# -*- 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,
2019-10-31 03:57:50 +00:00
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
2019-08-09 14:41:35 +00:00
@property
def id(self):
return self.download_link
2019-08-09 14:41:35 +00:00
def get_matches(self, video):
matches = self.found_matches
2019-08-09 14:41:35 +00:00
# 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')
2020-05-20 15:29:39 +00:00
# source
if video.source:
formats = [video.source.lower()]
if formats[0] == "web":
formats.append("webdl")
formats.append("webrip")
formats.append("web ")
for frmt in formats:
if frmt.lower() in self.description:
2020-05-20 15:29:39 +00:00
matches.add('source')
break
# video_codec
if video.video_codec:
video_codecs = [video.video_codec.lower()]
2020-05-20 15:29:39 +00:00
if video_codecs[0] == "H.264":
formats.append("x264")
2020-05-20 15:29:39 +00:00
elif video_codecs[0] == "H.265":
formats.append("x265")
for vc in formats:
if vc.lower() in self.description:
matches.add('video_codec')
break
2019-08-09 14:41:35 +00:00
return matches
class SubtitulamosTVProvider(Provider):
"""Subtitulamostv Provider"""
languages = {Language.fromietf(l) for l in ['en','es']}
2019-08-09 14:41:35 +00:00
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)
2019-08-09 14:41:35 +00:00
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"<div class=\"subtitle_language\">(.*?)<\/div>.*?(?=<div class=\"subtitle_language\">|<div id=\"subtitle-actions\">)", html, re.S):
lang = lang_m.group(1)
language = "es"
if "English" in lang:
2019-08-09 14:41:35 +00:00
language = "en"
logger.debug('Found subtitles in "%s" language.', language)
for subt_m in re.finditer(r"<div class=\"version_name\">(.*?)</div>.*?<a href=\"/(subtitles/\d+/download)\" rel=\"nofollow\">(?:.*?<div class=\"version_comments ?\">.*?</i>(.*?)</p>)?", lang_m.group(0), re.S):
matches = set()
2019-08-09 14:41:35 +00:00
if video.alternative_series is None:
if video.series.lower() == s['name'].lower():
2019-08-09 14:41:35 +00:00
matches.add('series')
2019-11-08 00:37:56 +00:00
elif s['name'].lower() in [video.series.lower()]+list(map(lambda name: name.lower(), video.alternative_series)):
2019-08-09 14:41:35 +00:00
matches.add('series')
if video.season == e['season']:
matches.add('season')
if video.episode == e['number']:
matches.add('episode')
if video.title == e['name']:
matches.add('title')
#if video.year is None or ("(%d)" % video.year) in s['name']:
matches.add('year')
subtitles.append(
SubtitulamosTVSubtitle(
Language.fromietf(language),
server_url + 'episodes/%d' % e['id'],
server_url + subt_m.group(2),
subt_m.group(1)+(subt_m.group(3) if not subt_m.group(3) is None else ""),
e['name'],
matches,
'%s %dx%d,%s,%s' % (s['name'], e['season'], e['number'], subt_m.group(1), lang_m.group(1)),
)
)
2019-08-09 14:41:35 +00:00
return subtitles
def list_subtitles(self, video, languages):
return self.query(languages, video)
def download_subtitle(self, subtitle):
logger.info('Downloading subtitle %r', subtitle)
r = self.session.get(subtitle.download_link, timeout=10)
r.raise_for_status()
2019-10-31 03:57:50 +00:00
r.encoding = "ISO-8859-1"
2019-11-08 00:37:56 +00:00
subtitle_content = r.content
2019-08-09 14:41:35 +00:00
if subtitle_content:
subtitle.content = fix_line_ending(subtitle_content)
else:
logger.debug('Could not download subtitle from %s', subtitle.download_link)