plugin.video.torrenter3/resources/lib/searchers/kinozal.py

74 lines
2.7 KiB
Python

# -*- coding: utf-8 -*-
import xbmc
from . import Searcher, urljoin_partial, ResultItem
from ..settings import option
from ..utils import notify, localize
from codequick.listing import local_image
class SearchEngine(Searcher):
base_url = option['kinozal_url']
search_path = '/browse.php?s={}&t=1'
name = 'Kinozal.tv'
icon = 'searcher_kinozal.png'
enabled = option.get_boolean('kinozal_enable')
def prepare(self):
if self.cookies:
c_uid = self.cookies.get('uid', None)
c_pass = self.cookies.get('pass', None)
if c_uid and c_pass:
return True
return self.login()
def process(self, body):
url_constructor = urljoin_partial(self.base_url)
rows = list(filter(lambda x: x.get('class') in ['first bg', 'bg'], body.findall('.//tr[@class]')))
for r in rows:
link = r.find('.//td[@class="nam"]/a')
url_part = link.get('href').strip().split('=')[1]
url = url_constructor(u'/download.php?id={}'.format(url_part))
title = link.text
seeders = int(r.find('.//td[@class="sl_s"]').text.strip())
leachers = int(r.find('.//td[@class="sl_p"]').text.strip())
size_l = list(filter(lambda x: x.text.strip().endswith((u'ГБ', u'МБ')),
r.findall('.//td[@class="s"]'))
)
size = size_l[0].text.strip() if len(size_l) else '0'
yield ResultItem(url, title, size, seeders, leachers, self.icon, self.cookies, self.base_url)
def login(self):
user = option['kinozal_login']
password = option['kinozal_password']
try:
if not user or not password:
raise Exception
resp = self.session.post('{}/takelogin.php'.format(self.base_url),
data={'username': user, 'password': password, 'returnto': ''},
cookies={}, headers=self.headers, allow_redirects=False)
# todo отладка
xbmc.log(f'Kinozal LOGIN BODY:\n{resp.text}')
#
if not resp.ok:
raise Exception
# todo отладка
xbmc.log(f'LOGIN COOKIE:\n{resp.cookies}')
#
cookies = resp.cookies
c_uid = cookies.get('uid', None)
c_pass = cookies.get('pass', None)
if not c_uid or not c_pass:
raise Exception
self.cookies = cookies
return True
except:
notify(self.name, localize(33056), local_image.format(self.icon))
return False
def normalize_url(self, url):
return url.encode('cp1251')