Ejemplo n.º 1
0
			for tr in infos[hsh].trackers:
				print_console( level2 + tr )

	elif opts.action == "torrent_dump":
		tors = utorrent.torrent_list( )
		infos = utorrent.torrent_info( args )
		for hsh, info in infos.items( ):
			print_console( tors[hsh].verbose_str( opts.format ) if opts.verbose else tors[hsh] )
			print_console( level1 + "Properties:" )
			dump_writer( tors[hsh], tors[hsh].get_public_attrs( ) )
			dump_writer( info, info.get_public_attrs( ) )
			print_console( level1 + "Read-only:" )
			dump_writer( tors[hsh], tors[hsh].get_readonly_attrs( ) )

	elif opts.action == "stats":
		res = utorrent.xfer_history_get( )
		excl_local = utorrent.settings_get( )["net.limit_excludeslocal"]
		torrents = utorrent.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 }
		print_console( "Last {} days:".format( period ) )
		print_console(
			level1 + "Downloaded: {} (+{} local)".format( utorrent_module.human_size( down_total ), utorrent_module.human_size( down_total_local ) ) )
Ejemplo n.º 2
0
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'])
Ejemplo n.º 3
0
                print_console(level2 + tr)

    elif opts.action == "torrent_dump":
        tors = utorrent.torrent_list()
        infos = utorrent.torrent_info(args)
        for hsh, info in infos.items():
            print_console(tors[hsh].verbose_str(opts.format) if opts.
                          verbose else tors[hsh])
            print_console(level1 + "Properties:")
            dump_writer(tors[hsh], tors[hsh].get_public_attrs())
            dump_writer(info, info.get_public_attrs())
            print_console(level1 + "Read-only:")
            dump_writer(tors[hsh], tors[hsh].get_readonly_attrs())

    elif opts.action == "stats":
        res = utorrent.xfer_history_get()
        excl_local = utorrent.settings_get()["net.limit_excludeslocal"]
        torrents = utorrent.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)
Ejemplo n.º 4
0
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'])