mirror of
https://github.com/clinton-hall/nzbToMedia.git
synced 2024-11-14 17:40:24 -08:00
f05b09f349
Updates rarfile to 3.1 Updates stevedore to 3.5.0 Updates appdirs to 1.4.4 Updates click to 8.1.3 Updates decorator to 5.1.1 Updates dogpile.cache to 1.1.8 Updates pbr to 5.11.0 Updates pysrt to 1.1.2 Updates pytz to 2022.6 Adds importlib-metadata version 3.1.1 Adds typing-extensions version 4.1.1 Adds zipp version 3.11.0
112 lines
2.8 KiB
Python
112 lines
2.8 KiB
Python
# -*- coding: utf-8 -*-
|
|
import logging
|
|
|
|
from babelfish import Language
|
|
from requests import Session
|
|
|
|
from . import Provider
|
|
from ..subtitle import Subtitle
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def get_subhash(hash):
|
|
"""Get a second hash based on napiprojekt's hash.
|
|
|
|
:param str hash: napiprojekt's hash.
|
|
:return: the subhash.
|
|
:rtype: str
|
|
|
|
"""
|
|
idx = [0xe, 0x3, 0x6, 0x8, 0x2]
|
|
mul = [2, 2, 5, 4, 3]
|
|
add = [0, 0xd, 0x10, 0xb, 0x5]
|
|
|
|
b = []
|
|
for i in range(len(idx)):
|
|
a = add[i]
|
|
m = mul[i]
|
|
i = idx[i]
|
|
t = a + int(hash[i], 16)
|
|
v = int(hash[t:t + 2], 16)
|
|
b.append(('%x' % (v * m))[-1])
|
|
|
|
return ''.join(b)
|
|
|
|
|
|
class NapiProjektSubtitle(Subtitle):
|
|
"""NapiProjekt Subtitle."""
|
|
provider_name = 'napiprojekt'
|
|
|
|
def __init__(self, language, hash):
|
|
super(NapiProjektSubtitle, self).__init__(language)
|
|
self.hash = hash
|
|
self.content = None
|
|
|
|
@property
|
|
def id(self):
|
|
return self.hash
|
|
|
|
@property
|
|
def info(self):
|
|
return self.hash
|
|
|
|
def get_matches(self, video):
|
|
matches = set()
|
|
|
|
# hash
|
|
if 'napiprojekt' in video.hashes and video.hashes['napiprojekt'] == self.hash:
|
|
matches.add('hash')
|
|
|
|
return matches
|
|
|
|
|
|
class NapiProjektProvider(Provider):
|
|
"""NapiProjekt Provider."""
|
|
languages = {Language.fromalpha2(l) for l in ['pl']}
|
|
required_hash = 'napiprojekt'
|
|
server_url = 'http://napiprojekt.pl/unit_napisy/dl.php'
|
|
subtitle_class = NapiProjektSubtitle
|
|
|
|
def __init__(self):
|
|
self.session = None
|
|
|
|
def initialize(self):
|
|
self.session = Session()
|
|
self.session.headers['User-Agent'] = self.user_agent
|
|
|
|
def terminate(self):
|
|
self.session.close()
|
|
|
|
def query(self, language, hash):
|
|
params = {
|
|
'v': 'dreambox',
|
|
'kolejka': 'false',
|
|
'nick': '',
|
|
'pass': '',
|
|
'napios': 'Linux',
|
|
'l': language.alpha2.upper(),
|
|
'f': hash,
|
|
't': get_subhash(hash)}
|
|
logger.info('Searching subtitle %r', params)
|
|
r = self.session.get(self.server_url, params=params, timeout=10)
|
|
r.raise_for_status()
|
|
|
|
# handle subtitles not found and errors
|
|
if r.content[:4] == b'NPc0':
|
|
logger.debug('No subtitles found')
|
|
return None
|
|
|
|
subtitle = self.subtitle_class(language, hash)
|
|
subtitle.content = r.content
|
|
logger.debug('Found subtitle %r', subtitle)
|
|
|
|
return subtitle
|
|
|
|
def list_subtitles(self, video, languages):
|
|
return [s for s in [self.query(l, video.hashes['napiprojekt']) for l in languages] if s is not None]
|
|
|
|
def download_subtitle(self, subtitle):
|
|
# there is no download step, content is already filled from listing subtitles
|
|
pass
|