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

67 lines
2.2 KiB
Python

from codequick.support import addon_data
from codequick.utils import urljoin_partial
from ..settings import option
import xbmcgui
import os
from hashlib import sha1
from gorrent2http import Engine
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_torrents_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_torrents_dir, t_fname)
def file_url(path):
if not path.startswith('file:'):
path = urljoin_partial('file:')(pathname2url(path))
return path
def get_engine(torrent_uri):
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_files=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, debug_alerts=False)
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_torrents_dir = os.path.join(storage_root, 'torrents')
storage_download_dir = os.path.join(storage_root, 'download')
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)