mirror of https://github.com/evilhero/mylar
99 lines
3.0 KiB
Python
Executable File
99 lines
3.0 KiB
Python
Executable File
import os
|
|
import mylar
|
|
from mylar import logger
|
|
from lib.transmissionrpc import Client
|
|
|
|
|
|
class TorrentClient(object):
|
|
def __init__(self):
|
|
self.conn = None
|
|
|
|
def connect(self, host, username, password):
|
|
if self.conn is not None:
|
|
return self.conn
|
|
|
|
if not host:
|
|
return False
|
|
try:
|
|
if username and password:
|
|
self.conn = Client(
|
|
host,
|
|
user=username,
|
|
password=password
|
|
)
|
|
else:
|
|
self.conn = Client(host)
|
|
except:
|
|
logger.error('Could not connect to %h' % host)
|
|
return False
|
|
|
|
return self.conn
|
|
|
|
def find_torrent(self, hash):
|
|
try:
|
|
return self.conn.get_torrent(hash)
|
|
except KeyError:
|
|
logger.error('torrent %s does not exist')
|
|
return False
|
|
|
|
def get_torrent(self, torrent):
|
|
torrent = self.conn.get_torrent(torrent.hashString)
|
|
torrent_files = []
|
|
torrent_directory = os.path.normpath(torrent.downloadDir)
|
|
|
|
for f in torrent.files().itervalues():
|
|
if not os.path.normpath(f['name']).startswith(torrent_directory):
|
|
file_path = os.path.join(torrent_directory,
|
|
f['name'].lstrip('/'))
|
|
else:
|
|
file_path = f['name']
|
|
|
|
torrent_files.append(file_path)
|
|
|
|
torrent_info = {
|
|
'hash': torrent.hashString,
|
|
'name': torrent.name,
|
|
'folder': torrent.downloadDir,
|
|
'completed': torrent.progress == 100,
|
|
'files': torrent_files,
|
|
'upload_total': torrent.uploadedEver,
|
|
'download_total': torrent.downloadedEver,
|
|
'ratio': torrent.ratio,
|
|
'total_filesize': torrent.sizeWhenDone,
|
|
'time_started': torrent.date_started
|
|
}
|
|
logger.debug(torrent_info)
|
|
return torrent_info if torrent_info else False
|
|
|
|
def start_torrent(self, torrent):
|
|
return torrent.start()
|
|
|
|
def stop_torrent(self, torrent):
|
|
return torrent.stop()
|
|
|
|
def load_torrent(self, filepath):
|
|
torrent = self.conn.add_torrent(filepath,
|
|
download_dir=mylar.CHECK_FOLDER)
|
|
|
|
torrent.start()
|
|
return self.get_torrent(torrent)
|
|
|
|
def delete_torrent(self, torrent):
|
|
deleted = []
|
|
files = torrent.files()
|
|
for file_item in files.itervalues():
|
|
file_path = os.path.join(torrent.downloadDir,
|
|
file_item['name'])
|
|
deleted.append(file_path)
|
|
|
|
if len(files) > 1:
|
|
torrent_path = os.path.join(torrent.downloadDir, torrent.name)
|
|
for path, _, _ in os.walk(torrent_path, topdown=False):
|
|
deleted.append(path)
|
|
|
|
if self.conn.remove_torrent(torrent.hashString, delete_data=True):
|
|
return deleted
|
|
else:
|
|
logger.error('Unable to delete %s' % torrent.name)
|
|
return []
|