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.

180 lines
5.9 KiB

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import os.path
import xbmc
import xbmcgui
from codequick import Route, Script, Listitem, utils, run # @UnresolvedImport
from codequick.storage import PersistentList # @UnresolvedImport
from resources.lib.overrrides.session import torrent_file_fetch
from resources.lib.player import VideoLoop
from resources.lib.searchers import rutor, kinozal, rutracker
from resources.lib.utils import localize, store_torrent_file, get_engine
video_extensions = ('.mp4', '.avi', '.3gp', '.ogv', '.mkv', '.ts', '.mpg', '.mpeg', '.webm', '.flv', '.vob')
search_engines = [
rutor.SearchEngine,
kinozal.SearchEngine,
rutracker.SearchEngine
]
ROOT = os.path.abspath(os.path.dirname(__file__))
ICON_DIR = os.path.join(ROOT, 'resources', 'icons')
@Route.register
def root(plugin, content_type='video'):
@Route.register_delayed
def set_view():
xbmc.executebuiltin('Container.SetViewMode("500")')
def build_item(a):
cb, name, image = a
i = Listitem.from_dict(cb, name)
i.art.local_thumb(image)
return i
for li in (
(search_history, localize(33052), 'history2.png'),
(new_search, localize(33053), 'search.png')):
yield build_item(li)
@Route.register
def search_history(plugin):
try:
@Route.register_delayed
def set_view():
xbmc.executebuiltin('Container.SetViewMode("51")')
h_list = PersistentList('search_history')
for i in h_list:
item = Listitem.from_dict(search, i, params={'search_query': i})
item.art.local_thumb('search.png')
yield item
else:
yield None
except:
yield False
@Route.register
def new_search(plugin):
q = utils.keyboard('Поиск')
if not q:
return False
return search(plugin, q)
@Route.register
def search(plugin, search_query, thumb=None):
try:
@Route.register_delayed
def set_view():
xbmc.executebuiltin('Container.SetViewMode("51")')
with PersistentList('search_history') as h_list:
if search_query in h_list:
del h_list[h_list.index(search_query)]
h_list.insert(0, search_query)
progress = xbmcgui.DialogProgress()
found_items = []
progress.create(localize(33054))
search_in = list(filter(lambda x: x.enabled, search_engines))
for p, se in zip(range(0, 100, 100 // len(search_in)), search_in):
progress.update(p, se.name)
found_items.extend(se().search(search_query))
# open('/tmp/t.t', 'w').write(repr(list(map(lambda x: x.url, found_items))))
res_items = []
for i in sorted(found_items, key=lambda x: x.seeders, reverse=True):
if '2160p' in i.title:
hd = f'{{2160p/{i.size}}} '
elif '1080p' in i.title:
hd = f'{{1080p/{i.size}}} '
elif '720p' in i.title:
hd = f'{{720p/{i.size}}} '
else:
hd = ''
item = Listitem.from_dict(
open_torrent,
f'{hd}{i.title} {i.size} ({i.seeders}/{i.leachers})',
params={'url': i.url, 'cookies': i.cookies, 'referer': i.referer}
)
if thumb:
item.art['thumb'] = thumb
else:
item.art.local_thumb(i.icon)
res_items.append(item)
progress.close()
return res_items
except:
return False
@Route.register
def open_torrent(plugin, url='--back--', cookies={}, referer='', path=''):
try:
if url == '--back--':
xbmc.executebuiltin('ActivateWindow("home")')
yield False
else:
@Route.register_delayed
def set_view():
xbmc.executebuiltin('Container.SetViewMode("51")')
tf = torrent_file_fetch(url, referer, cookies)
if not tf:
yield False
t_full_path = store_torrent_file(tf)
e = get_engine(t_full_path)
files = sorted(list(filter(
lambda x: x.name.startswith(path) and x.name.lower().endswith(
video_extensions), e.list_from_info(media_types=['video']))),
key=lambda x: x.name)
dirs = list(set(list(map(lambda x: x.name[len(path):].lstrip('/').split('/')[0], filter(
lambda x: len(x.name[len(path):].lstrip('/').split('/')) > 1, files)))))
for d in sorted(dirs):
item = Listitem.from_dict(open_torrent,
d,
params={'url': url, 'cookies': cookies, 'referer': referer,
'path': '{}/{}'.format(
path, d
) if path != '' else d}
)
item.art.local_thumb('folder.png')
yield item
for i in range(len(files)):
f = files[i]
p = f.name[len(path):].lstrip('/').split('/')
if len(p) == 1:
item = Listitem.from_dict(play_file,
f'{p[0]} ({f.size / 1024.0 / 1024.0 / 1024.0:.3f} GB)',
params={'t_full_path': t_full_path, 'f_index': f.index}
)
item.art.local_thumb('video.png')
yield item
else:
yield None
except:
yield False
@Script.register
def play_file(plugin, t_full_path, f_index):
try:
vl = VideoLoop(t_full_path)
vl.start(f_index)
return False
except:
import traceback
xbmc.log(f'PLAY exception: {traceback.format_exc()}')
return False
if __name__ == '__main__':
run()