Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Subs4Series provider #948

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/api/providers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ Shooter
.. automodule:: subliminal.providers.shooter
:private-members:

Subs4Series
-----------
.. automodule:: subliminal.providers.subs4series
:private-members:

TheSubDB
--------
.. automodule:: subliminal.providers.thesubdb
Expand Down
1 change: 1 addition & 0 deletions docs/user/how_it_works.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ subtitles. Current supported providers are:
* OpenSubtitles
* Podnapisi
* Shooter
* Subs4Series
* TheSubDB
* TvSubtitles

Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def find_version(*file_paths):
'opensubtitles = subliminal.providers.opensubtitles:OpenSubtitlesProvider',
'podnapisi = subliminal.providers.podnapisi:PodnapisiProvider',
'shooter = subliminal.providers.shooter:ShooterProvider',
'subs4series = subliminal.providers.subs4series:Subs4SeriesProvider',
'thesubdb = subliminal.providers.thesubdb:TheSubDBProvider',
'tvsubtitles = subliminal.providers.tvsubtitles:TVsubtitlesProvider'
],
Expand Down
1 change: 1 addition & 0 deletions subliminal/extensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ def unregister(self, entry_point):
'opensubtitles = subliminal.providers.opensubtitles:OpenSubtitlesProvider',
'podnapisi = subliminal.providers.podnapisi:PodnapisiProvider',
'shooter = subliminal.providers.shooter:ShooterProvider',
'subs4series = subliminal.providers.subs4series:Subs4SeriesProvider',
'thesubdb = subliminal.providers.thesubdb:TheSubDBProvider',
'tvsubtitles = subliminal.providers.tvsubtitles:TVsubtitlesProvider'
])
Expand Down
272 changes: 272 additions & 0 deletions subliminal/providers/subs4series.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
# -*- coding: utf-8 -*-
import io
import logging
import os

import rarfile
import re
import zipfile

from babelfish import Language
from guessit import guessit
from requests import Session
from six import text_type

from . import ParserBeautifulSoup, Provider
from .. import __short_version__
from ..cache import SHOW_EXPIRATION_TIME, region
from ..score import get_equivalent_release_groups
from ..subtitle import SUBTITLE_EXTENSIONS, Subtitle, fix_line_ending, guess_matches
from ..utils import sanitize, sanitize_release_group
from ..video import Episode

logger = logging.getLogger(__name__)

year_re = re.compile(r'^\((\d{4})\)$')


class Subs4SeriesSubtitle(Subtitle):
"""Subs4Series Subtitle."""
provider_name = 'subs4series'

def __init__(self, language, page_link, series, year, version, download_link):
super(Subs4SeriesSubtitle, self).__init__(language, page_link=page_link)
self.series = series
self.year = year
self.version = version
self.download_link = download_link
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.fromalpha2(l) for l in {'el', 'en'}}
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}'
subtitle_class = Subs4SeriesSubtitle

def __init__(self):
self.session = None

def initialize(self):
self.session = Session()
self.session.headers['User-Agent'] = 'Subliminal/{}'.format(__short_version__)

def terminate(self):
self.session.close()

def get_show_ids(self, title, year=None):
"""Get the best matching show id for `series` 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

"""
title_sanitized = sanitize(title).lower()
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved
show_ids = self._get_suggestions(title)
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved

matched_show_ids = []
for show in show_ids:
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved
show_id = None
show_title = sanitize(show['title'])
# attempt with year
if not show_id and year:
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved
logger.debug('Getting show id with year')
show_id = '/'.join(show['link'].rsplit('/', 2)[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 = '/'.join(show['link'].rsplit('/', 2)[1:]) if show_title == title_sanitized else None
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved

if show_id:
matched_show_ids.append(show_id)

return matched_show_ids

@region.cache_on_arguments(expiration_time=SHOW_EXPIRATION_TIME, to_str=text_type,
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved
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

"""
# 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),
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved
headers={'Referer': self.server_url}, timeout=10)
r.raise_for_status()

if not r.content:
logger.debug('No data returned from provider')
return {}
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved

soup = ParserBeautifulSoup(r.content, ['lxml', 'html.parser'])
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'])

year_num = None
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved
matches = year_re.match(str(soup.select_one('#dates_header_br > table div').contents[2]).strip())
if matches:
year_num = int(matches.group(1))
show_title = str(soup.select_one('#dates_header_br > table u').contents[0]).strip()
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved

subtitles = []
# loop over episode rows
for subtitle in soup.select('table.table_border div[align="center"] > div'):
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved
# 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])

subtitle = self.subtitle_class(language, page_link, show_title, year_num, version, download_link)

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 []
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved

show_ids = None
for title in titles:
show_ids = self.get_show_ids(title, video.year)
if show_ids and len(show_ids) > 0:
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved
break

subtitles = []
# query for subtitles with the show_id
for show_id in show_ids:
subtitles += [s for s in self.query(show_id, video.series, video.season, video.episode, video.title)
if s.language in languages]

return subtitles

def download_subtitle(self, subtitle):
if isinstance(subtitle, Subs4SeriesSubtitle):
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved
# 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('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

download_url = self.server_url + target
r = self.session.get(download_url, 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)


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)
pkoutsias marked this conversation as resolved.
Show resolved Hide resolved

return None
Loading