You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
311 lines
12 KiB
311 lines
12 KiB
6 years ago
|
# -*- coding: utf-8 -*-
|
||
5 years ago
|
from __future__ import absolute_import
|
||
6 years ago
|
import io
|
||
|
import logging
|
||
|
import os
|
||
|
|
||
|
import rarfile
|
||
|
import re
|
||
|
import zipfile
|
||
|
|
||
|
from subzero.language import Language
|
||
|
from guessit import guessit
|
||
4 years ago
|
from requests import Session
|
||
6 years ago
|
|
||
|
from subliminal.providers import ParserBeautifulSoup, Provider
|
||
|
from subliminal.cache import SHOW_EXPIRATION_TIME, region
|
||
4 years ago
|
from dogpile.cache.api import NO_VALUE
|
||
6 years ago
|
from subliminal.score import get_equivalent_release_groups
|
||
4 years ago
|
from subliminal.subtitle import SUBTITLE_EXTENSIONS, Subtitle, fix_line_ending
|
||
6 years ago
|
from subliminal.utils import sanitize, sanitize_release_group
|
||
|
from subliminal.video import Episode
|
||
4 years ago
|
from subliminal_patch.http import RetryingCFSession
|
||
|
from subliminal_patch.pitcher import pitchers, load_verification, store_verification
|
||
4 years ago
|
from subliminal_patch.subtitle import guess_matches
|
||
6 years ago
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
year_re = re.compile(r'^\((\d{4})\)$')
|
||
|
|
||
|
|
||
|
class Subs4SeriesSubtitle(Subtitle):
|
||
|
"""Subs4Series Subtitle."""
|
||
|
provider_name = 'subs4series'
|
||
|
|
||
5 years ago
|
def __init__(self, language, page_link, series, year, version, download_link, uploader):
|
||
6 years ago
|
super(Subs4SeriesSubtitle, self).__init__(language, page_link=page_link)
|
||
|
self.series = series
|
||
|
self.year = year
|
||
|
self.version = version
|
||
5 years ago
|
self.release_info = version
|
||
6 years ago
|
self.download_link = download_link
|
||
5 years ago
|
self.uploader = uploader
|
||
6 years ago
|
self.hearing_impaired = None
|
||
|
self.encoding = 'windows-1253'
|
||
|
|
||
|
@property
|
||
|
def id(self):
|
||
|
return self.download_link
|
||
|
|
||
|
def get_matches(self, video):
|
||
|
matches = set()
|
||
|
|
||
|
# episode
|
||
|
if isinstance(video, Episode):
|
||
|
# series name
|
||
|
if video.series and sanitize(self.series) in (
|
||
|
sanitize(name) for name in [video.series] + video.alternative_series):
|
||
|
matches.add('series')
|
||
|
# year
|
||
|
if video.original_series and self.year is None or video.year and video.year == self.year:
|
||
|
matches.add('year')
|
||
|
|
||
|
# release_group
|
||
|
if (video.release_group and self.version and
|
||
|
any(r in sanitize_release_group(self.version)
|
||
|
for r in get_equivalent_release_groups(sanitize_release_group(video.release_group)))):
|
||
|
matches.add('release_group')
|
||
|
# other properties
|
||
|
matches |= guess_matches(video, guessit(self.version, {'type': 'episode'}), partial=True)
|
||
|
|
||
|
return matches
|
||
|
|
||
|
|
||
|
class Subs4SeriesProvider(Provider):
|
||
|
"""Subs4Series Provider."""
|
||
|
languages = {Language(l) for l in ['ell', 'eng']}
|
||
|
video_types = (Episode,)
|
||
|
server_url = 'https://www.subs4series.com'
|
||
|
search_url = '/search_report.php?search={}&searchType=1'
|
||
|
episode_link = '/tv-series/{show_id}/season-{season:d}/episode-{episode:d}'
|
||
6 years ago
|
anti_block_1 = '/includes/anti-block-layover.php?launch=1'
|
||
|
anti_block_2 = '/includes/anti-block.php'
|
||
6 years ago
|
subtitle_class = Subs4SeriesSubtitle
|
||
|
|
||
|
def __init__(self):
|
||
|
self.session = None
|
||
4 years ago
|
self.captcha_session = None
|
||
6 years ago
|
|
||
|
def initialize(self):
|
||
4 years ago
|
self.session = RetryingCFSession()
|
||
5 years ago
|
self.session.headers['User-Agent'] = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, ' \
|
||
|
'like Gecko) Chrome/83.0.4103.116 Safari/537.36'
|
||
|
# We don't use FIRST_THOUSAND_OR_SO_USER_AGENTS list because it includes mobile browser that get redirected to
|
||
|
# mobile website
|
||
6 years ago
|
|
||
|
def terminate(self):
|
||
|
self.session.close()
|
||
|
|
||
6 years ago
|
def get_show_links(self, title, year=None):
|
||
|
"""Get the matching show links for `title` and `year`.
|
||
6 years ago
|
|
||
|
First search in the result of :meth:`_get_show_suggestions`.
|
||
|
|
||
|
:param title: show title.
|
||
|
:param year: year of the show, if any.
|
||
|
:type year: int
|
||
6 years ago
|
:return: the show links, if found.
|
||
|
:rtype: list of str
|
||
6 years ago
|
|
||
|
"""
|
||
6 years ago
|
title = sanitize(title)
|
||
|
suggestions = self._get_suggestions(title)
|
||
|
|
||
|
show_links = []
|
||
|
for suggestion in suggestions:
|
||
|
show_title = sanitize(suggestion['title'])
|
||
|
|
||
|
if show_title == title or (year and show_title == '{title} {year:d}'.format(title=title, year=year)):
|
||
|
logger.debug('Getting show link')
|
||
|
show_links.append('/'.join(suggestion['link'].rsplit('/', 2)[1:]))
|
||
|
|
||
|
return show_links
|
||
|
|
||
|
@region.cache_on_arguments(expiration_time=SHOW_EXPIRATION_TIME, should_cache_fn=lambda value: value)
|
||
6 years ago
|
def _get_suggestions(self, title):
|
||
|
"""Search the show or movie id from the `title` and `year`.
|
||
|
|
||
|
:param str title: title of the show.
|
||
|
:return: the show suggestions found.
|
||
6 years ago
|
:rtype: list of dict
|
||
6 years ago
|
|
||
|
"""
|
||
|
# make the search
|
||
|
logger.info('Searching show ids with %r', title)
|
||
6 years ago
|
r = self.session.get(self.server_url + self.search_url.format(title),
|
||
6 years ago
|
headers={'Referer': self.server_url}, timeout=10)
|
||
|
r.raise_for_status()
|
||
|
|
||
|
if not r.content:
|
||
|
logger.debug('No data returned from provider')
|
||
6 years ago
|
return []
|
||
6 years ago
|
|
||
6 years ago
|
soup = ParserBeautifulSoup(r.content, ['html.parser'])
|
||
6 years ago
|
series = [{'link': l.attrs['value'], 'title': l.text}
|
||
|
for l in soup.select('select[name="Mov_sel"] > option[value]')]
|
||
|
logger.debug('Found suggestions: %r', series)
|
||
|
|
||
|
return series
|
||
|
|
||
|
def query(self, show_id, series, season, episode, title):
|
||
|
# get the season list of the show
|
||
|
logger.info('Getting the subtitle list of show id %s', show_id)
|
||
|
if all((show_id, season, episode)):
|
||
|
page_link = self.server_url + self.episode_link.format(show_id=show_id, season=season, episode=episode)
|
||
|
else:
|
||
|
return []
|
||
|
|
||
|
r = self.session.get(page_link, timeout=10)
|
||
|
r.raise_for_status()
|
||
|
|
||
|
if not r.content:
|
||
|
logger.debug('No data returned from provider')
|
||
|
return []
|
||
|
|
||
|
soup = ParserBeautifulSoup(r.content, ['lxml', 'html.parser'])
|
||
|
|
||
6 years ago
|
year = None
|
||
6 years ago
|
matches = year_re.match(str(soup.select_one('#dates_header_br > table div').contents[2]).strip())
|
||
|
if matches:
|
||
6 years ago
|
year = int(matches.group(1))
|
||
|
show_title = str(soup.select_one('#dates_header_br > table div u').string).strip()
|
||
6 years ago
|
|
||
|
subtitles = []
|
||
|
# loop over episode rows
|
||
6 years ago
|
for subs_tag in soup.select('table .seeDark,.seeMedium'):
|
||
6 years ago
|
# read common info
|
||
5 years ago
|
version = subs_tag.find_all('b')[0].text
|
||
6 years ago
|
download_link = self.server_url + subs_tag.find('a')['href']
|
||
5 years ago
|
uploader = subs_tag.find_all('b')[1].text
|
||
6 years ago
|
language = Language.fromalpha2(subs_tag.find('img')['src'].split('/')[-1].split('.')[0])
|
||
6 years ago
|
|
||
5 years ago
|
subtitle = self.subtitle_class(language, page_link, show_title, year, version, download_link, uploader)
|
||
6 years ago
|
|
||
|
logger.debug('Found subtitle %r', subtitle)
|
||
|
subtitles.append(subtitle)
|
||
|
|
||
|
return subtitles
|
||
|
|
||
|
def list_subtitles(self, video, languages):
|
||
|
# lookup show_id
|
||
|
titles = [video.series] + video.alternative_series if isinstance(video, Episode) else []
|
||
|
|
||
6 years ago
|
show_links = None
|
||
6 years ago
|
for title in titles:
|
||
6 years ago
|
show_links = self.get_show_links(title, video.year)
|
||
|
if show_links:
|
||
6 years ago
|
break
|
||
|
|
||
|
subtitles = []
|
||
|
# query for subtitles with the show_id
|
||
6 years ago
|
for show_link in show_links:
|
||
|
subtitles += [s for s in self.query(show_link, video.series, video.season, video.episode, video.title)
|
||
6 years ago
|
if s.language in languages]
|
||
|
|
||
|
return subtitles
|
||
|
|
||
|
def download_subtitle(self, subtitle):
|
||
|
if isinstance(subtitle, Subs4SeriesSubtitle):
|
||
|
# download the subtitle
|
||
|
logger.info('Downloading subtitle %r', subtitle)
|
||
4 years ago
|
data = {"my_recaptcha_challenge_field": "manual_challenge"}
|
||
|
tries = 0
|
||
|
while tries <= 3:
|
||
|
tries += 1
|
||
|
r = self.session.get(subtitle.download_link, headers={'Referer': subtitle.page_link}, timeout=10)
|
||
|
|
||
|
if "g-recaptcha" in r.text or "grecaptcha" in r.text:
|
||
|
logger.info('Subs4series: Solving captcha. This might take a couple of minutes, but should only '
|
||
|
'happen once every so often')
|
||
|
|
||
|
for g, s in (("g-recaptcha-response", r'g-recaptcha.+?data-sitekey=\"(.+?)\"'),
|
||
|
("recaptcha_response", r'grecaptcha.execute\(\'(.+?)\',')):
|
||
|
site_key = re.search(s, r.text).group(1)
|
||
|
if site_key:
|
||
|
break
|
||
|
if not site_key:
|
||
|
logger.error("Subs4series: Captcha site-key not found!")
|
||
|
return
|
||
|
|
||
|
pitcher = pitchers.get_pitcher()("Subs4series", subtitle.download_link, site_key,
|
||
|
user_agent=self.session.headers["User-Agent"],
|
||
|
cookies=self.session.cookies.get_dict(),
|
||
|
headers={'Referer': subtitle.page_link},
|
||
|
is_invisible=True)
|
||
|
|
||
|
result = pitcher.throw()
|
||
|
if not result:
|
||
|
if tries >= 3:
|
||
|
raise Exception("Subs4series: Couldn't solve captcha!")
|
||
|
logger.info("Subs4series: Couldn't solve captcha! Retrying")
|
||
|
continue
|
||
|
else:
|
||
|
data['g-recaptcha-response'] = result
|
||
|
logger.info("Subs4series: Captcha solved. Trying to download subtitles...")
|
||
|
break
|
||
6 years ago
|
|
||
|
soup = ParserBeautifulSoup(r.content, ['lxml', 'html.parser'])
|
||
|
download_element = soup.select_one('a.style55ws')
|
||
|
if not download_element:
|
||
|
download_element = soup.select_one('form[method="post"]')
|
||
|
target = download_element['action'] if download_element else None
|
||
|
else:
|
||
|
target = download_element['href']
|
||
|
|
||
|
if not target:
|
||
|
logger.debug('Unable to download subtitle. No download link found')
|
||
|
return
|
||
|
|
||
6 years ago
|
self.apply_anti_block(subtitle)
|
||
|
|
||
6 years ago
|
download_url = self.server_url + target
|
||
4 years ago
|
r = self.session.post(download_url, data, headers={'Referer': subtitle.download_link},
|
||
|
allow_redirects=True, timeout=10)
|
||
|
if r.status_code == 403:
|
||
|
raise Exception("Subs4series: captcha expired waiting to be solved.")
|
||
6 years ago
|
|
||
|
if not r.content:
|
||
|
logger.debug('Unable to download subtitle. No data returned from provider')
|
||
|
return
|
||
|
|
||
|
archive = _get_archive(r.content)
|
||
|
subtitle_content = _get_subtitle_from_archive(archive) if archive else r.content
|
||
|
|
||
|
if subtitle_content:
|
||
|
subtitle.content = fix_line_ending(subtitle_content)
|
||
|
else:
|
||
|
logger.debug('Could not extract subtitle from %r', archive)
|
||
|
|
||
6 years ago
|
def apply_anti_block(self, subtitle):
|
||
|
self.session.get(self.server_url + self.anti_block_1, headers={'Referer': subtitle.download_link}, timeout=10)
|
||
|
self.session.get(self.server_url + self.anti_block_2, headers={'Referer': subtitle.download_link}, timeout=10)
|
||
|
|
||
6 years ago
|
|
||
|
def _get_archive(content):
|
||
|
# open the archive
|
||
|
archive_stream = io.BytesIO(content)
|
||
|
archive = None
|
||
|
if rarfile.is_rarfile(archive_stream):
|
||
|
logger.debug('Identified rar archive')
|
||
|
archive = rarfile.RarFile(archive_stream)
|
||
|
elif zipfile.is_zipfile(archive_stream):
|
||
|
logger.debug('Identified zip archive')
|
||
|
archive = zipfile.ZipFile(archive_stream)
|
||
|
|
||
|
return archive
|
||
|
|
||
|
|
||
|
def _get_subtitle_from_archive(archive):
|
||
|
for name in archive.namelist():
|
||
|
# discard hidden files
|
||
|
if os.path.split(name)[-1].startswith('.'):
|
||
|
continue
|
||
|
|
||
|
# discard non-subtitle files
|
||
|
if not name.lower().endswith(SUBTITLE_EXTENSIONS):
|
||
|
continue
|
||
|
|
||
|
return archive.read(name)
|
||
|
|
||
|
return None
|