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.
287 lines
11 KiB
287 lines
11 KiB
6 years ago
|
# coding=utf-8
|
||
|
|
||
5 years ago
|
from __future__ import absolute_import
|
||
6 years ago
|
import logging
|
||
|
import io
|
||
3 years ago
|
import re
|
||
|
import ssl
|
||
|
|
||
|
from urllib3 import poolmanager
|
||
6 years ago
|
|
||
|
from zipfile import ZipFile
|
||
|
|
||
|
from guessit import guessit
|
||
3 years ago
|
|
||
|
from requests.adapters import HTTPAdapter
|
||
|
|
||
6 years ago
|
from subliminal.utils import sanitize
|
||
3 years ago
|
from subliminal_patch.subtitle import guess_matches
|
||
6 years ago
|
from subliminal_patch.providers.mixins import ProviderSubtitleArchiveMixin
|
||
|
|
||
|
try:
|
||
|
from lxml import etree
|
||
|
except ImportError:
|
||
|
try:
|
||
|
import xml.etree.cElementTree as etree
|
||
|
except ImportError:
|
||
|
import xml.etree.ElementTree as etree
|
||
|
from babelfish import language_converters
|
||
3 years ago
|
from subliminal.video import Episode, Movie
|
||
6 years ago
|
from subliminal.providers.podnapisi import PodnapisiProvider as _PodnapisiProvider, \
|
||
|
PodnapisiSubtitle as _PodnapisiSubtitle
|
||
6 years ago
|
from subliminal_patch.utils import sanitize, fix_inconsistent_naming as _fix_inconsistent_naming
|
||
6 years ago
|
from subzero.language import Language
|
||
|
|
||
|
logger = logging.getLogger(__name__)
|
||
|
|
||
|
|
||
6 years ago
|
def fix_inconsistent_naming(title):
|
||
|
"""Fix titles with inconsistent naming using dictionary and sanitize them.
|
||
|
|
||
|
:param str title: original title.
|
||
|
:return: new title.
|
||
|
:rtype: str
|
||
|
|
||
|
"""
|
||
|
d = {}
|
||
|
nt = title.replace("Marvels", "").replace("Marvel's", "")
|
||
|
if nt != title:
|
||
|
d[title] = nt
|
||
|
|
||
|
return _fix_inconsistent_naming(title, d)
|
||
|
|
||
|
|
||
6 years ago
|
class PodnapisiSubtitle(_PodnapisiSubtitle):
|
||
|
provider_name = 'podnapisi'
|
||
|
hearing_impaired_verifiable = True
|
||
|
|
||
|
def __init__(self, language, hearing_impaired, page_link, pid, releases, title, season=None, episode=None,
|
||
|
year=None, asked_for_release_group=None, asked_for_episode=None):
|
||
|
super(PodnapisiSubtitle, self).__init__(language, hearing_impaired, page_link, pid, releases, title,
|
||
|
season=season, episode=episode, year=year)
|
||
|
self.release_info = u", ".join(releases)
|
||
|
self.asked_for_release_group = asked_for_release_group
|
||
|
self.asked_for_episode = asked_for_episode
|
||
|
self.matches = None
|
||
|
|
||
|
def get_matches(self, video):
|
||
|
"""
|
||
|
patch: set guessit to single_value
|
||
|
:param video:
|
||
|
:return:
|
||
|
"""
|
||
|
matches = set()
|
||
|
|
||
|
# episode
|
||
|
if isinstance(video, Episode):
|
||
|
# series
|
||
6 years ago
|
if video.series and (fix_inconsistent_naming(self.title) in (
|
||
|
fix_inconsistent_naming(name) for name in [video.series] + video.alternative_series)):
|
||
6 years ago
|
matches.add('series')
|
||
|
# year
|
||
|
if video.original_series and self.year is None or video.year and video.year == self.year:
|
||
|
matches.add('year')
|
||
|
# season
|
||
|
if video.season and self.season == video.season:
|
||
|
matches.add('season')
|
||
|
# episode
|
||
|
if video.episode and self.episode == video.episode:
|
||
|
matches.add('episode')
|
||
|
# guess
|
||
|
for release in self.releases:
|
||
3 years ago
|
matches |= guess_matches(video, guessit(release, {'type': 'episode'}))
|
||
6 years ago
|
# movie
|
||
|
elif 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')
|
||
|
# guess
|
||
|
for release in self.releases:
|
||
3 years ago
|
matches |= guess_matches(video, guessit(release, {'type': 'movie'}))
|
||
6 years ago
|
|
||
|
self.matches = matches
|
||
|
|
||
|
return matches
|
||
|
|
||
3 years ago
|
|
||
3 years ago
|
class PodnapisiAdapter(HTTPAdapter):
|
||
|
def init_poolmanager(self, connections, maxsize, block=False):
|
||
|
ctx = ssl.create_default_context()
|
||
2 years ago
|
ctx.set_ciphers('DEFAULT@SECLEVEL=0')
|
||
3 years ago
|
ctx.check_hostname = False
|
||
3 years ago
|
self.poolmanager = poolmanager.PoolManager(
|
||
|
num_pools=connections,
|
||
|
maxsize=maxsize,
|
||
|
block=block,
|
||
|
ssl_version=ssl.PROTOCOL_TLS,
|
||
|
ssl_context=ctx
|
||
|
)
|
||
6 years ago
|
|
||
3 years ago
|
|
||
6 years ago
|
class PodnapisiProvider(_PodnapisiProvider, ProviderSubtitleArchiveMixin):
|
||
|
languages = ({Language('por', 'BR'), Language('srp', script='Latn'), Language('srp', script='Cyrl')} |
|
||
|
{Language.fromalpha2(l) for l in language_converters['alpha2'].codes})
|
||
|
languages.update(set(Language.rebuild(l, forced=True) for l in languages))
|
||
4 years ago
|
languages.update(set(Language.rebuild(l, hi=True) for l in languages))
|
||
6 years ago
|
|
||
3 years ago
|
video_types = (Episode, Movie)
|
||
|
|
||
6 years ago
|
server_url = 'https://podnapisi.net/subtitles/'
|
||
|
only_foreign = False
|
||
|
also_foreign = False
|
||
3 years ago
|
verify_ssl = True
|
||
6 years ago
|
subtitle_class = PodnapisiSubtitle
|
||
|
hearing_impaired_verifiable = True
|
||
|
|
||
3 years ago
|
def __init__(self, only_foreign=False, also_foreign=False, verify_ssl=True):
|
||
6 years ago
|
self.only_foreign = only_foreign
|
||
|
self.also_foreign = also_foreign
|
||
3 years ago
|
self.verify_ssl = verify_ssl
|
||
6 years ago
|
|
||
|
if only_foreign:
|
||
|
logger.info("Only searching for foreign/forced subtitles")
|
||
|
|
||
|
super(PodnapisiProvider, self).__init__()
|
||
|
|
||
3 years ago
|
def initialize(self):
|
||
|
super().initialize()
|
||
|
self.session.mount('https://', PodnapisiAdapter())
|
||
3 years ago
|
self.session.verify = self.verify_ssl
|
||
3 years ago
|
|
||
6 years ago
|
def list_subtitles(self, video, languages):
|
||
|
if video.is_special:
|
||
|
logger.info("%s can't search for specials right now, skipping", self)
|
||
|
return []
|
||
|
|
||
|
season = episode = None
|
||
|
if isinstance(video, Episode):
|
||
6 years ago
|
titles = [fix_inconsistent_naming(title) for title in [video.series] + video.alternative_series]
|
||
6 years ago
|
season = video.season
|
||
|
episode = video.episode
|
||
|
else:
|
||
|
titles = [video.title] + video.alternative_titles
|
||
|
|
||
|
for title in titles:
|
||
|
subtitles = [s for l in languages for s in
|
||
|
self.query(l, title, video, season=season, episode=episode, year=video.year,
|
||
|
only_foreign=self.only_foreign, also_foreign=self.also_foreign)]
|
||
|
if subtitles:
|
||
|
return subtitles
|
||
|
|
||
|
return []
|
||
|
|
||
|
def query(self, language, keyword, video, season=None, episode=None, year=None, only_foreign=False,
|
||
|
also_foreign=False):
|
||
|
search_language = str(language).lower()
|
||
|
|
||
|
# sr-Cyrl specialcase
|
||
|
if search_language == "sr-cyrl":
|
||
|
search_language = "sr"
|
||
|
|
||
|
# set parameters, see http://www.podnapisi.net/forum/viewtopic.php?f=62&t=26164#p212652
|
||
|
params = {'sXML': 1, 'sL': search_language, 'sK': keyword}
|
||
|
|
||
|
is_episode = False
|
||
|
if season and episode:
|
||
|
is_episode = True
|
||
|
params['sTS'] = season
|
||
|
params['sTE'] = episode
|
||
|
|
||
|
if year:
|
||
|
params['sY'] = year
|
||
|
|
||
|
# loop over paginated results
|
||
|
logger.info('Searching subtitles %r', params)
|
||
|
subtitles = []
|
||
|
pids = set()
|
||
|
while True:
|
||
|
# query the server
|
||
|
content = None
|
||
|
try:
|
||
11 months ago
|
content = self.session.get(self.server_url + 'search/old', params=params, timeout=30).content
|
||
6 years ago
|
xml = etree.fromstring(content)
|
||
6 years ago
|
except etree.ParseError:
|
||
6 years ago
|
logger.error("Wrong data returned: %r", content)
|
||
|
break
|
||
|
|
||
|
# exit if no results
|
||
9 months ago
|
if not xml.find('pagination/results') or not int(xml.find('pagination/results').text):
|
||
6 years ago
|
logger.debug('No subtitles found')
|
||
|
break
|
||
|
|
||
|
# loop over subtitles
|
||
|
for subtitle_xml in xml.findall('subtitle'):
|
||
|
# read xml elements
|
||
|
pid = subtitle_xml.find('pid').text
|
||
|
# ignore duplicates, see http://www.podnapisi.net/forum/viewtopic.php?f=62&t=26164&start=10#p213321
|
||
|
if pid in pids:
|
||
|
continue
|
||
|
|
||
6 years ago
|
_language = Language.fromietf(subtitle_xml.find('language').text)
|
||
6 years ago
|
hearing_impaired = 'n' in (subtitle_xml.find('flags').text or '')
|
||
|
foreign = 'f' in (subtitle_xml.find('flags').text or '')
|
||
|
if only_foreign and not foreign:
|
||
|
continue
|
||
|
|
||
|
elif not only_foreign and not also_foreign and foreign:
|
||
|
continue
|
||
|
|
||
|
elif also_foreign and foreign:
|
||
6 years ago
|
_language = Language.rebuild(_language, forced=True)
|
||
|
|
||
4 years ago
|
# set subtitle language to hi if it's hearing_impaired
|
||
|
if hearing_impaired:
|
||
|
_language = Language.rebuild(_language, hi=True)
|
||
|
|
||
6 years ago
|
if language != _language:
|
||
|
continue
|
||
6 years ago
|
|
||
|
page_link = subtitle_xml.find('url').text
|
||
|
releases = []
|
||
|
if subtitle_xml.find('release').text:
|
||
|
for release in subtitle_xml.find('release').text.split():
|
||
|
releases.append(re.sub(r'\.+$', '', release)) # remove trailing dots
|
||
|
title = subtitle_xml.find('title').text
|
||
|
r_season = int(subtitle_xml.find('tvSeason').text)
|
||
|
r_episode = int(subtitle_xml.find('tvEpisode').text)
|
||
|
r_year = int(subtitle_xml.find('year').text)
|
||
|
|
||
|
if is_episode:
|
||
6 years ago
|
subtitle = self.subtitle_class(_language, hearing_impaired, page_link, pid, releases, title,
|
||
6 years ago
|
season=r_season, episode=r_episode, year=r_year,
|
||
|
asked_for_release_group=video.release_group,
|
||
|
asked_for_episode=episode)
|
||
|
else:
|
||
6 years ago
|
subtitle = self.subtitle_class(_language, hearing_impaired, page_link, pid, releases, title,
|
||
6 years ago
|
year=r_year, asked_for_release_group=video.release_group)
|
||
|
|
||
|
|
||
|
logger.debug('Found subtitle %r', subtitle)
|
||
|
subtitles.append(subtitle)
|
||
|
pids.add(pid)
|
||
|
|
||
|
# stop on last page
|
||
|
if int(xml.find('pagination/current').text) >= int(xml.find('pagination/count').text):
|
||
|
break
|
||
|
|
||
|
# increment current page
|
||
|
params['page'] = int(xml.find('pagination/current').text) + 1
|
||
|
logger.debug('Getting page %d', params['page'])
|
||
|
xml = None
|
||
|
|
||
|
return subtitles
|
||
|
|
||
|
def download_subtitle(self, subtitle):
|
||
|
# download as a zip
|
||
|
logger.info('Downloading subtitle %r', subtitle)
|
||
11 months ago
|
r = self.session.get(self.server_url + subtitle.pid + '/download', params={'container': 'zip'}, timeout=30)
|
||
6 years ago
|
r.raise_for_status()
|
||
|
|
||
|
# open the zip
|
||
|
with ZipFile(io.BytesIO(r.content)) as zf:
|
||
|
subtitle.content = self.get_subtitle_from_archive(subtitle, zf)
|