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

55 lines
2.2 KiB
Python
Raw Normal View History

2019-05-04 22:38:06 +03:00
from codequick.support import addon_data # @UnresolvedImport
2019-05-05 10:16:32 +03:00
from codequick.listing import local_image # @UnresolvedImport
2019-05-04 22:38:06 +03:00
from urlquick import urljoin # @UnresolvedImport
2019-05-05 13:17:16 +03:00
from ..settings import option
2019-05-04 22:38:06 +03:00
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 notify(heading, message, icon=xbmcgui.NOTIFICATION_INFO):
n = xbmcgui.Dialog()
n.notification(heading, message, icon, time=5, sound=True)
2019-05-04 22:38:06 +03:00
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()
2019-05-05 13:17:16 +03:00
storage_root = os.path.abspath(os.path.join(option['storage_dir'], 'Torrenter3'))
storage_toorents_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_download_dir): os.makedirs(storage_download_dir)