print_console( "Starting " + ", ".join( torrs ) + "..." ) utorrent.torrent_start( args, opts.force ) elif opts.action == "torrent_stop": torr_list = None if opts.all: torr_list = utorrent.torrent_list( ) args = torr_list.keys( ) print_console( "Stopping all torrents..." ) else: if opts.verbose: torrs = utorrent.resolve_torrent_hashes( args, torr_list ) else: torrs = args print_console( "Stopping " + ", ".join( torrs ) + "..." ) utorrent.torrent_stop( args ) elif opts.action == "torrent_resume": torr_list = None if opts.all: torr_list = utorrent.torrent_list( ) args = torr_list.keys( ) print_console( "Resuming all torrents..." ) else: if opts.verbose: torrs = utorrent.resolve_torrent_hashes( args, torr_list ) else: torrs = args print_console( "Resuming " + ", ".join( torrs ) + "..." ) utorrent.torrent_resume( args )
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'])
print_console("Starting " + ", ".join(torrs) + "...") utorrent.torrent_start(args, opts.force) elif opts.action == "torrent_stop": torr_list = None if opts.all: torr_list = utorrent.torrent_list() args = torr_list.keys() print_console("Stopping all torrents...") else: if opts.verbose: torrs = utorrent.resolve_torrent_hashes(args, torr_list) else: torrs = args print_console("Stopping " + ", ".join(torrs) + "...") utorrent.torrent_stop(args) elif opts.action == "torrent_resume": torr_list = None if opts.all: torr_list = utorrent.torrent_list() args = torr_list.keys() print_console("Resuming all torrents...") else: if opts.verbose: torrs = utorrent.resolve_torrent_hashes(args, torr_list) else: torrs = args print_console("Resuming " + ", ".join(torrs) + "...") utorrent.torrent_resume(args)
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'])