From 05aaf8094d63f43e56ede00e9dbf75c670ba5164 Mon Sep 17 00:00:00 2001 From: panni Date: Fri, 21 Jun 2019 04:52:27 +0200 Subject: [PATCH] core: update to subliminal_patch:head; subscene fallback for non-year results; detect downtime (raise ServiceUnavailable) --- libs/subliminal_patch/providers/subscene.py | 6 +++- libs/subscene_api/subscene.py | 33 ++++++++++++++------- 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/libs/subliminal_patch/providers/subscene.py b/libs/subliminal_patch/providers/subscene.py index a5cab3fd8..af5c40d83 100644 --- a/libs/subliminal_patch/providers/subscene.py +++ b/libs/subliminal_patch/providers/subscene.py @@ -19,7 +19,7 @@ from babelfish import language_converters from guessit import guessit from dogpile.cache.api import NO_VALUE from subliminal import Episode, ProviderError -from subliminal.exceptions import ConfigurationError +from subliminal.exceptions import ConfigurationError, ServiceUnavailable from subliminal.utils import sanitize_release_group from subliminal.cache import region from subliminal_patch.http import RetryingCFSession @@ -141,6 +141,10 @@ class SubsceneProvider(Provider, ProviderSubtitleArchiveMixin): def login(self): r = self.session.get("https://subscene.com/account/login") + if "Server Error" in r.content: + logger.error("Login unavailable; Maintenance?") + raise ServiceUnavailable("Login unavailable; Maintenance?") + match = re.search(r"", r.content) if match: diff --git a/libs/subscene_api/subscene.py b/libs/subscene_api/subscene.py index 39064f744..07d1eda3e 100644 --- a/libs/subscene_api/subscene.py +++ b/libs/subscene_api/subscene.py @@ -255,26 +255,39 @@ def get_first_film(soup, section, year=None, session=None): url = SITE_DOMAIN + t.div.a.get("href") break if not url: - return + # fallback to non-year results + logger.info("Falling back to non-year results as year wasn't found (%s)", year) + url = SITE_DOMAIN + tag.findNext("ul").find("li").div.a.get("href") return Film.from_url(url, session=session) +def find_endpoint(session, content=None): + endpoint = region.get("subscene_endpoint2") + if endpoint is NO_VALUE: + if not content: + content = session.get(SITE_DOMAIN).text + + m = ENDPOINT_RE.search(content) + if m: + endpoint = m.group(1).strip() + logger.debug("Switching main endpoint to %s", endpoint) + region.set("subscene_endpoint2", endpoint) + return endpoint + + def search(term, release=True, session=None, year=None, limit_to=SearchTypes.Exact, throttle=0): # note to subscene: if you actually start to randomize the endpoint, we'll have to query your server even more if release: endpoint = "release" else: - endpoint = region.get("subscene_endpoint2") - if endpoint is NO_VALUE: - ret = session.get(SITE_DOMAIN) - time.sleep(throttle) - m = ENDPOINT_RE.search(ret.text) - if m: - endpoint = m.group(1).strip() - logger.debug("Switching main endpoint to %s", endpoint) - region.set("subscene_endpoint2", endpoint) + endpoint = find_endpoint(session) + time.sleep(throttle) + + if not endpoint: + logger.error("Couldn't find endpoint, exiting") + return soup = soup_for("%s/subtitles/%s" % (SITE_DOMAIN, endpoint), data={"query": term}, session=session)