plugin.video.torrenter3/resources/lib/utils/__init__.py

46 lines
1.6 KiB
Python

from codequick.support import addon_data # @UnresolvedImport
from urlquick import urljoin # @UnresolvedImport
from ..settings import option, storage_toorents_dir, storage_download_dir
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
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)
if os.path.exists(full_path):
with open(full_path, 'rb') as f:
if sha1(f.read()).hexdigest() == h:
return full_path
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)
def file_url(path):
if not path.startswith('file:'):
path = urljoin('file:', pathname2url(path))
return path
def get_engine(torrent_uri):
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)
while not option['storage_dir']:
dialog = xbmcgui.Dialog()
dialog.ok(localize(33000), localize(33051))
addon_data.openSettings()