Extended torrents plugin

This commit is contained in:
Fabio Manganiello 2018-10-22 18:51:00 +02:00
parent 7375fb74fc
commit ab51c1d1bc
2 changed files with 44 additions and 4 deletions

View file

@ -26,6 +26,7 @@ class TorrentPlugin(Plugin):
default_torrent_ports = [6881, 6891] default_torrent_ports = [6881, 6891]
torrent_state = {} torrent_state = {}
transfers = {}
def __init__(self, download_dir=None, torrent_ports=[], *argv, **kwargs): def __init__(self, download_dir=None, torrent_ports=[], *argv, **kwargs):
""" """
@ -114,6 +115,7 @@ class TorrentPlugin(Plugin):
else: else:
raise RuntimeError('download_dir not specified') raise RuntimeError('download_dir not specified')
download_dir = os.path.abspath(os.path.expanduser(download_dir))
os.makedirs(download_dir, exist_ok=True) os.makedirs(download_dir, exist_ok=True)
info = {} info = {}
torrent_file = None torrent_file = None
@ -126,13 +128,15 @@ class TorrentPlugin(Plugin):
elif torrent.startswith('http://') or torrent.startswith('https://'): elif torrent.startswith('http://') or torrent.startswith('https://'):
import requests import requests
response = requests.get(torrent, allow_redirects=True) response = requests.get(torrent, allow_redirects=True)
torrent_file = os.path.join(os.path.expanduser(download_dir), torrent_file = os.path.join(download_dir,
self._generate_rand_filename()) self._generate_rand_filename())
with open(torrent_file, 'wb') as f: with open(torrent_file, 'wb') as f:
f.write(response.content) f.write(response.content)
else: else:
torrent_file = os.path.expanduser(torrent) torrent_file = os.path.abspath(os.path.expanduser(torrent))
if not os.path.isfile(torrent_file):
raise RuntimeError('{} is not a valid torrent file'.format(torrent_file))
if torrent_file: if torrent_file:
file_info = lt.torrent_info(torrent_file) file_info = lt.torrent_info(torrent_file)
@ -163,11 +167,12 @@ class TorrentPlugin(Plugin):
status = transfer.status() status = transfer.status()
files = [] files = []
self.transfers[torrent] = transfer
self.torrent_state[torrent] = { self.torrent_state[torrent] = {
'url': torrent, 'url': torrent,
'title': info['name'], 'title': info['name'],
'trackers': info['trackers'], 'trackers': info['trackers'],
'save_path': info['save_path'], 'save_path': download_dir,
} }
bus.post(TorrentDownloadStartEvent(**self.torrent_state[torrent])) bus.post(TorrentDownloadStartEvent(**self.torrent_state[torrent]))
@ -214,13 +219,48 @@ class TorrentPlugin(Plugin):
bus.post(TorrentStateChangeEvent(**self.torrent_state[torrent], files=files)) bus.post(TorrentStateChangeEvent(**self.torrent_state[torrent], files=files))
del self.torrent_state[torrent] del self.torrent_state[torrent]
del self.transfers[torrent]
return files return files
@action @action
def get_status(self): def get_status(self):
"""
Get the status of the current transfers.
:returns: A dictionary in the format torrent_url -> status
"""
return self.torrent_state return self.torrent_state
@action
def pause(self, torrent_url):
"""
Pause a torrent transfer.
:param torrent_url: Torrent URL as returned from `get_status()`
:type torrent_url: str
"""
if torrent_url not in self.transfers:
return (None, "No transfer in progress for {}".format(torrent_url))
self.transfers[torrent_url].pause()
@action
def resume(self, torrent_url):
"""
Resume a torrent transfer.
:param torrent_url: Torrent URL as returned from `get_status()`
:type torrent_url: str
"""
if torrent_url not in self.transfers:
return (None, "No transfer in progress for {}".format(torrent_url))
self.transfers[torrent_url].resume()
def _generate_rand_filename(self, length=16): def _generate_rand_filename(self, length=16):
name = '' name = ''
for i in range(0, length): for i in range(0, length):

View file

@ -97,7 +97,7 @@ class VideoOmxplayerPlugin(Plugin):
response = torrents.download(resource, download_dir=self.download_dir) response = torrents.download(resource, download_dir=self.download_dir)
resources = [f for f in response.output if self._is_video_file(f)] resources = [f for f in response.output if self._is_video_file(f)]
if resources: if resources:
self.videos_queue = resources self.videos_queue = sorted(resources)
resource = self.videos_queue.pop(0) resource = self.videos_queue.pop(0)
else: else:
raise RuntimeError('Unable to download torrent {}'.format(resource)) raise RuntimeError('Unable to download torrent {}'.format(resource))