2020-01-07 11:23:17 +00:00
|
|
|
# -*- 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
|
|
|
|
|
2020-01-07 11:23:17 +00:00
|
|
|
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
|
2020-01-07 11:23:17 +00:00
|
|
|
from subliminal.subtitle import guess_matches
|
2020-02-23 16:37:02 +00:00
|
|
|
from subliminal.video import Episode, Movie
|
2020-01-07 11:23:17 +00:00
|
|
|
from subzero.language import Language
|
2021-01-23 04:10:04 +00:00
|
|
|
from subliminal.exceptions import ServiceUnavailable
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
import gzip
|
|
|
|
import random
|
|
|
|
from time import sleep
|
|
|
|
from xml.etree import ElementTree
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2020-02-16 04:53:32 +00:00
|
|
|
|
2020-01-07 11:23:17 +00:00
|
|
|
class BSPlayerSubtitle(Subtitle):
|
|
|
|
"""BSPlayer Subtitle."""
|
2021-03-03 03:48:32 +00:00
|
|
|
provider_name = "bsplayer"
|
2020-02-16 04:53:32 +00:00
|
|
|
hash_verifiable = True
|
2020-01-07 11:23:17 +00:00
|
|
|
|
2020-11-04 20:29:34 +00:00
|
|
|
def __init__(self, language, filename, subtype, video, link, subid):
|
2020-01-07 11:23:17 +00:00
|
|
|
super(BSPlayerSubtitle, self).__init__(language)
|
|
|
|
self.language = language
|
|
|
|
self.filename = filename
|
|
|
|
self.page_link = link
|
|
|
|
self.subtype = subtype
|
|
|
|
self.video = video
|
2020-11-04 20:29:34 +00:00
|
|
|
self.subid = subid
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
2020-11-04 20:29:34 +00:00
|
|
|
return self.subid
|
2020-01-07 11:23:17 +00:00
|
|
|
|
2020-01-18 11:59:14 +00:00
|
|
|
@property
|
|
|
|
def release_info(self):
|
|
|
|
return self.filename
|
|
|
|
|
2020-01-07 11:23:17 +00:00
|
|
|
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")
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
|
|
class BSPlayerProvider(Provider):
|
|
|
|
"""BSPlayer Provider."""
|
2021-03-03 03:48:32 +00:00
|
|
|
|
|
|
|
# fmt: off
|
2020-01-07 11:23:17 +00:00
|
|
|
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
|
2020-02-16 04:53:32 +00:00
|
|
|
hash_verifiable = True
|
2021-03-03 03:48:32 +00:00
|
|
|
# fmt: on
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
# 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)
|
|
|
|
|
2020-01-07 11:23:17 +00:00
|
|
|
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):
|
2020-01-07 11:23:17 +00:00
|
|
|
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
|
|
|
|
),
|
2020-01-07 11:23:17 +00:00
|
|
|
}
|
|
|
|
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>"
|
2020-01-07 11:23:17 +00:00
|
|
|
).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)
|
2020-01-07 11:23:17 +00:00
|
|
|
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())
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
except Exception as ex:
|
2021-03-03 03:48:32 +00:00
|
|
|
logger.error(f"Exception parsing response: {ex}")
|
|
|
|
if func_name == "logIn":
|
2020-01-07 11:23:17 +00:00
|
|
|
self.search_url = self.get_sub_domain()
|
2020-02-16 04:53:32 +00:00
|
|
|
|
2020-01-07 11:23:17 +00:00
|
|
|
sleep(1)
|
2021-03-03 03:48:32 +00:00
|
|
|
|
|
|
|
raise TooManyRequests(f"Too many retries: {tries}")
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
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")
|
2020-01-07 11:23:17 +00:00
|
|
|
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>"
|
|
|
|
),
|
2020-01-07 11:23:17 +00:00
|
|
|
)
|
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.")
|
2020-01-07 11:23:17 +00:00
|
|
|
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")
|
2020-01-07 11:23:17 +00:00
|
|
|
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),
|
2020-01-07 11:23:17 +00:00
|
|
|
)
|
2021-03-03 03:48:32 +00:00
|
|
|
res = root.find(".//return")
|
2020-01-07 11:23:17 +00:00
|
|
|
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.")
|
2020-01-07 11:23:17 +00:00
|
|
|
return True
|
2021-03-03 03:48:32 +00:00
|
|
|
|
2020-01-07 11:23:17 +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")
|
2020-01-07 11:23:17 +00:00
|
|
|
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))
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
if video.imdb_id is None:
|
2021-03-03 03:48:32 +00:00
|
|
|
imdbId = "*"
|
2020-01-07 11:23:17 +00:00
|
|
|
else:
|
|
|
|
imdbId = video.imdb_id
|
|
|
|
sleep(self.SEARCH_THROTTLE)
|
|
|
|
root = self.api_request(
|
2021-03-03 03:48:32 +00:00
|
|
|
func_name="searchSubtitles",
|
2020-01-07 11:23:17 +00:00
|
|
|
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,
|
|
|
|
),
|
2020-01-07 11:23:17 +00:00
|
|
|
)
|
2021-03-03 03:48:32 +00:00
|
|
|
res = root.find(".//return/result")
|
|
|
|
|
|
|
|
if not res:
|
|
|
|
logger.debug("No subtitles found")
|
2020-01-07 11:23:17 +00:00
|
|
|
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")
|
2020-01-07 11:23:17 +00:00
|
|
|
subtitles = []
|
|
|
|
if items:
|
2021-03-03 03:48:32 +00:00
|
|
|
logger.debug("Subtitles Found.")
|
2020-01-07 11:23:17 +00:00
|
|
|
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
|
2020-01-07 11:23:17 +00:00
|
|
|
subtitles.append(
|
2021-03-03 03:48:32 +00:00
|
|
|
BSPlayerSubtitle(
|
|
|
|
subLang, subName, subFormat, video, subDownloadLink, subID
|
|
|
|
)
|
2020-01-07 11:23:17 +00:00
|
|
|
)
|
|
|
|
return subtitles
|
|
|
|
|
|
|
|
def list_subtitles(self, video, languages):
|
2021-03-03 03:48:32 +00:00
|
|
|
return self.query(video, video.hashes["bsplayer"], languages)
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
def get_sub_domain(self):
|
2021-03-03 03:48:32 +00:00
|
|
|
# API_URL_TEMPLATE = None
|
|
|
|
# session = Session()
|
2020-02-16 04:53:32 +00:00
|
|
|
# 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',
|
2020-02-16 04:53:32 +00:00
|
|
|
'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")
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
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)
|
2020-01-07 11:23:17 +00:00
|
|
|
if res:
|
2021-03-03 03:48:32 +00:00
|
|
|
if res.text == "500":
|
|
|
|
raise ServiceUnavailable("Error 500 on server")
|
2020-01-07 11:23:17 +00:00
|
|
|
|
|
|
|
with gzip.GzipFile(fileobj=io.BytesIO(res.content)) as gf:
|
2020-02-16 04:53:32 +00:00
|
|
|
subtitle.content = gf.read()
|
2020-01-07 11:23:17 +00:00
|
|
|
subtitle.normalize()
|
|
|
|
|
|
|
|
return subtitle
|
2021-03-03 03:48:32 +00:00
|
|
|
raise ServiceUnavailable("Problems conecting to the server")
|