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

68 lines
2.0 KiB
Python
Raw Normal View History

2022-03-20 00:15:29 +03:00
from codequick.support import addon_data
from codequick.utils import urljoin_partial
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
2022-03-20 00:15:29 +03:00
from gorrent2http import Engine
2022-03-14 09:16:22 +03:00
from urllib.request import pathname2url
2019-05-04 22:38:06 +03:00
def notify(heading, message, icon=xbmcgui.NOTIFICATION_INFO):
n = xbmcgui.Dialog()
n.notification(heading, message, icon, time=5, sound=True)
2022-03-14 09:16:22 +03:00
2019-05-04 22:38:06 +03:00
def localize(sid):
return addon_data.getLocalizedString(sid)
2022-03-14 09:16:22 +03:00
2019-05-04 22:38:06 +03:00
def store_torrent_file(file_bytes):
h = sha1(file_bytes).hexdigest()
t_fname = '{}.torrent'.format(h)
2022-03-14 09:16:22 +03:00
full_path = os.path.join(storage_torrents_dir, t_fname)
2019-05-04 22:38:06 +03:00
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
2022-03-14 09:16:22 +03:00
2019-05-04 22:38:06 +03:00
def torrent_full_path(t_fname):
2022-03-14 09:16:22 +03:00
return os.path.join(storage_torrents_dir, t_fname)
2019-05-04 22:38:06 +03:00
def file_url(path):
if not path.startswith('file:'):
2022-03-14 09:16:22 +03:00
path = urljoin_partial('file:')(pathname2url(path))
2019-05-04 22:38:06 +03:00
return path
2022-03-14 09:16:22 +03:00
2019-05-04 22:38:06 +03:00
def get_engine(torrent_uri):
2022-03-14 09:16:22 +03:00
if option.get_boolean('use_socks_for_trackers'): # @UndefinedVariable
proxy = {
'host': option['socks_ip'],
'port': int(option['socks_port'])
2022-03-14 09:16:22 +03:00
}
else:
proxy = None
return Engine(uri=file_url(torrent_uri),
download_path=storage_download_dir,
keep_files=False,
2022-03-14 09:16:22 +03:00
listen_port=6881,
proxy=proxy,
debug_alerts=False)
2022-03-14 09:16:22 +03:00
2019-05-04 22:38:06 +03:00
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'))
2022-03-14 09:16:22 +03:00
storage_torrents_dir = os.path.join(storage_root, 'torrents')
2019-05-05 13:17:16 +03:00
storage_download_dir = os.path.join(storage_root, 'download')
2022-03-14 09:16:22 +03:00
if not os.path.exists(storage_torrents_dir): os.makedirs(storage_torrents_dir)
2019-05-05 13:17:16 +03:00
if not os.path.exists(storage_download_dir): os.makedirs(storage_download_dir)