opts, args = parser.parse_args( ) try: if opts.host is None: # we didn't supply host in command line => load auth data from config opts.host = utorrentcfg["host"] if opts.user is None: opts.user = utorrentcfg["login"] if opts.password is None: opts.password = utorrentcfg["password"] if opts.api is None: opts.api = utorrentcfg["api"] utorrent = None if opts.action is not None: utorrent = Connection( opts.host, opts.user, opts.password ).utorrent( opts.api ) if opts.action == "server_version": print_console( utorrent.version( ).verbose_str( ) if opts.verbose else utorrent.version( ) ) elif opts.action == "torrent_list": total_ul, total_dl, count, total_size = 0, 0, 0, 0 opts.sort_field = opts.sort_field.lower( ) # ensure that int is returned opts.limit = int( opts.limit ) if not opts.sort_field in utorrent.TorrentClass.get_public_attrs( ) + utorrent.TorrentClass.get_readonly_attrs( ): opts.sort_field = "name" for h, t in sorted( utorrent.torrent_list( ).items( ), key = lambda x: getattr( x[1], opts.sort_field ), reverse = opts.sort_desc ): if not opts.active or opts.active and ( t.ul_speed > 0 or t.dl_speed > 0 ): # handle --active if opts.label is None or opts.label == t.label: # handle --label count += 1
if opts.host is None: # we didn't supply host in command line => load auth data from config opts.host = utorrentcfg["host"] if opts.user is None: opts.user = utorrentcfg["login"] if opts.password is None: opts.password = utorrentcfg["password"] if opts.api is None: opts.api = utorrentcfg["api"] if opts.ssl == False and "ssl" in utorrentcfg and utorrentcfg[ "ssl"] is not None: opts.ssl = utorrentcfg["ssl"] utorrent = None if opts.action is not None: utorrent = Connection(opts.host, opts.user, opts.password, opts.ssl).utorrent(opts.api) if opts.action == "server_version": print_console(utorrent.version().verbose_str() if opts. verbose else utorrent.version()) elif opts.action == "torrent_list": total_ul, total_dl, count, total_size = 0, 0, 0, 0 opts.sort_field = opts.sort_field.lower() # ensure that int is returned opts.limit = int(opts.limit) if not opts.sort_field in utorrent.TorrentClass.get_public_attrs( ) + utorrent.TorrentClass.get_readonly_attrs(): opts.sort_field = "name" for h, t in sorted(utorrent.torrent_list().items(), key=lambda x: getattr(x[1], opts.sort_field),
def __init__(self, host, username, password): self._conn = Connection(host, username, password).utorrent(api='falcon')
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'])
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'])