for hsh in args: if hsh in torr_list: torr = torr_list[hsh] torr.stop( ) torr.recheck( ) if ( torr.status.started and not torr.status.paused ) or torr.status.error: torr.start( not ( torr.status.queued or torr.status.error ) ) elif opts.action == "torrent_remove": if opts.verbose: torrs = utorrent.resolve_torrent_hashes( args ) else: torrs = args print_console( "Removing " + ", ".join( torrs ) + "..." ) if utorrent.api_version == LinuxServer.api_version: utorrent.torrent_remove( args, opts.with_data or opts.force, opts.with_torrent or opts.force ) else: utorrent.torrent_remove( args, opts.with_data or opts.force ) elif opts.action == "torrent_info": tors = utorrent.torrent_list( ) files = utorrent.file_list( args ) infos = utorrent.torrent_info( args ) for hsh, fls in files.items( ): print_console( tors[hsh].verbose_str( opts.format ) if opts.verbose else tors[hsh] ) print_console( level1 + ( infos[hsh].verbose_str( ) if opts.verbose else str( infos[hsh] ) ) ) print_console( level1 + "Files ({}):".format( len( fls ) ) ) for f in fls: print_console( level2 + ( f.verbose_str( ) if opts.verbose else str( f ) ) ) print_console( level1 + "Trackers:" ) for tr in infos[hsh].trackers:
class UtorrentMgmt(object): def __init__(self, host, username, password): self._conn = Connection(host, username, password).utorrent(api='falcon') def get_stats(self, time_frame='1d'): res = self._conn.xfer_history_get() try: excl_local = self._conn.settings_get()["net.limit_excludeslocal"] except self._conn.uTorrentError: return "Failed to fetch Statistics." torrents = self._conn.torrent_list() today_start = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) period = len(res["daily_download"]) period_start = today_start - datetime.timedelta(days=period - 1) down_total_local = sum(res["daily_local_download"]) down_total = sum( res["daily_download"]) - (down_total_local if excl_local else 0) up_total_local = sum(res["daily_local_upload"]) up_total = sum( res["daily_upload"]) - (down_total_local if excl_local else 0) period_added_torrents = { k: v for k, v in torrents.items() if v.added_on >= period_start } period_completed_torrents = { k: v for k, v in torrents.items() if v.completed_on >= period_start } stat_msg = "" if time_frame == '31d': stat_msg += "Last {} days:\n".format(period) stat_msg += level1 + "Downloaded: {} (+{} local)\n".format( utorrent_module.human_size(down_total), utorrent_module.human_size(down_total_local)) stat_msg += level1 + " Uploaded: {} (+{} local)\n".format( utorrent_module.human_size(up_total), utorrent_module.human_size(up_total_local)) stat_msg += level1 + " Total: {} (+{} local)\n".format( utorrent_module.human_size(down_total + up_total), utorrent_module.human_size(down_total_local + up_total_local)) stat_msg += level1 + "Ratio: {:.2f}\n".format( up_total / down_total) stat_msg += level1 + "Added torrents: {}\n".format( len(period_added_torrents)) stat_msg += level1 + "Completed torrents: {}\n".format( len(period_completed_torrents)) elif time_frame == '1d': down_day_local = res["daily_local_download"][0] down_day = res["daily_download"][0] - (down_day_local if excl_local else 0) up_day_local = res["daily_local_upload"][0] up_day = res["daily_upload"][0] - (up_day_local if excl_local else 0) today_added_torrents = { k: v for k, v in torrents.items() if v.added_on >= today_start } today_completed_torrents = { k: v for k, v in torrents.items() if v.completed_on >= today_start } stat_msg += "Today:" stat_msg += level1 + "Downloaded: {} (+{} local)\n".format( utorrent_module.human_size(down_day), utorrent_module.human_size(down_day_local)) stat_msg += level1 + " Uploaded: {} (+{} local)\n".format( utorrent_module.human_size(up_day), utorrent_module.human_size(up_day_local)) stat_msg += level1 + " Total: {} (+{} local)\n".format( utorrent_module.human_size(down_day + up_day), utorrent_module.human_size(down_day_local + up_day_local)) stat_msg += level1 + "Ratio: {:.2f}\n".format(up_day / down_day) stat_msg += level1 + "Added torrents: {}\n".format( len(today_added_torrents)) stat_msg += level1 + "Completed torrents: {}\n".format( len(today_completed_torrents)) return stat_msg @run_in_thread def remove(self): def older_than_min(t_data): min_age = time.time() - 259200 return True if time.mktime( t_data.completed_on.timetuple()) < min_age else False sort_field = 'ratio' t_list = sorted(self._conn.torrent_list().items(), key=lambda x: getattr(x[1], sort_field), reverse=True) t_score = [] for _t in t_list: torrent_score = { 'name': _t[1].name, 'data': _t[1], 'hash': _t[0], 'score': 100 } if torrent_score['data'].label.lower() == 'no_delete': torrent_score['score'] -= 100 continue elif torrent_score['data'].ratio < 3: torrent_score['score'] -= 80 elif torrent_score['data'].ratio < 3 and older_than_min( torrent_score['data']): torrent_score['score'] -= 40 elif torrent_score['data'].ul_speed > 0: torrent_score['score'] -= 20 elif older_than_min(torrent_score['data']): torrent_score['score'] -= 10 t_score.append(torrent_score) to_delete = sorted(t_score, key=lambda k: k['score'], reverse=True) self._conn.torrent_stop(to_delete[0]['hash']) self._conn.torrent_remove(to_delete[0]['hash'], with_data=True) removed_q.put(to_delete[0]['data'])
if hsh in torr_list: torr = torr_list[hsh] torr.stop() torr.recheck() if (torr.status.started and not torr.status.paused) or torr.status.error: torr.start(not (torr.status.queued or torr.status.error)) elif opts.action == "torrent_remove": if opts.verbose: torrs = utorrent.resolve_torrent_hashes(args) else: torrs = args print_console("Removing " + ", ".join(torrs) + "...") if utorrent.api_version == LinuxServer.api_version: utorrent.torrent_remove(args, opts.with_data or opts.force, opts.with_torrent or opts.force) else: utorrent.torrent_remove(args, opts.with_data or opts.force) elif opts.action == "torrent_info": tors = utorrent.torrent_list() files = utorrent.file_list(args) infos = utorrent.torrent_info(args) for hsh, fls in files.items(): print_console(tors[hsh].verbose_str(opts.format) if opts. verbose else tors[hsh]) print_console(level1 + ( infos[hsh].verbose_str() if opts.verbose else str(infos[hsh]))) print_console(level1 + "Files ({}):".format(len(fls))) for f in fls: print_console(level2 +
class UtorrentMgmt(object): def __init__(self, host, username, password): self._conn = Connection(host, username, password).utorrent(api='falcon') def get_stats(self, time_frame='1d'): res = self._conn.xfer_history_get() try: excl_local = self._conn.settings_get()["net.limit_excludeslocal"] except self._conn.uTorrentError: return "Failed to fetch Statistics." torrents = self._conn.torrent_list() today_start = datetime.datetime.now().replace(hour=0, minute=0, second=0, microsecond=0) period = len(res["daily_download"]) period_start = today_start - datetime.timedelta(days=period - 1) down_total_local = sum(res["daily_local_download"]) down_total = sum(res["daily_download"]) - (down_total_local if excl_local else 0) up_total_local = sum(res["daily_local_upload"]) up_total = sum(res["daily_upload"]) - (down_total_local if excl_local else 0) period_added_torrents = {k: v for k, v in torrents.items() if v.added_on >= period_start} period_completed_torrents = {k: v for k, v in torrents.items() if v.completed_on >= period_start} stat_msg = "" if time_frame == '31d': stat_msg += "Last {} days:\n".format(period) stat_msg += level1 + "Downloaded: {} (+{} local)\n".format(utorrent_module.human_size(down_total), utorrent_module.human_size(down_total_local)) stat_msg += level1 + " Uploaded: {} (+{} local)\n".format(utorrent_module.human_size(up_total), utorrent_module.human_size(up_total_local)) stat_msg += level1 + " Total: {} (+{} local)\n".format( utorrent_module.human_size(down_total + up_total), utorrent_module.human_size( down_total_local + up_total_local)) stat_msg += level1 + "Ratio: {:.2f}\n".format(up_total / down_total) stat_msg += level1 + "Added torrents: {}\n".format(len(period_added_torrents)) stat_msg += level1 + "Completed torrents: {}\n".format(len(period_completed_torrents)) elif time_frame == '1d': down_day_local = res["daily_local_download"][0] down_day = res["daily_download"][0] - (down_day_local if excl_local else 0) up_day_local = res["daily_local_upload"][0] up_day = res["daily_upload"][0] - (up_day_local if excl_local else 0) today_added_torrents = {k: v for k, v in torrents.items() if v.added_on >= today_start} today_completed_torrents = {k: v for k, v in torrents.items() if v.completed_on >= today_start} stat_msg += "Today:" stat_msg += level1 + "Downloaded: {} (+{} local)\n".format(utorrent_module.human_size(down_day), utorrent_module.human_size(down_day_local)) stat_msg += level1 + " Uploaded: {} (+{} local)\n".format(utorrent_module.human_size(up_day), utorrent_module.human_size(up_day_local)) stat_msg += level1 + " Total: {} (+{} local)\n".format(utorrent_module.human_size(down_day + up_day), utorrent_module.human_size( down_day_local + up_day_local)) stat_msg += level1 + "Ratio: {:.2f}\n".format(up_day / down_day) stat_msg += level1 + "Added torrents: {}\n".format(len(today_added_torrents)) stat_msg += level1 + "Completed torrents: {}\n".format(len(today_completed_torrents)) return stat_msg @run_in_thread def remove(self): def older_than_min(t_data): min_age = time.time() - 259200 return True if time.mktime(t_data.completed_on.timetuple()) < min_age else False sort_field = 'ratio' t_list = sorted(self._conn.torrent_list().items(), key=lambda x: getattr(x[1], sort_field), reverse=True) t_score = [] for _t in t_list: torrent_score = {'name': _t[1].name, 'data': _t[1], 'hash': _t[0], 'score': 100} if torrent_score['data'].label.lower() == 'no_delete': torrent_score['score'] -= 100 continue elif torrent_score['data'].ratio < 3: torrent_score['score'] -= 80 elif torrent_score['data'].ratio < 3 and older_than_min(torrent_score['data']): torrent_score['score'] -= 40 elif torrent_score['data'].ul_speed > 0: torrent_score['score'] -= 20 elif older_than_min(torrent_score['data']): torrent_score['score'] -= 10 t_score.append(torrent_score) to_delete = sorted(t_score, key=lambda k: k['score'], reverse=True) self._conn.torrent_stop(to_delete[0]['hash']) self._conn.torrent_remove(to_delete[0]['hash'], with_data=True) removed_q.put(to_delete[0]['data'])