2019-02-24 17:41:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-09-17 02:04:27 +00:00
|
|
|
from __future__ import absolute_import
|
2019-02-24 17:41:22 +00:00
|
|
|
import io
|
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
|
|
|
|
import rarfile
|
|
|
|
import re
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
from subzero.language import Language
|
|
|
|
from guessit import guessit
|
|
|
|
from requests import Session
|
|
|
|
|
|
|
|
from subliminal.providers import ParserBeautifulSoup, Provider
|
|
|
|
from subliminal import __short_version__
|
|
|
|
from subliminal.cache import SHOW_EXPIRATION_TIME, region
|
|
|
|
from subliminal.score import get_equivalent_release_groups
|
|
|
|
from subliminal.subtitle import SUBTITLE_EXTENSIONS, Subtitle, fix_line_ending, guess_matches
|
|
|
|
from subliminal.utils import sanitize, sanitize_release_group
|
|
|
|
from subliminal.video import Movie
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
year_re = re.compile(r'^\((\d{4})\)$')
|
|
|
|
|
|
|
|
|
|
|
|
class Subs4FreeSubtitle(Subtitle):
|
|
|
|
"""Subs4Free Subtitle."""
|
|
|
|
provider_name = 'subs4free'
|
|
|
|
|
|
|
|
def __init__(self, language, page_link, title, year, version, download_link):
|
|
|
|
super(Subs4FreeSubtitle, self).__init__(language, page_link=page_link)
|
|
|
|
self.title = title
|
|
|
|
self.year = year
|
|
|
|
self.version = version
|
|
|
|
self.download_link = download_link
|
|
|
|
self.hearing_impaired = None
|
|
|
|
self.encoding = 'utf8'
|
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self.download_link
|
|
|
|
|
|
|
|
def get_matches(self, video):
|
|
|
|
matches = set()
|
|
|
|
|
|
|
|
# movie
|
|
|
|
if isinstance(video, Movie):
|
|
|
|
# title
|
|
|
|
if video.title and (sanitize(self.title) in (
|
|
|
|
sanitize(name) for name in [video.title] + video.alternative_titles)):
|
|
|
|
matches.add('title')
|
|
|
|
# year
|
|
|
|
if video.year and self.year == video.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': 'movie'}), partial=True)
|
|
|
|
|
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
|
|
class Subs4FreeProvider(Provider):
|
|
|
|
"""Subs4Free Provider."""
|
|
|
|
languages = {Language(l) for l in ['ell', 'eng']}
|
|
|
|
video_types = (Movie,)
|
|
|
|
server_url = 'https://www.sf4-industry.com'
|
|
|
|
download_url = '/getSub.html'
|
|
|
|
search_url = '/search_report.php?search={}&searchType=1'
|
2019-07-14 00:29:05 +00:00
|
|
|
anti_block_1 = 'https://images.subs4free.info/favicon.ico'
|
|
|
|
anti_block_2 = 'https://www.subs4series.com/includes/anti-block-layover.php?launch=1'
|
|
|
|
anti_block_3 = 'https://www.subs4series.com/includes/anti-block.php'
|
2019-02-24 17:41:22 +00:00
|
|
|
subtitle_class = Subs4FreeSubtitle
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.session = None
|
|
|
|
|
|
|
|
def initialize(self):
|
|
|
|
self.session = Session()
|
2020-03-13 08:12:20 +00:00
|
|
|
from .utils import FIRST_THOUSAND_OR_SO_USER_AGENTS as AGENT_LIST
|
|
|
|
self.session.headers['User-Agent'] = AGENT_LIST[random.randint(0, len(AGENT_LIST) - 1)]
|
2019-02-24 17:41:22 +00:00
|
|
|
|
|
|
|
def terminate(self):
|
|
|
|
self.session.close()
|
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
def get_show_links(self, title, year=None):
|
|
|
|
"""Get the matching show links for `title` and `year`.
|
2019-02-24 17:41:22 +00:00
|
|
|
|
|
|
|
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
|
2019-07-14 00:29:05 +00:00
|
|
|
:return: the show links, if found.
|
|
|
|
:rtype: list of str
|
2019-02-24 17:41:22 +00:00
|
|
|
|
|
|
|
"""
|
2019-07-14 00:29:05 +00:00
|
|
|
title = sanitize(title)
|
|
|
|
suggestions = self._get_suggestions(title)
|
|
|
|
|
|
|
|
show_links = []
|
|
|
|
for suggestion in suggestions:
|
|
|
|
show_title = sanitize(suggestion['title'])
|
2019-02-24 17:41:22 +00:00
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
if show_title == title or (year and show_title == '{title} {year:d}'.format(title=title, year=year)):
|
|
|
|
logger.debug('Getting show id')
|
|
|
|
show_links.append(suggestion['link'].split('?p=')[-1])
|
2019-02-24 17:41:22 +00:00
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
return show_links
|
2019-02-24 17:41:22 +00:00
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
@region.cache_on_arguments(expiration_time=SHOW_EXPIRATION_TIME, should_cache_fn=lambda value: value)
|
2019-02-24 17:41:22 +00:00
|
|
|
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.
|
2019-07-14 00:29:05 +00:00
|
|
|
:rtype: list of dict
|
2019-02-24 17:41:22 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
# make the search
|
|
|
|
logger.info('Searching show ids with %r', title)
|
2019-07-14 00:29:05 +00:00
|
|
|
r = self.session.get(self.server_url + self.search_url.format(title),
|
2019-02-24 17:41:22 +00:00
|
|
|
headers={'Referer': self.server_url}, timeout=10)
|
|
|
|
r.raise_for_status()
|
|
|
|
|
|
|
|
if not r.content:
|
|
|
|
logger.debug('No data returned from provider')
|
2019-07-14 00:29:05 +00:00
|
|
|
return []
|
2019-02-24 17:41:22 +00:00
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
soup = ParserBeautifulSoup(r.content, ['html.parser'])
|
2019-02-24 17:41:22 +00:00
|
|
|
suggestions = [{'link': l.attrs['value'], 'title': l.text}
|
|
|
|
for l in soup.select('select[name="Mov_sel"] > option[value]')]
|
|
|
|
logger.debug('Found suggestions: %r', suggestions)
|
|
|
|
|
|
|
|
return suggestions
|
|
|
|
|
|
|
|
def query(self, movie_id, title, year):
|
|
|
|
# get the season list of the show
|
|
|
|
logger.info('Getting the subtitle list of show id %s', movie_id)
|
|
|
|
if movie_id:
|
|
|
|
page_link = self.server_url + '/' + movie_id
|
|
|
|
else:
|
2019-07-14 00:29:05 +00:00
|
|
|
page_link = self.server_url + self.search_url.format(' '.join([title, str(year)]))
|
2019-02-24 17:41:22 +00:00
|
|
|
|
|
|
|
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, ['html.parser'])
|
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
year = None
|
2019-02-24 17:41:22 +00:00
|
|
|
year_element = soup.select_one('td#dates_header > table div')
|
|
|
|
matches = False
|
|
|
|
if year_element:
|
|
|
|
matches = year_re.match(str(year_element.contents[2]).strip())
|
|
|
|
if matches:
|
2019-07-14 00:29:05 +00:00
|
|
|
year = int(matches.group(1))
|
2019-02-24 17:41:22 +00:00
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
title_tag = soup.select_one('td#dates_header > table u')
|
|
|
|
show_title = str(title_tag.contents[0]).strip() if title_tag else None
|
2019-02-24 17:41:22 +00:00
|
|
|
|
|
|
|
subtitles = []
|
|
|
|
# loop over episode rows
|
2019-07-14 00:29:05 +00:00
|
|
|
for subs_tag in soup.select('table .seeDark,.seeMedium'):
|
2019-02-24 17:41:22 +00:00
|
|
|
# read common info
|
2019-07-14 00:29:05 +00:00
|
|
|
version = subs_tag.find('b').text
|
|
|
|
download_link = self.server_url + subs_tag.find('a')['href']
|
|
|
|
language = Language.fromalpha2(subs_tag.find('img')['src'].split('/')[-1].split('.')[0])
|
2019-02-24 17:41:22 +00:00
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
subtitle = self.subtitle_class(language, page_link, show_title, year, version, download_link)
|
2019-02-24 17:41:22 +00:00
|
|
|
|
|
|
|
logger.debug('Found subtitle {!r}'.format(subtitle))
|
|
|
|
subtitles.append(subtitle)
|
|
|
|
|
|
|
|
return subtitles
|
|
|
|
|
|
|
|
def list_subtitles(self, video, languages):
|
|
|
|
# lookup show_id
|
|
|
|
titles = [video.title] + video.alternative_titles if isinstance(video, Movie) else []
|
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
show_links = None
|
2019-02-24 17:41:22 +00:00
|
|
|
for title in titles:
|
2019-07-14 00:29:05 +00:00
|
|
|
show_links = self.get_show_links(title, video.year)
|
|
|
|
if show_links:
|
2019-02-24 17:41:22 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
subtitles = []
|
|
|
|
# query for subtitles with the show_id
|
2019-07-14 00:29:05 +00:00
|
|
|
if show_links:
|
|
|
|
for show_link in show_links:
|
|
|
|
subtitles += [s for s in self.query(show_link, video.title, video.year) if s.language in languages]
|
2019-02-24 17:41:22 +00:00
|
|
|
else:
|
2019-07-14 00:29:05 +00:00
|
|
|
subtitles += [s for s in self.query(None, sanitize(video.title), video.year) if s.language in languages]
|
2019-02-24 17:41:22 +00:00
|
|
|
|
|
|
|
return subtitles
|
|
|
|
|
|
|
|
def download_subtitle(self, subtitle):
|
|
|
|
if isinstance(subtitle, Subs4FreeSubtitle):
|
|
|
|
# download the subtitle
|
|
|
|
logger.info('Downloading subtitle %r', subtitle)
|
|
|
|
r = self.session.get(subtitle.download_link, headers={'Referer': subtitle.page_link}, timeout=10)
|
|
|
|
r.raise_for_status()
|
|
|
|
|
|
|
|
if not r.content:
|
|
|
|
logger.debug('Unable to download subtitle. No data returned from provider')
|
|
|
|
return
|
|
|
|
|
|
|
|
soup = ParserBeautifulSoup(r.content, ['lxml', 'html.parser'])
|
|
|
|
download_element = soup.select_one('input[name="id"]')
|
|
|
|
image_element = soup.select_one('input[type="image"]')
|
|
|
|
subtitle_id = download_element['value'] if download_element else None
|
|
|
|
width = int(str(image_element['width']).strip('px')) if image_element else 0
|
|
|
|
height = int(str(image_element['height']).strip('px')) if image_element else 0
|
|
|
|
|
|
|
|
if not subtitle_id:
|
|
|
|
logger.debug('Unable to download subtitle. No download link found')
|
|
|
|
return
|
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
self.apply_anti_block(subtitle)
|
|
|
|
|
2019-02-24 17:41:22 +00:00
|
|
|
download_url = self.server_url + self.download_url
|
2019-07-14 00:29:05 +00:00
|
|
|
r = self.session.post(download_url, data={'id': subtitle_id, 'x': random.randint(0, width),
|
2019-02-24 17:41:22 +00:00
|
|
|
'y': random.randint(0, height)},
|
|
|
|
headers={'Referer': subtitle.download_link}, timeout=10)
|
|
|
|
r.raise_for_status()
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2019-07-14 00:29:05 +00:00
|
|
|
def apply_anti_block(self, subtitle):
|
|
|
|
self.session.get(self.anti_block_1, headers={'Referer': subtitle.download_link}, timeout=10)
|
|
|
|
self.session.get(self.anti_block_2, headers={'Referer': subtitle.download_link}, timeout=10)
|
|
|
|
self.session.get(self.anti_block_3, headers={'Referer': subtitle.download_link}, timeout=10)
|
|
|
|
|
2019-02-24 17:41:22 +00:00
|
|
|
|
|
|
|
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
|