2019-07-18 23:21:22 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
2019-09-17 02:04:27 +00:00
|
|
|
from __future__ import absolute_import
|
2019-07-18 23:21:22 +00:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
import io
|
|
|
|
import rarfile
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
from guessit import guessit
|
|
|
|
from requests import Session
|
|
|
|
|
2023-12-28 21:03:23 +00:00
|
|
|
from subliminal import Episode
|
|
|
|
from subliminal.exceptions import AuthenticationError, ConfigurationError
|
2019-07-18 23:21:22 +00:00
|
|
|
from subliminal_patch.subtitle import Subtitle, guess_matches
|
|
|
|
from subliminal.subtitle import fix_line_ending, SUBTITLE_EXTENSIONS
|
|
|
|
from subliminal_patch.providers import Provider
|
|
|
|
from subzero.language import Language
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
server_url = 'https://api.betaseries.com/'
|
|
|
|
|
|
|
|
|
|
|
|
class BetaSeriesSubtitle(Subtitle):
|
|
|
|
provider_name = 'betaseries'
|
|
|
|
|
2021-06-07 16:41:28 +00:00
|
|
|
def __init__(self, subtitle_id, language, video_name, url, matches, source, video_release_group):
|
2019-07-18 23:21:22 +00:00
|
|
|
super(BetaSeriesSubtitle, self).__init__(language, page_link=url)
|
|
|
|
self.subtitle_id = subtitle_id
|
|
|
|
self.video_name = video_name
|
|
|
|
self.download_url = url
|
|
|
|
self.matches = matches
|
|
|
|
self.source = source
|
2021-06-07 16:41:28 +00:00
|
|
|
self.video_release_group = video_release_group
|
2021-12-19 14:56:13 +00:00
|
|
|
self.release_info = video_name
|
2019-07-18 23:21:22 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def id(self):
|
|
|
|
return self.subtitle_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def download_link(self):
|
|
|
|
return self.download_url
|
|
|
|
|
|
|
|
def get_matches(self, video):
|
|
|
|
matches = self.matches
|
|
|
|
|
|
|
|
if isinstance(video, Episode):
|
|
|
|
matches |= guess_matches(video, guessit(
|
|
|
|
self.video_name, {'type': 'episode'}), partial=True)
|
|
|
|
|
|
|
|
return matches
|
|
|
|
|
|
|
|
|
|
|
|
class BetaSeriesProvider(Provider):
|
|
|
|
"""BetaSeries Provider"""
|
|
|
|
languages = {Language(l) for l in ['fra', 'eng']}
|
|
|
|
video_types = (Episode,)
|
|
|
|
|
|
|
|
def __init__(self, token=None):
|
|
|
|
if not token:
|
|
|
|
raise ConfigurationError('Token must be specified')
|
|
|
|
self.token = token
|
2021-06-07 16:41:28 +00:00
|
|
|
self.video = None
|
2019-07-18 23:21:22 +00:00
|
|
|
|
|
|
|
def initialize(self):
|
|
|
|
self.session = Session()
|
|
|
|
self.session.headers = {
|
|
|
|
'User-Agent': os.environ.get("SZ_USER_AGENT", "Sub-Zero/2")}
|
|
|
|
|
|
|
|
def terminate(self):
|
|
|
|
self.session.close()
|
|
|
|
|
|
|
|
def query(self, languages, video):
|
|
|
|
# query the server
|
2019-12-21 21:56:45 +00:00
|
|
|
self.video = video
|
2019-07-18 23:21:22 +00:00
|
|
|
matches = set()
|
|
|
|
if video.tvdb_id:
|
|
|
|
params = {'key': self.token,
|
|
|
|
'thetvdb_id': video.tvdb_id,
|
|
|
|
'v': 3.0,
|
|
|
|
'subtitles': 1}
|
|
|
|
logger.debug('Searching subtitles %r', params)
|
|
|
|
res = self.session.get(
|
|
|
|
server_url + 'episodes/display', params=params, timeout=10)
|
2024-03-19 00:28:19 +00:00
|
|
|
try:
|
|
|
|
if res.status_code == 400 and res.json()['errors'][0]['code'] == 4001:
|
|
|
|
# this is to catch no series found
|
|
|
|
return []
|
|
|
|
elif res.status_code == 400 and res.json()['errors'][0]['code'] == 1001:
|
|
|
|
raise AuthenticationError("Invalid token provided")
|
|
|
|
except Exception:
|
|
|
|
pass
|
2019-07-18 23:21:22 +00:00
|
|
|
res.raise_for_status()
|
|
|
|
result = res.json()
|
|
|
|
matches.add('tvdb_id')
|
|
|
|
elif video.series_tvdb_id:
|
|
|
|
params = {'key': self.token,
|
|
|
|
'thetvdb_id': video.series_tvdb_id,
|
|
|
|
'season': video.season,
|
|
|
|
'episode': video.episode,
|
|
|
|
'subtitles': 1,
|
|
|
|
'v': 3.0}
|
|
|
|
logger.debug('Searching subtitles %r', params)
|
|
|
|
res = self.session.get(
|
|
|
|
server_url + 'shows/episodes', params=params, timeout=10)
|
2024-03-19 00:28:19 +00:00
|
|
|
try:
|
|
|
|
if res.status_code == 400 and res.json()['errors'][0]['code'] == 4001:
|
|
|
|
# this is to catch no series found
|
|
|
|
return []
|
|
|
|
elif res.status_code == 400 and res.json()['errors'][0]['code'] == 1001:
|
|
|
|
raise AuthenticationError("Invalid token provided")
|
|
|
|
except Exception:
|
|
|
|
pass
|
2019-07-18 23:21:22 +00:00
|
|
|
res.raise_for_status()
|
|
|
|
result = res.json()
|
|
|
|
matches.add('series_tvdb_id')
|
|
|
|
else:
|
|
|
|
logger.debug(
|
|
|
|
'The show has no tvdb_id and series_tvdb_id: the search can\'t be done')
|
|
|
|
return []
|
|
|
|
|
|
|
|
if result['errors'] != []:
|
|
|
|
logger.debug('Status error: %r', result['errors'])
|
|
|
|
return []
|
|
|
|
|
|
|
|
# parse the subtitles
|
|
|
|
subtitles = []
|
2023-12-28 21:03:23 +00:00
|
|
|
if 'episode' in result and 'subtitles' in result['episode']:
|
2019-07-18 23:21:22 +00:00
|
|
|
subs = result['episode']['subtitles']
|
2023-12-31 20:59:16 +00:00
|
|
|
elif 'episodes' in result and len(result['episodes']) and 'subtitles' in result['episodes'][0]:
|
2019-07-18 23:21:22 +00:00
|
|
|
subs = result['episodes'][0]['subtitles']
|
|
|
|
else:
|
|
|
|
return []
|
|
|
|
|
|
|
|
for sub in subs:
|
|
|
|
language = _translateLanguageCodeToLanguage(sub['language'])
|
|
|
|
if language in languages:
|
|
|
|
# Filter seriessub source because it shut down so the links are all 404
|
|
|
|
if str(sub['source']) != 'seriessub':
|
|
|
|
subtitles.append(BetaSeriesSubtitle(
|
2021-06-07 16:41:28 +00:00
|
|
|
sub['id'], language, sub['file'], sub['url'], matches, str(sub['source']),
|
|
|
|
self.video.release_group))
|
2019-07-18 23:21:22 +00:00
|
|
|
|
|
|
|
return subtitles
|
|
|
|
|
|
|
|
def list_subtitles(self, video, languages):
|
|
|
|
return self.query(languages, video)
|
|
|
|
|
|
|
|
def download_subtitle(self, subtitle):
|
|
|
|
logger.info('Downloading subtitle %r', subtitle)
|
|
|
|
r = self.session.get(subtitle.download_link, timeout=10)
|
2020-05-08 11:57:42 +00:00
|
|
|
if r.status_code == 404:
|
|
|
|
logger.error('Error 404 downloading %r', subtitle)
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
r.raise_for_status()
|
2019-07-18 23:21:22 +00:00
|
|
|
|
|
|
|
archive = _get_archive(r.content)
|
2019-12-21 21:56:45 +00:00
|
|
|
if archive:
|
|
|
|
subtitle_names = _get_subtitle_names_from_archive(archive)
|
2021-06-07 16:41:28 +00:00
|
|
|
subtitle_to_download = _choose_subtitle_with_release_group(subtitle_names, subtitle.video_release_group)
|
2019-12-21 21:56:45 +00:00
|
|
|
logger.debug('Subtitle to download: ' + subtitle_to_download)
|
|
|
|
subtitle_content = archive.read(subtitle_to_download)
|
|
|
|
else:
|
|
|
|
subtitle_content = r.content
|
2019-07-18 23:21:22 +00:00
|
|
|
|
|
|
|
if subtitle_content:
|
|
|
|
subtitle.content = fix_line_ending(subtitle_content)
|
|
|
|
else:
|
2020-05-08 11:57:42 +00:00
|
|
|
logger.error('Could not extract subtitle from %r', archive)
|
2019-07-18 23:21: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
|
|
|
|
|
|
|
|
|
2019-12-21 21:56:45 +00:00
|
|
|
def _get_subtitle_names_from_archive(archive):
|
|
|
|
subtitlesToConsider = []
|
2019-07-18 23:21:22 +00:00
|
|
|
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
|
|
|
|
|
2019-12-21 21:56:45 +00:00
|
|
|
subtitlesToConsider.append(name)
|
2019-07-18 23:21:22 +00:00
|
|
|
|
2019-12-21 21:56:45 +00:00
|
|
|
if len(subtitlesToConsider)>0:
|
|
|
|
logger.debug('Subtitles in archive: ' + ' '.join(subtitlesToConsider))
|
|
|
|
return subtitlesToConsider
|
|
|
|
else:
|
|
|
|
return None
|
2019-07-18 23:21:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _translateLanguageCodeToLanguage(languageCode):
|
|
|
|
if languageCode.lower() == 'vo':
|
|
|
|
return Language.fromalpha2('en')
|
|
|
|
elif languageCode.lower() == 'vf':
|
|
|
|
return Language.fromalpha2('fr')
|
2019-12-21 21:56:45 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _choose_subtitle_with_release_group(subtitle_names, release_group):
|
|
|
|
if release_group:
|
|
|
|
for subtitle in subtitle_names:
|
|
|
|
if release_group in subtitle:
|
|
|
|
return subtitle
|
|
|
|
return subtitle_names[0]
|