class TransmissionBroker: def __init__(self, config, persistence): self.conn = Client(config.address, port=config.port, user=config.user, password=config.password) self.secret = config.secret self.persistence = persistence @staticmethod def pretty_torrents_list(torrents): info_list = list() for torrent in torrents: info_list.append('%s: %s, %s : %d%%' % (torrent.id, torrent.name, torrent.status, torrent.percentDone * 100)) return '\n'.join(info_list) def retrieve_list(self, chat_id): torrents = self.conn.get_torrents() return TransmissionBroker.pretty_torrents_list(torrents) def add_torrent(self, chat_id, url): self.conn.add_torrent(url) def remove_torrent(self, chat_id, torrent_ids): # Check is not embedded to transmissionrpc module, so we have to do it ourselves missing_torrents = list() torrents = self.conn.get_torrents() for tid in torrent_ids: id_found = False for torrent in torrents: if tid == torrent.id: id_found = True break if not id_found: missing_torrents.append(tid) if len(missing_torrents) > 0: raise TransmissionError('Torrents %s not found' % missing_torrents) self.conn.remove_torrent(torrent_ids) def check_chat_authorization(self, chat_id): if not self.persistence.check_chat_id(chat_id): raise NotAuthorizedChatException() def authorize_chat(self, chat_id, secret): if self.secret == secret: self.persistence.add_chat_id(chat_id) return True else: return False
class TransmissionBroker: def __init__(self): print(f"user={TS_USER}, password={PASSWORD}") self.conn = Client(ADDRESS, PORT, user=TS_USER, password=PASSWORD) self.persistence = PERSISTENCE_FILE @staticmethod def pretty_torrents_list(torrents): info_list = [] for torrent in torrents: percent = torrent.percentDone * 100 info_list.append( f'{torrent.id}:{torrent.name}, {torrent.status}:{percent}' ) return '\n'.join(info_list) def retrieve_list(self, chat_id): torrents = self.conn.get_torrents() return TransmissionBroker.pretty_torrents_list(torrents) def add_torrent(self, chat_id, url): self.conn.add_torrent(url) def remove_torrent(self, chat_id, torrent_ids): # Check is not embedded to transmissionrpc module, so we have to do it ourselves missing_torrents = list() torrents = self.conn.get_torrents() for tid in torrent_ids: id_found = False for torrent in torrents: if tid == torrent.id: id_found = True break if not id_found: missing_torrents.append(tid) if len(missing_torrents) > 0: raise TransmissionError(f'Torrents {missing_torrents} not found') self.conn.remove_torrent(torrent_ids)
class TorC: MANAGE_ACTION_INIT = 1 MANAGE_ACTION_CLEAN = 2 TOR_TYPE_SINGLE = 1 TOR_TYPE_SERIES = 2 Videos = ["mp4", "mkv", "avi", "mpeg", "mpg", "mov", "mov", "wmv"] def __init__(self, user, password): #TODO: host/port/url self.client = Client(user=user, password=password) self.torrent_props = {} for t in self.client.get_torrents(): self.sort_by_episodes(t) def get_files(self, torrent = None): if not torrent: ret = [] for t, tinfo in self.client.get_files().items(): for fid, finfo in tinfo.items(): if finfo["name"].split(".")[-1] in TorC.Videos: ret.append(finfo) return ret return [x for x in list(torrent.files().values()) if x["name"].split(".")[-1] in TorC.Videos ] def download_dir(self): return self.client.session.download_dir def add_magnet(self, magnet): torrent = self.client.add_torrent(magnet) while not torrent.files(): time.sleep(.1) torrent.update() torrent.uploadLimit = 20 torrent.stop() torrent.update() self.manage_files(torrent, TorC.MANAGE_ACTION_INIT) return torrent def status(self): ret = [] for t in self.client.get_torrents(): t.update() tor_data = [t.name, float(t.rateDownload), []] if t.hashString not in self.torrent_props: tor_data[2].append({ "name": t.name, "completed": float(t.percentDone)*100}) else: files_list = t.files() for f in self.torrent_props[t.hashString]["episodes"]: fdict = files_list[f[0]] if fdict["size"] == fdict["completed"] or not fdict["selected"]: continue tor_data[2].append({"name": fdict["name"], "completed": float(100*fdict["completed"])/fdict["size"]}), ret.append(tor_data) return ret def clean(self): toremove = [] for t in self.client.get_torrents(): t.update() if t.hashString not in self.torrent_props: if t.status == "seeding": toremove.append(t.id) else: if not self.start_using_priority(t): toremove.append(t.id) if toremove: self.client.remove_torrent(toremove) def init_torrent_download(self, torrent): files_list = torrent.files() self.torrent_props[torrent.hashString] = {} #Skip all files: skip_files = {torrent.id : {}} for idx, f in enumerate(files_list): skip_files[torrent.id][idx] = {"selected": False} self.client.set_files(skip_files) video_files = { k:v for k,v in files_list.items() if v["name"].split(".")[-1].lower() in TorC.Videos } if len(video_files) > 1: sizesorted_files_list = sorted(video_files.values(), key=lambda x: x["size"], reverse=True) if sizesorted_files_list[0]["size"] > 5* sizesorted_files_list[1]["size"]: video_files = [sizesorted_files_list[0]] self.torrent_props[torrent.hashString]["episodes"] = self.sort_by_episodes(torrent) if len(video_files) > 2: #Type is series self.sort_by_episodes(torrent) self.start_using_priority(torrent) else: start_files = {} for idx,f in video_files.items(): start_files[torrent.id] = {idx:{"selected": True}} self.client.set_files(start_files) def start_using_priority(self, torrent, startat=0): #TODO startat function files_list = torrent.files() pos = startat f = files_list[self.torrent_props[torrent.hashString]["episodes"][pos][0]] while pos < len(self.torrent_props[torrent.hashString]["episodes"]) and f["size"] == f["completed"]: pos += 1 try: f = files_list[self.torrent_props[torrent.hashString]["episodes"][pos][0]] except IndexError: pass if pos == len(self.torrent_props[torrent.hashString]["episodes"]): return False mod_files = {torrent.id:{self.torrent_props[torrent.hashString]["episodes"][pos][0]:{"selected": True, "priority": "high"}}} if pos < len(self.torrent_props[torrent.hashString]["episodes"]) - 1: mod_files[torrent.id][self.torrent_props[torrent.hashString]["episodes"][pos+1][0]] = {"priority":"normal", "selected": True} self.client.set_files(mod_files) return True def manage_files(self, torrent, action): files_list = torrent.files() if action == TorC.MANAGE_ACTION_INIT: self.init_torrent_download(torrent) def sort_by_episodes(self, torrent): files_list = torrent.files() video_files = { k:v for k,v in files_list.items() if v["name"].split(".")[-1].lower() in TorC.Videos } p = re.compile("S\d+E\d+", re.IGNORECASE) episodes = { re.search(p, v["name"]).group().upper(): (k, v["name"]) for k,v in video_files.items()} self.torrent_props[torrent.hashString]= {"episodes": [y[1] for y in sorted(episodes.items(), key = lambda x: x[0])]} def start_all(self): for t in self.client.get_torrents(): t.start() def stop_all(self): for t in self.client.get_torrents(): t.stop()
class TransmissionClient(client.TorrentClient): """ Backend implementation for transmission """ config_key = "client_transmission" _torrent_list_args = None def __init__(self, host=None, port=None, user=None, password=None): super(TransmissionClient, self).__init__() if not host: host = config.get_default(self.config_key, "host", "localhost") self.host = host if not port: port = config.get_default(self.config_key, "port", DEFAULT_PORT, int) self.port = port if not user: user = config.get_default(self.config_key, "user", None) self.user = user if not password: password = config.get_default(self.config_key, "password", None) self.password = password self.client = None self.connect() def client_version(self): version = "{}.{} ({})".format(*self.client.server_version) return version def connect(self): try: self.client = Client(address=self.host, port=self.port, user=self.user, password=self.password) except TransmissionError as err: if err.original.code == 111: self.log.error("Failed to connect to transmission-daemon, is it running?") elif err.original.code == 113: self.log.error("No route to host") else: self.log.exception("Error connecting to transmission server") raise def add(self, data, download_dir=None): """ Add a torrent to the client :param data: Torrent data to load in :type data: TorrentData :param download_dir: Path on deluge server to store download :type download_dir: basestring :return: Status of successful load (according to deluge) :rtype: bool """ try: torrent = Torrent.from_str(data.torrent_data) try: self.torrent_status(torrent.info_hash) except KeyError: pass else: self.log.warn("Tried to load duplicate info hash: {}".format(torrent.info_hash)) return True torrent_data = b64encode(data.torrent_data) res = self.client.add_torrent(torrent_data, download_dir=download_dir) except TransmissionError as err: try: msg = err._message except AttributeError: msg = err.message if "duplicate torrent" in msg: self.log.warning("Tried to add duplicate torrent file") return True self.log.exception(err) return False return res torrent_add = add def current_speeds(self): """ Fetch the speeds from the session instance :return: Upload, Download speeds in bytes/s :rtype: tuple """ ses = self.client.session_stats() return ses.uploadSpeed, ses.downloadSpeed def torrent_list(self): """ Get a list of currently loaded torrents from the client :return: :rtype: """ if not self._torrent_list_args: self._torrent_list_args = get_arguments('torrent-get', self.client.rpc_version) self._torrent_list_args.extend(['seeders', 'peersKnown', 'peersGettingFromUs', 'peersSendingToUs', 'isPrivate']) torrents = self.client.get_torrents(arguments=self._torrent_list_args) torrent_data = list() for torrent in torrents: data = client.ClientTorrentData( info_hash=torrent.hashString, name=torrent.name, ratio=torrent.ratio, up_rate=torrent.rateUpload, dn_rate=torrent.rateDownload, up_total=torrent.uploadedEver, dn_total=torrent.downloadedEver, size=torrent.sizeWhenDone, size_completed=torrent.sizeWhenDone-torrent.leftUntilDone, # TODO peer values are wrong peers=torrent.peersGettingFromUs, total_peers=0, seeders=torrent.peersSendingToUs, total_seeders=0, priority=torrent.priority, private=torrent.isPrivate, state=torrent.status, progress=torrent.progress ) torrent_data.append(data) return torrent_data def torrent_remove(self, torrent_id, remove_data=False): """ Remove a torrent from the backend client via its torrentID supplied by the torrent daemon :param remove_data: Remove the torrent data file as well as .torrent :type remove_data: bool :param torrent_id: TorrentID provided by transmission :type torrent_id: int """ self.client.remove_torrent(torrent_id, delete_data=remove_data) def torrent_peers(self, info_hash): torrent = self.client.get_torrent(info_hash, arguments=['id', 'hashString', 'peers']) peers = [] session = Session() # TODO country code lookup for peer in torrent.peers: peers.append({ 'client': peer['clientName'], 'down_speed': peer['rateToClient'], 'up_speed': peer['rateToPeer'], 'progress': peer['progress'], 'ip': peer['address'], 'country': geoip.find_country_code(session, peer['address']) }) return peers def torrent_start(self, info_hash): self.client.start_torrent(info_hash) return True def torrent_pause(self, info_hash): self.client.stop(info_hash) return True def torrent_files(self, info_hash): files = [] file_set = self.client.get_files(info_hash) for v in file_set.values(): for file_info in [f for f in v.values()]: files.append(client.ClientFileData( path=file_info['name'], progress=file_info['size'] - file_info['completed'], size=file_info['size'], priority=file_info['priority'] )) break return files def torrent_speed(self, info_hash): speed = self.client.get_torrent(info_hash, arguments=['id', 'hashString', 'rateDownload', 'rateUpload']) return speed.rateDownload, speed.rateUpload def disconnect(self): return True def torrent_status(self, info_hash): key_map = { 'info_hash': 'hashString', 'up_rate': 'rateUpload', 'dn_rate': 'rateDownload', 'up_total': 'uploadedEver', 'dn_total': 'downloadedEver', 'size': 'totalSize', 'size_completed': 'percentDone', # wrong 'seeders': lambda t: len(t.peers), 'total_seeders': lambda t: len(t.peers), 'peers': 'peersConnected', 'total_peers': lambda t: len(t.peers), 'priority': 'queue_position', 'private': 'isPrivate', 'state': 'status', 'progress': '', 'tracker_status': '', 'next_announce': lambda t: t.trackerStats[0]['nextAnnounceTime'], 'save_path': 'downloadDir', 'piece_length': 'pieceSize', 'num_pieces': 'pieceCount', 'time_added': 'addedDate', 'distributed_copies': lambda t: functools.reduce(lambda a,b: a+b, [p['progress'] for p in t.peers], 0), 'active_time': '', #'seeding_time': 'secondsSeeding', Not found in my version? trans 2.8.4 'num_files': lambda t: len(torrent.files()), 'queue_position': 'queue_position' } torrent = self.client.get_torrent(info_hash) detail = client.ClientTorrentDataDetail(info_hash=info_hash) for key in detail.key_list: val = key_map.get(key, None) if val: if callable(val): detail[key] = val(torrent) else: detail[key] = getattr(torrent, val) return detail def torrent_recheck(self, info_hash): self.client.verify_torrent(info_hash) return True def torrent_reannounce(self, info_hash): self.client.reannounce_torrent(info_hash) return True def torrent_queue_up(self, info_hash): self.client.queue_up(info_hash) return True def torrent_queue_down(self, info_hash): self.client.queue_down(info_hash) return True def torrent_queue_top(self, info_hash): self.client.queue_top(info_hash) return True def torrent_queue_bottom(self, info_hash): self.client.queue_bottom(info_hash) return True def torrent_move_data(self, info_hash, dest): self.client.move_torrent_data(info_hash, dest) return True
class TransmissionClient(client.TorrentClient): """ Backend implementation for transmission """ config_key = "client_transmission" _torrent_list_args = None def __init__(self, host=None, port=None, user=None, password=None): super(TransmissionClient, self).__init__() if not host: host = config.get_default(self.config_key, "host", "localhost") self.host = host if not port: port = config.get_default(self.config_key, "port", DEFAULT_PORT, int) self.port = port if not user: user = config.get_default(self.config_key, "user", None) self.user = user if not password: password = config.get_default(self.config_key, "password", None) self.password = password self.client = None self.connect() def client_version(self): version = "{}.{} ({})".format(*self.client.server_version) return version def connect(self): try: self.client = Client(address=self.host, port=self.port, user=self.user, password=self.password) except TransmissionError as err: if err.original.code == 111: self.log.error( "Failed to connect to transmission-daemon, is it running?") elif err.original.code == 113: self.log.error("No route to host") else: self.log.exception("Error connecting to transmission server") raise def add(self, data, download_dir=None): """ Add a torrent to the client :param data: Torrent data to load in :type data: TorrentData :param download_dir: Path on deluge server to store download :type download_dir: basestring :return: Status of successful load (according to deluge) :rtype: bool """ try: torrent = Torrent.from_str(data.torrent_data) try: self.torrent_status(torrent.info_hash) except KeyError: pass else: self.log.warn("Tried to load duplicate info hash: {}".format( torrent.info_hash)) return True torrent_data = b64encode(data.torrent_data) res = self.client.add_torrent(torrent_data, download_dir=download_dir) except TransmissionError as err: try: msg = err._message except AttributeError: msg = err.message if "duplicate torrent" in msg: self.log.warning("Tried to add duplicate torrent file") return True self.log.exception(err) return False return res torrent_add = add def current_speeds(self): """ Fetch the speeds from the session instance :return: Upload, Download speeds in bytes/s :rtype: tuple """ ses = self.client.session_stats() return ses.uploadSpeed, ses.downloadSpeed def torrent_list(self): """ Get a list of currently loaded torrents from the client :return: :rtype: """ if not self._torrent_list_args: self._torrent_list_args = get_arguments('torrent-get', self.client.rpc_version) self._torrent_list_args.extend([ 'seeders', 'peersKnown', 'peersGettingFromUs', 'peersSendingToUs', 'isPrivate' ]) torrents = self.client.get_torrents(arguments=self._torrent_list_args) torrent_data = list() for torrent in torrents: data = client.ClientTorrentData( info_hash=torrent.hashString, name=torrent.name, ratio=torrent.ratio, up_rate=torrent.rateUpload, dn_rate=torrent.rateDownload, up_total=torrent.uploadedEver, dn_total=torrent.downloadedEver, size=torrent.sizeWhenDone, size_completed=torrent.sizeWhenDone - torrent.leftUntilDone, # TODO peer values are wrong peers=torrent.peersGettingFromUs, total_peers=0, seeders=torrent.peersSendingToUs, total_seeders=0, priority=torrent.priority, private=torrent.isPrivate, state=torrent.status, progress=torrent.progress) torrent_data.append(data) return torrent_data def torrent_remove(self, torrent_id, remove_data=False): """ Remove a torrent from the backend client via its torrentID supplied by the torrent daemon :param remove_data: Remove the torrent data file as well as .torrent :type remove_data: bool :param torrent_id: TorrentID provided by transmission :type torrent_id: int """ self.client.remove_torrent(torrent_id, delete_data=remove_data) def torrent_peers(self, info_hash): torrent = self.client.get_torrent( info_hash, arguments=['id', 'hashString', 'peers']) peers = [] session = Session() # TODO country code lookup for peer in torrent.peers: peers.append({ 'client': peer['clientName'], 'down_speed': peer['rateToClient'], 'up_speed': peer['rateToPeer'], 'progress': peer['progress'], 'ip': peer['address'], 'country': geoip.find_country_code(session, peer['address']) }) return peers def torrent_start(self, info_hash): self.client.start_torrent(info_hash) return True def torrent_pause(self, info_hash): self.client.stop(info_hash) return True def torrent_files(self, info_hash): files = [] file_set = self.client.get_files(info_hash) for v in list(file_set.values()): for file_info in [f for f in list(v.values())]: files.append( client.ClientFileData(path=file_info['name'], progress=file_info['size'] - file_info['completed'], size=file_info['size'], priority=file_info['priority'])) break return files def torrent_speed(self, info_hash): speed = self.client.get_torrent( info_hash, arguments=['id', 'hashString', 'rateDownload', 'rateUpload']) return speed.rateDownload, speed.rateUpload def disconnect(self): return True def torrent_status(self, info_hash): key_map = { 'info_hash': 'hashString', 'up_rate': 'rateUpload', 'dn_rate': 'rateDownload', 'up_total': 'uploadedEver', 'dn_total': 'downloadedEver', 'size': 'totalSize', 'size_completed': 'percentDone', # wrong 'seeders': lambda t: len(t.peers), 'total_seeders': lambda t: len(t.peers), 'peers': 'peersConnected', 'total_peers': lambda t: len(t.peers), 'priority': 'queue_position', 'private': 'isPrivate', 'state': 'status', 'progress': '', 'tracker_status': '', 'next_announce': lambda t: t.trackerStats[0]['nextAnnounceTime'], 'save_path': 'downloadDir', 'piece_length': 'pieceSize', 'num_pieces': 'pieceCount', 'time_added': 'addedDate', 'distributed_copies': lambda t: functools.reduce(lambda a, b: a + b, [p['progress'] for p in t.peers], 0), 'active_time': '', #'seeding_time': 'secondsSeeding', Not found in my version? trans 2.8.4 'num_files': lambda t: len(torrent.files()), 'queue_position': 'queue_position' } torrent = self.client.get_torrent(info_hash) detail = client.ClientTorrentDataDetail(info_hash=info_hash) for key in detail.key_list: val = key_map.get(key, None) if val: if callable(val): detail[key] = val(torrent) else: detail[key] = getattr(torrent, val) return detail def torrent_recheck(self, info_hash): self.client.verify_torrent(info_hash) return True def torrent_reannounce(self, info_hash): self.client.reannounce_torrent(info_hash) return True def torrent_queue_up(self, info_hash): self.client.queue_up(info_hash) return True def torrent_queue_down(self, info_hash): self.client.queue_down(info_hash) return True def torrent_queue_top(self, info_hash): self.client.queue_top(info_hash) return True def torrent_queue_bottom(self, info_hash): self.client.queue_bottom(info_hash) return True def torrent_move_data(self, info_hash, dest): self.client.move_torrent_data(info_hash, dest) return True
class TransmissionClient(BitTorrentClient): def __init__(self): BitTorrentClient.__init__(self, TRANSMISSION_CLIENT) self.dict_client = {} def connect(self, address, port, user, password): try: self.client = TClient(address, port, user, password) except TransmissionError as err: raise BTCexception(" {} TransmissionError {}".format(address, err)) except socket.timeout as err: raise BTCexception(" {} Socket error {}".format(address, err)) else: self.dict_client['name'] = address def get_torrents(self): tr_torrents = self.client.get_torrents() self.dict_client['torrents'] = [] for _torrent in tr_torrents: _torrent_dict = { u'name': _torrent.name, u'hash': _torrent.hashString, u'progress': _torrent.progress, u'status': _torrent.status, u'date_active': _torrent.date_active, u'date_added': _torrent.date_added, u'date_started': _torrent.date_started, u'date_done': _torrent.date_done, u'eta': -1, u'error': _torrent.error, u'errorString': _torrent.errorString[:350], } #TODO: Warning eta can return exception ValuerError try: _torrent_dict[u'eta'] = timedelta.total_seconds(_torrent.eta) except ValueError: pass self.dict_client['torrents'].append(_torrent_dict) if _torrent_dict[u'error'] == 3: _torrent_dict[u'status'] = u'error_torrentclient' if _torrent_dict[u'error'] in [1, 2]: _torrent_dict[u'status'] = u'error_torrenttracker' return self.dict_client def add_torrent(self, torrent_path): pass def remove(self, torrent_hash): self.client.remove_torrent(torrent_hash) def verify(self, torrent_hash): self.client.verify_torrent(torrent_hash) def free_space_bytes(self): free_space = None try: session = self.client.get_session() except TransmissionError as err: raise BTCexception(" {} TransmissionError {}".format( self.dict_client['name'], err)) except socket.timeout as err: raise BTCexception(" {} Socket error {}".format( self.dict_client['name'], err)) except Exception as err: raise BTCexception("{}: {}".format(self.dict_client['name'], err.message)) else: free_space = session.download_dir_free_space return free_space
dbg('Total trackers: {}'.format(len(trackers))) if not trackers: lg("No trackers loaded, nothing to do") exit(1) try: tc = Client(**client) except: if not config['err_on_connect']: exit() print("Unable to connect to Transmission: ", sys.exc_info()[0]) raise torrents = tc.get_torrents() dbg('{} torrents total'.format(len(torrents))) for t in torrents: if config['status_filter'] and not t.status in config['status_filter']: dbg('{}: skipping due to status filter'.format(t.name)) continue ttrk = set(()) for trk in t.trackers: ttrk.add(trk['announce']) diff = trackers - ttrk if diff:
#!/usr/bin/env python3 from transmissionrpc import Client if __name__ == "__main__": client = Client('localhost', port=9091) torrents = client.get_torrents() for torrent in torrents: if "S02E05" in torrent.name: client.remove_torrent(torrent.id)