Compare commits

..

2 Commits

Author SHA1 Message Date
450a8d6a06 added torrent size and completion 2019-02-18 15:54:49 -05:00
b80d3b989a tracker stats added 2019-02-18 10:32:19 -05:00
5 changed files with 52 additions and 17 deletions

View File

@ -20,6 +20,7 @@ signal.signal(signal.SIGINT, rtorrent.stop_watch)
def index():
"""The index page."""
torrents = rtorrent.get_active()
tracker_stats = rtorrent.get_stats()
return render_template("index.html", **locals())
@app.route("/static/<path:path>")

View File

@ -27,12 +27,17 @@ class Torrent:
self.state = "leeching"
self.downrate = raw[4]
self.downrate_str = speedrate(self.downrate)
self.downrate_str = size_units(self.downrate) + '/s'
self.uprate = raw[5]
self.uprate_str = speedrate(self.uprate)
self.uprate_str = size_units(self.uprate) + '/s'
self.tracker = get_tracker(raw[6])
self.down_total = raw[7]
self.total_size = raw[8]
self.total_size_str = size_units(self.total_size)
self.down_percent = round((self.down_total / self.total_size) * 100, 2)
class Watch(threading.Thread):
"""A thread class that continously queries the rTorrent instances."""
@ -56,15 +61,18 @@ class Watch(threading.Thread):
self._stop_event.wait(2)
def speedrate(rate):
"""Helper to assign appropriate prefixes to speed rates."""
unit = "B/s"
def size_units(rate):
"""Helper to assign appropriate prefixes to numbers."""
unit = "B"
if rate > 1024:
rate /= 1024
unit = "KiB/s"
unit = "KiB"
if rate > 1024:
rate /= 1024
unit = "MiB/s"
unit = "MiB"
if rate > 1024:
rate /= 1024
unit = "GiB"
rate = round(rate, 1)
return str(rate) + unit
@ -83,13 +91,6 @@ def all_torrents():
res += item
return res
def open_proxy():
"""Opens connections to all of the rtorrent instances."""
global sp
for n in range(NUM_INST):
s = xmlrpc.client.ServerProxy(f"http://localhost:8000/RPC{n}")
sp.append(s)
def get_all(n):
"""Gets all torrent information from a instance and returns it."""
res = sp[n].d.multicall2('', 'main',
@ -100,13 +101,18 @@ def get_all(n):
'd.down.rate=',
'd.up.rate=',
'd.directory=',
'd.completed_bytes=',
'd.size_bytes=',
)
return [Torrent(raw) for raw in res]
def init():
"""Initializes the rTorrent interface."""
global WATCH_HANDLE
open_proxy()
global sp
for n in range(NUM_INST):
s = xmlrpc.client.ServerProxy(f"http://localhost:8000/RPC{n}")
sp.append(s)
WATCH_HANDLE = Watch()
WATCH_HANDLE.start()
@ -119,3 +125,11 @@ def get_active():
"""Returns all actively seeding or leeching torrents."""
active = [t for t in all_torrents() if t.downrate or t.uprate]
return active
def get_stats():
"""Returns various statistical information about the torrents."""
trackers = [t.tracker for t in all_torrents()]
tracker_stats = {tr: trackers.count(tr) for tr in set(trackers)}
tracker_stats = dict(sorted(tracker_stats.items()))
tracker_stats['total'] = len(trackers)
return tracker_stats

View File

@ -18,7 +18,7 @@ tr {
padding-left: 1em;
}
.state, .downrate, .uprate, .tracker {
.totalSize, .state, .downrate, .downPercent, .uprate, .tracker {
text-align: center;
width: 10%;
}

View File

@ -11,7 +11,15 @@ function get_active_torrents() {
torrents = JSON.parse(httpRequest.responseText);
let html_str = '';
for (let i = 0; i < torrents.length; i++) {
html_str += '<tr><td class="name">' + torrents[i].name + '</td><td class="state">' + torrents[i].state + '</td><td class="downrate">' + torrents[i].downrate_str + '</td><td class="uprate">' + torrents[i].uprate_str + '</td><td class="tracker">' + torrents[i].tracker + "</td>";
html_str += '<tr>'
html_str += '<td class="name">' + torrents[i].name + '</td>';
html_str += '<td class="totalSize">' + torrents[i].total_size_str + '</td>';
html_str += '<td class="state">' + torrents[i].state + '</td>';
html_str += '<td class="downrate">' + torrents[i].downrate_str + '</td>';
html_str += '<td class="downPercent">' + torrents[i].down_percent + '</td>';
html_str += '<td class="uprate">' + torrents[i].uprate_str + '</td>';
html_str += '<td class="tracker">' + torrents[i].tracker + '</td>';
html_str += '</tr>';
}
document.getElementById('torrents').children[1].innerHTML = html_str;
};

View File

@ -14,8 +14,10 @@
<thead>
<tr>
<th class="name">Name</th>
<th class="sizeTotal">Size</th>
<th class="state">State</th>
<th class="downrate">DL</th>
<th class="downPercent">%</th>
<th class="uprate">UL</th>
<th class="tracker">Tracker</th>
</tr>
@ -24,13 +26,23 @@
{% for torrent in torrents %}
<tr>
<td class="name">{{ torrent.name }}</td>
<td class="totalSize">{{ torrent.total_size_str }}</td>
<td class="state">{{ torrent.state }}</td>
<td class="downrate">{{ torrent.downrate_str }}</td>
<td class="downPercent">{{ torrent.down_percent }}</td>
<td class="uprate">{{ torrent.uprate_str }}</td>
<td class="tracker">{{ torrent.tracker }}</td>
</tr>
{% endfor %}
</tbody>
</table>
<table id="tracker_stats">
{% for tracker, count in tracker_stats.items() %}
<tr>
<td>{{ tracker }}</td>
<td>{{ count }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>