bazarr/libs/subliminal_patch/providers/bsplayer.py

306 lines
10 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from __future__ import absolute_import
import logging
import io
from requests import Session
2021-03-03 03:48:32 +00:00
from requests.adapters import HTTPAdapter
from requests.packages.urllib3.util.retry import Retry
from guessit import guessit
from subliminal_patch.providers import Provider
from subliminal_patch.subtitle import Subtitle
2021-03-03 03:48:32 +00:00
from subliminal_patch.exceptions import TooManyRequests
from subliminal.subtitle import guess_matches
2020-02-23 16:37:02 +00:00
from subliminal.video import Episode, Movie
from subzero.language import Language
2021-01-23 04:10:04 +00:00
from subliminal.exceptions import ServiceUnavailable
import gzip
import random
from time import sleep
from xml.etree import ElementTree
logger = logging.getLogger(__name__)
class BSPlayerSubtitle(Subtitle):
"""BSPlayer Subtitle."""
2021-03-03 03:48:32 +00:00
provider_name = "bsplayer"
hash_verifiable = True
def __init__(self, language, filename, subtype, video, link, subid):
super(BSPlayerSubtitle, self).__init__(language)
self.language = language
self.filename = filename
self.page_link = link
self.subtype = subtype
self.video = video
self.subid = subid
@property
def id(self):
return self.subid
@property
def release_info(self):
return self.filename
def get_matches(self, video):
matches = set()
matches |= guess_matches(video, guessit(self.filename))
2020-02-23 16:37:02 +00:00
# episode
if isinstance(video, Episode):
# already matched in search query
2021-03-03 03:48:32 +00:00
matches.update(["title", "series", "season", "episode", "year"])
2020-02-23 16:37:02 +00:00
# movie
elif isinstance(video, Movie):
# already matched in search query
2021-03-03 03:48:32 +00:00
matches.update(["title", "year"])
2020-02-23 16:37:02 +00:00
2021-03-03 03:48:32 +00:00
matches.add("hash")
return matches
class BSPlayerProvider(Provider):
"""BSPlayer Provider."""
2021-03-03 03:48:32 +00:00
# fmt: off
languages = {Language('por', 'BR')} | {Language(l) for l in [
'ara', 'bul', 'ces', 'dan', 'deu', 'ell', 'eng', 'fin', 'fra', 'hun', 'ita', 'jpn', 'kor', 'nld', 'pol', 'por',
'ron', 'rus', 'spa', 'swe', 'tur', 'ukr', 'zho'
]}
SEARCH_THROTTLE = 8
hash_verifiable = True
2021-03-03 03:48:32 +00:00
# fmt: on
# batantly based on kodi's bsplayer plugin
# also took from BSPlayer-Subtitles-Downloader
def __init__(self):
self.initialize()
def initialize(self):
self.session = Session()
2021-03-03 03:48:32 +00:00
# Try to avoid bsplayer throttling increasing retries time (0, 4, 6, 8, 10)
retry = Retry(connect=5, backoff_factor=2)
adapter = HTTPAdapter(max_retries=retry)
self.session.mount("http://", adapter)
self.search_url = self.get_sub_domain()
self.login()
def terminate(self):
self.session.close()
self.logout()
2021-03-03 03:48:32 +00:00
def api_request(self, func_name="logIn", params="", tries=5):
headers = {
2021-03-03 03:48:32 +00:00
"User-Agent": "BSPlayer/2.x (1022.12360)",
"Content-Type": "text/xml; charset=utf-8",
"Connection": "close",
"SOAPAction": '"http://api.bsplayer-subtitles.com/v1.php#{func_name}"'.format(
func_name=func_name
),
}
data = (
'<?xml version="1.0" encoding="UTF-8"?>\n'
'<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" '
'xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" '
'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" '
'xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:ns1="{search_url}">'
'<SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">'
2021-03-03 03:48:32 +00:00
"<ns1:{func_name}>{params}</ns1:{func_name}></SOAP-ENV:Body></SOAP-ENV:Envelope>"
).format(search_url=self.search_url, func_name=func_name, params=params)
2021-03-03 03:48:32 +00:00
logger.debug("Sending request: %s." % func_name)
for i in iter(range(tries)):
try:
self.session.headers.update(headers.items())
res = self.session.post(self.search_url, data)
2021-03-03 03:48:32 +00:00
return ElementTree.fromstring(res.text.strip())
except Exception as ex:
2021-03-03 03:48:32 +00:00
logger.error(f"Exception parsing response: {ex}")
if func_name == "logIn":
self.search_url = self.get_sub_domain()
sleep(1)
2021-03-03 03:48:32 +00:00
raise TooManyRequests(f"Too many retries: {tries}")
def login(self):
2021-03-03 03:48:32 +00:00
# Setting attribute here as initialize() will reset it
if hasattr(self, "token"):
logger.debug("Token already met. Skipping logging")
return True
root = self.api_request(
2021-03-03 03:48:32 +00:00
func_name="logIn",
params=(
"<username></username>"
"<password></password>"
"<AppID>BSPlayer v2.67</AppID>"
),
)
2021-03-03 03:48:32 +00:00
res = root.find(".//return")
# avoid AttributeError
if not res:
return False
if res.find("status").text == "OK":
self.token = res.find("data").text
logger.debug("Logged In Successfully.")
return True
return False
def logout(self):
# If already logged out / not logged in
2021-03-03 03:48:32 +00:00
# if not self.token:
# return True
if not hasattr(self, "token"):
logger.debug("Already logged out")
return True
root = self.api_request(
2021-03-03 03:48:32 +00:00
func_name="logOut",
params="<handle>{token}</handle>".format(token=self.token),
)
2021-03-03 03:48:32 +00:00
res = root.find(".//return")
self.token = None
2021-03-03 03:48:32 +00:00
# avoid AttributeError
if not res:
logger.debug("Root logout returned None")
return False
if res.find("status").text == "OK":
logger.debug("Logged Out Successfully.")
return True
2021-03-03 03:48:32 +00:00
return False
def query(self, video, video_hash, language):
if not self.login():
2021-03-03 03:48:32 +00:00
logger.debug("Token not found. Can't perform query")
return []
if isinstance(language, (tuple, list, set)):
# language_ids = ",".join(language)
# language_ids = 'spa'
2021-03-03 03:48:32 +00:00
language_ids = ",".join(sorted(l.opensubtitles for l in language))
if video.imdb_id is None:
2021-03-03 03:48:32 +00:00
imdbId = "*"
else:
imdbId = video.imdb_id
sleep(self.SEARCH_THROTTLE)
root = self.api_request(
2021-03-03 03:48:32 +00:00
func_name="searchSubtitles",
params=(
2021-03-03 03:48:32 +00:00
"<handle>{token}</handle>"
"<movieHash>{movie_hash}</movieHash>"
"<movieSize>{movie_size}</movieSize>"
"<languageId>{language_ids}</languageId>"
"<imdbId>{imdbId}</imdbId>"
).format(
token=self.token,
movie_hash=video_hash,
movie_size=video.size,
language_ids=language_ids,
imdbId=imdbId,
),
)
2021-03-03 03:48:32 +00:00
res = root.find(".//return/result")
if not res:
logger.debug("No subtitles found")
return []
2021-03-03 03:48:32 +00:00
status = res.find("status").text
if status != "OK":
logger.debug(f"No subtitles found (bad status: {status})")
return []
items = root.findall(".//return/data/item")
subtitles = []
if items:
2021-03-03 03:48:32 +00:00
logger.debug("Subtitles Found.")
for item in items:
2021-03-03 03:48:32 +00:00
subID = item.find("subID").text
subDownloadLink = item.find("subDownloadLink").text
subLang = Language.fromopensubtitles(item.find("subLang").text)
subName = item.find("subName").text
subFormat = item.find("subFormat").text
subtitles.append(
2021-03-03 03:48:32 +00:00
BSPlayerSubtitle(
subLang, subName, subFormat, video, subDownloadLink, subID
)
)
return subtitles
def list_subtitles(self, video, languages):
2021-03-03 03:48:32 +00:00
return self.query(video, video.hashes["bsplayer"], languages)
def get_sub_domain(self):
2021-03-03 03:48:32 +00:00
# API_URL_TEMPLATE = None
# session = Session()
# s1-9, s101-109
2021-03-03 03:48:32 +00:00
# Don't test again
# fixme: Setting attribute here as initialize() may reset it (maybe
# there's a more elegant way?)
if hasattr(self, "API_URL_TEMPLATE"):
logger.debug(f"Working subdomain already met: {self.API_URL_TEMPLATE}")
return self.API_URL_TEMPLATE
else:
self.API_URL_TEMPLATE = None
# fmt: off
2020-03-13 15:21:20 +00:00
SUB_DOMAINS = ['s1', 's2', 's3', 's4', 's5', 's6', 's7', 's8',
's101', 's102', 's103', 's104', 's105', 's106', 's107', 's108', 's109']
2021-03-03 03:48:32 +00:00
# fmt: on
2021-01-23 04:10:04 +00:00
random.shuffle(SUB_DOMAINS)
2021-03-03 03:48:32 +00:00
# Limit to 8 tests
for domain in SUB_DOMAINS[:8]:
2021-01-23 04:10:04 +00:00
TEST_URL = "http://{}.api.bsplayer-subtitles.com".format(domain)
try:
2021-03-03 03:48:32 +00:00
logging.debug("Testing BSplayer sub-domain {}".format(TEST_URL))
res = self.session.get(TEST_URL, timeout=10)
2021-01-23 04:10:04 +00:00
except:
continue
else:
res.raise_for_status()
if res.status_code == 200:
2021-03-03 03:48:32 +00:00
logger.debug(f"Found working subdomain: {domain}")
self.API_URL_TEMPLATE = (
"http://{}.api.bsplayer-subtitles.com/v1.php".format(domain)
)
2021-01-23 04:10:04 +00:00
break
else:
sleep(5)
continue
2021-03-03 03:48:32 +00:00
if self.API_URL_TEMPLATE:
return self.API_URL_TEMPLATE
raise ServiceUnavailable("No API URL template was found")
def download_subtitle(self, subtitle):
2021-03-03 03:48:32 +00:00
# session = Session()
_addheaders = {"User-Agent": "Mozilla/4.0 (compatible; Synapse)"}
self.session.headers.update(_addheaders)
res = self.session.get(subtitle.page_link)
if res:
2021-03-03 03:48:32 +00:00
if res.text == "500":
raise ServiceUnavailable("Error 500 on server")
with gzip.GzipFile(fileobj=io.BytesIO(res.content)) as gf:
subtitle.content = gf.read()
subtitle.normalize()
return subtitle
2021-03-03 03:48:32 +00:00
raise ServiceUnavailable("Problems conecting to the server")