Версия для Kodi 19
This commit is contained in:
1 parent
e5bd0ca668
commit
baf6d0c369
8 files changed
+235
-203
No files matched your search
@@ -1,85 +1,28 @@
|
||||
import urlquick # @UnresolvedImport
|
||||
from typing import Optional
|
||||
|
||||
from ..settings import option
|
||||
import socks
|
||||
import requests
|
||||
|
||||
class SocksiPyConnection(urlquick.HTTPConnection):
|
||||
def __init__(self, proxytype, proxyaddr, proxyport=None, host='127.0.0.1', port=9050, rdns=True, username=None, password=None, **kwargs):
|
||||
self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password)
|
||||
urlquick.HTTPConnection.__init__(self, host, port=port, **kwargs)
|
||||
|
||||
def connect(self):
|
||||
self.sock = socks.socksocket()
|
||||
self.sock.setproxy(*self.proxyargs)
|
||||
if type(self.timeout) in (int, float):
|
||||
self.sock.settimeout(self.timeout)
|
||||
self.sock.connect((self.host, self.port))
|
||||
|
||||
class SocksiPyConnectionS(urlquick.HTTPSConnection):
|
||||
def __init__(self, proxytype, proxyaddr, proxyport=None, host='127.0.0.1', port=9050, rdns=True, username=None, password=None, **kwargs):
|
||||
self.proxyargs = (proxytype, proxyaddr, proxyport, rdns, username, password)
|
||||
urlquick.HTTPSConnection.__init__(self, host, port=port, **kwargs)
|
||||
|
||||
def connect(self):
|
||||
sock = socks.socksocket()
|
||||
sock.setproxy(*self.proxyargs)
|
||||
if type(self.timeout) in (int, float):
|
||||
sock.settimeout(self.timeout)
|
||||
sock.connect((self.host, self.port))
|
||||
self.sock = urlquick.ssl.wrap_socket(sock, self.key_file, self.cert_file)
|
||||
|
||||
class Session(urlquick.Session):
|
||||
def connect(self, req, timeout, verify):
|
||||
# Fetch connection from pool and attempt to reuse if available
|
||||
pool = self.request_handler[req.type]
|
||||
if req.host in pool:
|
||||
try:
|
||||
# noinspection PyTypeChecker
|
||||
return self.send_request(pool[req.host], req)
|
||||
except Exception as e:
|
||||
# Remove the connection from the pool as it's unusable
|
||||
pool[req.host].close()
|
||||
del pool[req.host]
|
||||
|
||||
# Raise the exception if it's not a subclass of UrlError
|
||||
if not isinstance(e, urlquick.UrlError):
|
||||
raise
|
||||
|
||||
|
||||
host_port = req.host.split(':')
|
||||
host = host_port[0]
|
||||
port = int(host_port[2]) if len(host_port) > 1 else 443 if req.type == 'https' else 80
|
||||
# Create a new connection
|
||||
if not option.get_boolean('use_socks'): # @UndefinedVariable
|
||||
if req.type == "https":
|
||||
context = urlquick.ssl._create_unverified_context() if verify is False else None
|
||||
conn = urlquick.HTTPSConnection(host, port, timeout=timeout, context=context)
|
||||
else:
|
||||
conn = urlquick.HTTPConnection(host, port, timeout=timeout)
|
||||
else:
|
||||
if req.type == "https":
|
||||
context = urlquick.ssl._create_unverified_context() if verify is False else None
|
||||
conn = SocksiPyConnectionS(socks.PROXY_TYPE_SOCKS5, option['socks_ip'], proxyport=int(option['socks_port']),
|
||||
host=host, port=port, timeout=timeout, context=context)
|
||||
else:
|
||||
conn = SocksiPyConnection(socks.PROXY_TYPE_SOCKS5, option['socks_ip'], proxyport=int(option['socks_port']),
|
||||
host=host, port=port, timeout=timeout)
|
||||
|
||||
# Make first connection to server
|
||||
response = self.send_request(conn, req)
|
||||
|
||||
# Add connection to the pool if the response is not set to close
|
||||
if not response.will_close:
|
||||
pool[req.host] = conn
|
||||
return response
|
||||
def torrent_file_fetch(url, referer, cookies):
|
||||
sess = Session()
|
||||
headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 YaBrowser/14.10.2062.12061 Safari/537.36',
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
|
||||
'Chrome/37.0.2062.124 YaBrowser/14.10.2062.12061 Safari/537.36',
|
||||
'Referer': referer
|
||||
}
|
||||
resp = sess.get(url, cookies=cookies, headers=headers)
|
||||
}
|
||||
|
||||
resp = requests.get(url, cookies=cookies, headers=headers, proxies=proxies())
|
||||
if resp.ok:
|
||||
return resp.content
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
def proxies():
|
||||
if option.get_boolean('use_socks'):
|
||||
return {
|
||||
'http': f'socks5://{option["socks_ip"]}:{option["socks_port"]}',
|
||||
'https': f'socks5://{option["socks_ip"]}:{option["socks_port"]}'
|
||||
}
|
||||
else:
|
||||
return None
|
||||
+65
-56
@@ -1,61 +1,69 @@
|
||||
import xbmc, xbmcgui
|
||||
from .utils import get_engine, localize
|
||||
|
||||
class TorrentPlayer(xbmc.Player) :
|
||||
pyrrent_engine = None
|
||||
loop = None
|
||||
paused = False
|
||||
def onPlayBackEnded(self):
|
||||
self.pyrrent_engine.close()
|
||||
self.loop.stopped = True
|
||||
xbmc.Player().stop()
|
||||
def onPlayBackPaused(self):
|
||||
self.pyrrent_engine.pause()
|
||||
self.paused = True
|
||||
def onPlayBackResumed(self):
|
||||
self.paused = False
|
||||
self.pyrrent_engine.resume()
|
||||
|
||||
def onPlayBackStopped(self):
|
||||
self.pyrrent_engine.close()
|
||||
self.loop.stopped = True
|
||||
xbmc.Player().stop()
|
||||
def play(self, engine, f_index):
|
||||
self.pyrrent_engine = engine
|
||||
self.pyrrent_engine.start()
|
||||
self.pyrrent_engine.activate_file(f_index)
|
||||
monitor = xbmc.Monitor()
|
||||
pw = xbmcgui.DialogProgress()
|
||||
pw.create(localize(33055), line1='0 Kbit/s')
|
||||
f_size = self.pyrrent_engine.file_status(f_index).size
|
||||
EXPECTED_KBYTES = f_size / 3 / 1024 / 1024
|
||||
if EXPECTED_KBYTES > 768:
|
||||
EXPECTED_KBYTES = 768
|
||||
while True:
|
||||
xbmc.sleep(500)
|
||||
if monitor.abortRequested() or pw.iscanceled():
|
||||
pw.close()
|
||||
self.pyrrent_engine.close()
|
||||
xbmc.Player().stop()
|
||||
self.loop.stopped = True
|
||||
return False
|
||||
status = self.pyrrent_engine.status()
|
||||
d_rate = status.download_rate
|
||||
# xbmc.log('*** DRATE: {}'.format(d_rate), level=xbmc.LOGNOTICE)
|
||||
perc = d_rate / EXPECTED_KBYTES * 100
|
||||
if perc > 100: perc = 100
|
||||
pw.update(perc, line1=' {} / {} KB/s'.format(int(d_rate), int(EXPECTED_KBYTES)))
|
||||
if perc == 100:
|
||||
pw.close()
|
||||
break
|
||||
fstat = self.pyrrent_engine.file_status(f_index)
|
||||
listitem = xbmcgui.ListItem('.'.join(fstat.name.split('/')[-1].split('.')[:-1])[:-1], path=fstat.url)
|
||||
xbmc.Player.play(self, fstat.url, listitem)
|
||||
class TorrentPlayer(xbmc.Player):
|
||||
pyrrent_engine = None
|
||||
loop = None
|
||||
paused = False
|
||||
|
||||
def onPlayBackEnded(self):
|
||||
self.pyrrent_engine.close()
|
||||
self.loop.stopped = True
|
||||
xbmc.Player().stop()
|
||||
|
||||
def onPlayBackPaused(self):
|
||||
self.pyrrent_engine.pause()
|
||||
self.paused = True
|
||||
|
||||
def onPlayBackResumed(self):
|
||||
self.paused = False
|
||||
self.pyrrent_engine.resume()
|
||||
|
||||
def onPlayBackStopped(self):
|
||||
self.pyrrent_engine.close()
|
||||
self.loop.stopped = True
|
||||
xbmc.Player().stop()
|
||||
|
||||
def play(self, engine, f_index):
|
||||
self.pyrrent_engine = engine
|
||||
self.pyrrent_engine.start()
|
||||
self.pyrrent_engine.activate_file(f_index)
|
||||
monitor = xbmc.Monitor()
|
||||
pw = xbmcgui.DialogProgress()
|
||||
pw.create(localize(33055), '0 Kbit/s')
|
||||
f_size = self.pyrrent_engine.file_status(f_index).size
|
||||
EXPECTED_KBYTES = f_size // 3 // 1024 // 1024
|
||||
if EXPECTED_KBYTES > 768:
|
||||
EXPECTED_KBYTES = 768
|
||||
while True:
|
||||
xbmc.sleep(500)
|
||||
if monitor.abortRequested() or pw.iscanceled():
|
||||
pw.close()
|
||||
self.pyrrent_engine.close()
|
||||
xbmc.Player().stop()
|
||||
self.loop.stopped = True
|
||||
return False
|
||||
status = self.pyrrent_engine.status()
|
||||
d_rate = status.download_rate
|
||||
# xbmc.log('*** DRATE: {}'.format(d_rate), level=xbmc.LOGNOTICE)
|
||||
perc = d_rate // EXPECTED_KBYTES * 100
|
||||
if perc > 100: perc = 100
|
||||
pw.update(perc, ' {} / {} KB/s'.format(int(d_rate), int(EXPECTED_KBYTES)))
|
||||
if perc == 100:
|
||||
pw.close()
|
||||
break
|
||||
fstat = self.pyrrent_engine.file_status(f_index)
|
||||
listitem = xbmcgui.ListItem('.'.join(fstat.name.split('/')[-1].split('.')[:-1])[:-1], path=fstat.url)
|
||||
xbmc.Player.play(self, fstat.url, listitem)
|
||||
|
||||
|
||||
class VideoLoop(object):
|
||||
stopped = False
|
||||
|
||||
def __init__(self, torr_fp):
|
||||
self.e = get_engine(torr_fp)
|
||||
|
||||
def start(self, f_index):
|
||||
self.statinfo = xbmcgui.Dialog()
|
||||
self.mediaPlayer = TorrentPlayer()
|
||||
@@ -68,11 +76,12 @@ class VideoLoop(object):
|
||||
status = self.e.status()
|
||||
f_status = self.e.file_status(f_index)
|
||||
if self.mediaPlayer.paused:
|
||||
self.statinfo.notification('[{}]{}.'.format(('|' * (int(f_status.progress * 100) / 2)).ljust(50, '.'), ' ' * 100),
|
||||
'S: {} DL: {} KB/s UL: {} KB/s'.format(status.num_seeds,
|
||||
status.download_rate,
|
||||
status.upload_rate),
|
||||
time=2, sound=False
|
||||
)
|
||||
self.statinfo.notification(
|
||||
# '[{}]{}.'.format(('|' * (int(f_status.progress * 100) // 2)).ljust(50, '.'), ' ' * 100),
|
||||
'[{}]'.format(('|' * (int(f_status.progress * 100) // 2)).ljust(50, '.')),
|
||||
'S: {} DL: {} KB/s UL: {} KB/s'.format(status.num_seeds,
|
||||
status.download_rate,
|
||||
status.upload_rate),
|
||||
time=2, sound=False
|
||||
)
|
||||
xbmc.sleep(1000)
|
||||
|
||||
@@ -1,8 +1,10 @@
|
||||
from codequick.utils import urljoin_partial # @UnresolvedImport
|
||||
import urlquick # @UnresolvedImport
|
||||
from ..settings import option
|
||||
from ..overrrides import session
|
||||
#import xbmc
|
||||
import xbmc
|
||||
from codequick.utils import urljoin_partial
|
||||
from ..overrrides.session import proxies
|
||||
from requests.sessions import Session
|
||||
from htmlement import HTMLement
|
||||
from urllib.parse import quote
|
||||
|
||||
|
||||
class ResultItem(object):
|
||||
def __init__(self, url, title, size, seeders, leachers, icon='video.png', cookies={}, referer=''):
|
||||
@@ -15,34 +17,46 @@ class ResultItem(object):
|
||||
self.cookies = cookies
|
||||
self.referer = referer
|
||||
|
||||
|
||||
class Searcher(object):
|
||||
base_url = None
|
||||
search_path = None
|
||||
cookies = {}
|
||||
headers = {}
|
||||
name = 'BaseClass'
|
||||
|
||||
def __init__(self):
|
||||
self.session = session.Session()
|
||||
self.session = Session()
|
||||
self.session.proxies.update(proxies())
|
||||
self.headers = {
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 YaBrowser/14.10.2062.12061 Safari/537.36',
|
||||
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
|
||||
'Chrome/37.0.2062.124 YaBrowser/14.10.2062.12061 Safari/537.36',
|
||||
'Referer': self.base_url
|
||||
}
|
||||
}
|
||||
|
||||
def prepare(self):
|
||||
'''Login or something else if needed'''
|
||||
"""Login or something else if needed"""
|
||||
return True
|
||||
|
||||
def search(self, query):
|
||||
if self.prepare():
|
||||
try:
|
||||
url_constructor = urljoin_partial(self.base_url)
|
||||
s_url = self.normalize_url(url_constructor(self.search_path.format(urlquick.quote(query))))
|
||||
s_url = self.normalize_url(url_constructor(self.search_path.format(quote(query))))
|
||||
resp = self.session.get(s_url, cookies=self.cookies, headers=self.headers)
|
||||
body = resp.parse('body')
|
||||
return self.process(body)
|
||||
parser = HTMLement('body')
|
||||
parser.feed(resp.text)
|
||||
# todo отладка
|
||||
xbmc.log(f'SEARCH BODY:\n{resp.text}')
|
||||
#
|
||||
return self.process(parser.close())
|
||||
except:
|
||||
return []
|
||||
else:
|
||||
return []
|
||||
|
||||
def process(self, body):
|
||||
'''Process element tree'''
|
||||
"""Process element tree"""
|
||||
|
||||
def normalize_url(self, url):
|
||||
return url
|
||||
@@ -1,15 +1,19 @@
|
||||
# -*- 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 # @UnresolvedImport
|
||||
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') # @UndefinedVariable
|
||||
enabled = option.get_boolean('kinozal_enable')
|
||||
|
||||
def prepare(self):
|
||||
if self.cookies:
|
||||
c_uid = self.cookies.get('uid', None)
|
||||
@@ -17,6 +21,7 @@ class SearchEngine(Searcher):
|
||||
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]')))
|
||||
@@ -28,20 +33,31 @@ class SearchEngine(Searcher):
|
||||
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"]'))
|
||||
)
|
||||
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': ''},
|
||||
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)
|
||||
@@ -52,5 +68,6 @@ class SearchEngine(Searcher):
|
||||
except:
|
||||
notify(self.name, localize(33056), local_image.format(self.icon))
|
||||
return False
|
||||
|
||||
def normalize_url(self, url):
|
||||
return url.encode('cp1251')
|
||||
@@ -1,4 +1,6 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
import xbmc
|
||||
|
||||
from . import Searcher, urljoin_partial, ResultItem
|
||||
from ..settings import option
|
||||
from ..utils import notify, localize
|
||||
@@ -43,6 +45,11 @@ class SearchEngine(Searcher):
|
||||
cookies={}, headers=self.headers, allow_redirects=False)
|
||||
if not resp.ok:
|
||||
raise Exception
|
||||
|
||||
# todo отладка
|
||||
xbmc.log(f'RuTracker LOGIN BODY:\n{resp.text}')
|
||||
#
|
||||
|
||||
cookies = resp.cookies
|
||||
bb_session = cookies.get('bb_session', None)
|
||||
if not bb_session:
|
||||
|
||||
@@ -1,28 +1,26 @@
|
||||
from codequick.support import addon_data # @UnresolvedImport
|
||||
from codequick.listing import local_image # @UnresolvedImport
|
||||
from urlquick import urljoin # @UnresolvedImport
|
||||
from codequick.support import addon_data # @UnresolvedImport
|
||||
from codequick.utils import urljoin_partial # @UnresolvedImport
|
||||
from ..settings import option
|
||||
import xbmcgui
|
||||
import os
|
||||
from hashlib import sha1
|
||||
from pyrrent2http import Engine # @UnresolvedImport
|
||||
import sys
|
||||
py3 = sys.version_info >= (3, 0)
|
||||
if py3:
|
||||
from urllib.request import pathname2url # @UnresolvedImport
|
||||
else:
|
||||
from urllib import pathname2url
|
||||
from urllib.request import pathname2url
|
||||
|
||||
|
||||
def notify(heading, message, icon=xbmcgui.NOTIFICATION_INFO):
|
||||
n = xbmcgui.Dialog()
|
||||
n.notification(heading, message, icon, time=5, sound=True)
|
||||
|
||||
|
||||
def localize(sid):
|
||||
return addon_data.getLocalizedString(sid)
|
||||
|
||||
|
||||
def store_torrent_file(file_bytes):
|
||||
h = sha1(file_bytes).hexdigest()
|
||||
t_fname = '{}.torrent'.format(h)
|
||||
full_path = os.path.join(storage_toorents_dir, t_fname)
|
||||
full_path = os.path.join(storage_torrents_dir, t_fname)
|
||||
if os.path.exists(full_path):
|
||||
with open(full_path, 'rb') as f:
|
||||
if sha1(f.read()).hexdigest() == h:
|
||||
@@ -30,32 +28,39 @@ def store_torrent_file(file_bytes):
|
||||
with open(full_path, 'wb') as f:
|
||||
f.write(file_bytes)
|
||||
return full_path
|
||||
|
||||
|
||||
def torrent_full_path(t_fname):
|
||||
return os.path.join(storage_toorents_dir, t_fname)
|
||||
return os.path.join(storage_torrents_dir, t_fname)
|
||||
|
||||
|
||||
def file_url(path):
|
||||
if not path.startswith('file:'):
|
||||
path = urljoin('file:', pathname2url(path))
|
||||
path = urljoin_partial('file:')(pathname2url(path))
|
||||
return path
|
||||
|
||||
|
||||
def get_engine(torrent_uri):
|
||||
if option.get_boolean('use_socks_for_trackers'): # @UndefinedVariable
|
||||
if option.get_boolean('use_socks_for_trackers'): # @UndefinedVariable
|
||||
proxy = {
|
||||
'host': option['socks_ip'],
|
||||
'port': int(option['socks_port'])
|
||||
}
|
||||
}
|
||||
else:
|
||||
proxy = None
|
||||
return Engine(uri=file_url(torrent_uri), download_path=storage_download_dir,
|
||||
encryption=1, keep_complete=False, keep_incomplete=False,
|
||||
dht_routers=["router.bittorrent.com:6881", "router.utorrent.com:6881"], use_random_port=False, listen_port=6881,
|
||||
user_agent='', enable_dht=True, proxy=proxy)
|
||||
encryption=1, keep_complete=False, keep_incomplete=False,
|
||||
dht_routers=["router.bittorrent.com:6881", "router.utorrent.com:6881"], use_random_port=False,
|
||||
listen_port=6881,
|
||||
user_agent='', enable_dht=True, proxy=proxy)
|
||||
|
||||
|
||||
while not option['storage_dir']:
|
||||
dialog = xbmcgui.Dialog()
|
||||
dialog.ok(localize(33000), localize(33051))
|
||||
addon_data.openSettings()
|
||||
storage_root = os.path.abspath(os.path.join(option['storage_dir'], 'Torrenter3'))
|
||||
storage_toorents_dir = os.path.join(storage_root, 'torrents')
|
||||
storage_torrents_dir = os.path.join(storage_root, 'torrents')
|
||||
storage_download_dir = os.path.join(storage_root, 'download')
|
||||
if not os.path.exists(storage_toorents_dir): os.makedirs(storage_toorents_dir)
|
||||
if not os.path.exists(storage_torrents_dir): os.makedirs(storage_torrents_dir)
|
||||
if not os.path.exists(storage_download_dir): os.makedirs(storage_download_dir)
|
||||
Reference in new issue
Block a user