|
|
|
@ -1,5 +1,4 @@
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
# encoding=utf8
|
|
|
|
|
import io
|
|
|
|
|
import logging
|
|
|
|
|
import os
|
|
|
|
@ -12,7 +11,6 @@ import zipfile
|
|
|
|
|
from subzero.language import Language
|
|
|
|
|
from guessit import guessit
|
|
|
|
|
from requests import Session
|
|
|
|
|
from six import text_type
|
|
|
|
|
|
|
|
|
|
from subliminal.providers import ParserBeautifulSoup, Provider
|
|
|
|
|
from subliminal import __short_version__
|
|
|
|
@ -75,6 +73,9 @@ class Subs4FreeProvider(Provider):
|
|
|
|
|
server_url = 'https://www.sf4-industry.com'
|
|
|
|
|
download_url = '/getSub.html'
|
|
|
|
|
search_url = '/search_report.php?search={}&searchType=1'
|
|
|
|
|
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'
|
|
|
|
|
subtitle_class = Subs4FreeSubtitle
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
@ -87,62 +88,51 @@ class Subs4FreeProvider(Provider):
|
|
|
|
|
def terminate(self):
|
|
|
|
|
self.session.close()
|
|
|
|
|
|
|
|
|
|
def get_show_ids(self, title, year=None):
|
|
|
|
|
"""Get the best matching show id for `series` and `year``.
|
|
|
|
|
def get_show_links(self, title, year=None):
|
|
|
|
|
"""Get the matching show links for `title` and `year`.
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
:return: the show id, if found.
|
|
|
|
|
:rtype: str
|
|
|
|
|
:return: the show links, if found.
|
|
|
|
|
:rtype: list of str
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
title_sanitized = sanitize(title).lower()
|
|
|
|
|
show_ids = self._get_suggestions(title)
|
|
|
|
|
|
|
|
|
|
matched_show_ids = []
|
|
|
|
|
for show in show_ids:
|
|
|
|
|
show_id = None
|
|
|
|
|
show_title = sanitize(show['title'])
|
|
|
|
|
# attempt with year
|
|
|
|
|
if not show_id and year:
|
|
|
|
|
logger.debug('Getting show id with year')
|
|
|
|
|
show_id = show['link'].split('?p=')[-1] if show_title == '{title} {year:d}'.format(
|
|
|
|
|
title=title_sanitized, year=year) else None
|
|
|
|
|
|
|
|
|
|
# attempt clean
|
|
|
|
|
if not show_id:
|
|
|
|
|
logger.debug('Getting show id')
|
|
|
|
|
show_id = show['link'].split('?p=')[-1] if show_title == title_sanitized else None
|
|
|
|
|
title = sanitize(title)
|
|
|
|
|
suggestions = self._get_suggestions(title)
|
|
|
|
|
|
|
|
|
|
show_links = []
|
|
|
|
|
for suggestion in suggestions:
|
|
|
|
|
show_title = sanitize(suggestion['title'])
|
|
|
|
|
|
|
|
|
|
if show_id:
|
|
|
|
|
matched_show_ids.append(show_id)
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
|
|
return matched_show_ids
|
|
|
|
|
return show_links
|
|
|
|
|
|
|
|
|
|
@region.cache_on_arguments(expiration_time=SHOW_EXPIRATION_TIME, to_str=text_type,
|
|
|
|
|
should_cache_fn=lambda value: value)
|
|
|
|
|
@region.cache_on_arguments(expiration_time=SHOW_EXPIRATION_TIME, should_cache_fn=lambda value: value)
|
|
|
|
|
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.
|
|
|
|
|
:rtype: dict
|
|
|
|
|
:rtype: list of dict
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
# make the search
|
|
|
|
|
logger.info('Searching show ids with %r', title)
|
|
|
|
|
r = self.session.get(self.server_url + text_type(self.search_url).format(title),
|
|
|
|
|
r = self.session.get(self.server_url + self.search_url.format(title),
|
|
|
|
|
headers={'Referer': self.server_url}, timeout=10)
|
|
|
|
|
r.raise_for_status()
|
|
|
|
|
|
|
|
|
|
if not r.content:
|
|
|
|
|
logger.debug('No data returned from provider')
|
|
|
|
|
return {}
|
|
|
|
|
return []
|
|
|
|
|
|
|
|
|
|
soup = ParserBeautifulSoup(r.content, ['lxml', 'html.parser'])
|
|
|
|
|
soup = ParserBeautifulSoup(r.content, ['html.parser'])
|
|
|
|
|
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)
|
|
|
|
@ -155,7 +145,7 @@ class Subs4FreeProvider(Provider):
|
|
|
|
|
if movie_id:
|
|
|
|
|
page_link = self.server_url + '/' + movie_id
|
|
|
|
|
else:
|
|
|
|
|
page_link = self.server_url + text_type(self.search_url).format(' '.join([title, str(year)]))
|
|
|
|
|
page_link = self.server_url + self.search_url.format(' '.join([title, str(year)]))
|
|
|
|
|
|
|
|
|
|
r = self.session.get(page_link, timeout=10)
|
|
|
|
|
r.raise_for_status()
|
|
|
|
@ -166,26 +156,26 @@ class Subs4FreeProvider(Provider):
|
|
|
|
|
|
|
|
|
|
soup = ParserBeautifulSoup(r.content, ['html.parser'])
|
|
|
|
|
|
|
|
|
|
year_num = None
|
|
|
|
|
year = None
|
|
|
|
|
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:
|
|
|
|
|
year_num = int(matches.group(1))
|
|
|
|
|
year = int(matches.group(1))
|
|
|
|
|
|
|
|
|
|
title_element = soup.select_one('td#dates_header > table u')
|
|
|
|
|
show_title = str(title_element.contents[0]).strip() if title_element else None
|
|
|
|
|
title_tag = soup.select_one('td#dates_header > table u')
|
|
|
|
|
show_title = str(title_tag.contents[0]).strip() if title_tag else None
|
|
|
|
|
|
|
|
|
|
subtitles = []
|
|
|
|
|
# loop over episode rows
|
|
|
|
|
for subtitle in soup.select('table.table_border div[align="center"] > div'):
|
|
|
|
|
for subs_tag in soup.select('table .seeDark,.seeMedium'):
|
|
|
|
|
# read common info
|
|
|
|
|
version = subtitle.find('b').text
|
|
|
|
|
download_link = self.server_url + subtitle.find('a')['href']
|
|
|
|
|
language = Language.fromalpha2(subtitle.find('img')['src'].split('/')[-1].split('.')[0])
|
|
|
|
|
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])
|
|
|
|
|
|
|
|
|
|
subtitle = self.subtitle_class(language, page_link, show_title, year_num, version, download_link)
|
|
|
|
|
subtitle = self.subtitle_class(language, page_link, show_title, year, version, download_link)
|
|
|
|
|
|
|
|
|
|
logger.debug('Found subtitle {!r}'.format(subtitle))
|
|
|
|
|
subtitles.append(subtitle)
|
|
|
|
@ -196,19 +186,19 @@ class Subs4FreeProvider(Provider):
|
|
|
|
|
# lookup show_id
|
|
|
|
|
titles = [video.title] + video.alternative_titles if isinstance(video, Movie) else []
|
|
|
|
|
|
|
|
|
|
show_ids = None
|
|
|
|
|
show_links = None
|
|
|
|
|
for title in titles:
|
|
|
|
|
show_ids = self.get_show_ids(title, video.year)
|
|
|
|
|
if show_ids and len(show_ids) > 0:
|
|
|
|
|
show_links = self.get_show_links(title, video.year)
|
|
|
|
|
if show_links:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
subtitles = []
|
|
|
|
|
# query for subtitles with the show_id
|
|
|
|
|
if show_ids and len(show_ids) > 0:
|
|
|
|
|
for show_id in show_ids:
|
|
|
|
|
subtitles += [s for s in self.query(show_id, video.title, video.year) if s.language in languages]
|
|
|
|
|
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]
|
|
|
|
|
else:
|
|
|
|
|
subtitles += [s for s in self.query(None, video.title, video.year) if s.language in languages]
|
|
|
|
|
subtitles += [s for s in self.query(None, sanitize(video.title), video.year) if s.language in languages]
|
|
|
|
|
|
|
|
|
|
return subtitles
|
|
|
|
|
|
|
|
|
@ -234,8 +224,10 @@ class Subs4FreeProvider(Provider):
|
|
|
|
|
logger.debug('Unable to download subtitle. No download link found')
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.apply_anti_block(subtitle)
|
|
|
|
|
|
|
|
|
|
download_url = self.server_url + self.download_url
|
|
|
|
|
r = self.session.post(download_url, data={'utf8': 1, 'id': subtitle_id, 'x': random.randint(0, width),
|
|
|
|
|
r = self.session.post(download_url, data={'id': subtitle_id, 'x': random.randint(0, width),
|
|
|
|
|
'y': random.randint(0, height)},
|
|
|
|
|
headers={'Referer': subtitle.download_link}, timeout=10)
|
|
|
|
|
r.raise_for_status()
|
|
|
|
@ -253,6 +245,11 @@ class Subs4FreeProvider(Provider):
|
|
|
|
|
else:
|
|
|
|
|
logger.debug('Could not extract subtitle from %r', archive)
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_archive(content):
|
|
|
|
|
# open the archive
|
|
|
|
|