Compare commits

..

No commits in common. "450a8d6a062f7112f717af3fb51f518168833578" and "e013d79e6e487eca9ad07d699df96aef53c2b2b4" have entirely different histories.

5 changed files with 17 additions and 52 deletions

View File

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

View File

@ -27,17 +27,12 @@ class Torrent:
self.state = "leeching" self.state = "leeching"
self.downrate = raw[4] self.downrate = raw[4]
self.downrate_str = size_units(self.downrate) + '/s' self.downrate_str = speedrate(self.downrate)
self.uprate = raw[5] self.uprate = raw[5]
self.uprate_str = size_units(self.uprate) + '/s' self.uprate_str = speedrate(self.uprate)
self.tracker = get_tracker(raw[6]) 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): class Watch(threading.Thread):
"""A thread class that continously queries the rTorrent instances.""" """A thread class that continously queries the rTorrent instances."""
@ -61,18 +56,15 @@ class Watch(threading.Thread):
self._stop_event.wait(2) self._stop_event.wait(2)
def size_units(rate): def speedrate(rate):
"""Helper to assign appropriate prefixes to numbers.""" """Helper to assign appropriate prefixes to speed rates."""
unit = "B" unit = "B/s"
if rate > 1024: if rate > 1024:
rate /= 1024 rate /= 1024
unit = "KiB" unit = "KiB/s"
if rate > 1024: if rate > 1024:
rate /= 1024 rate /= 1024
unit = "MiB" unit = "MiB/s"
if rate > 1024:
rate /= 1024
unit = "GiB"
rate = round(rate, 1) rate = round(rate, 1)
return str(rate) + unit return str(rate) + unit
@ -91,6 +83,13 @@ def all_torrents():
res += item res += item
return res 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): def get_all(n):
"""Gets all torrent information from a instance and returns it.""" """Gets all torrent information from a instance and returns it."""
res = sp[n].d.multicall2('', 'main', res = sp[n].d.multicall2('', 'main',
@ -101,18 +100,13 @@ def get_all(n):
'd.down.rate=', 'd.down.rate=',
'd.up.rate=', 'd.up.rate=',
'd.directory=', 'd.directory=',
'd.completed_bytes=',
'd.size_bytes=',
) )
return [Torrent(raw) for raw in res] return [Torrent(raw) for raw in res]
def init(): def init():
"""Initializes the rTorrent interface.""" """Initializes the rTorrent interface."""
global WATCH_HANDLE global WATCH_HANDLE
global sp open_proxy()
for n in range(NUM_INST):
s = xmlrpc.client.ServerProxy(f"http://localhost:8000/RPC{n}")
sp.append(s)
WATCH_HANDLE = Watch() WATCH_HANDLE = Watch()
WATCH_HANDLE.start() WATCH_HANDLE.start()
@ -125,11 +119,3 @@ def get_active():
"""Returns all actively seeding or leeching torrents.""" """Returns all actively seeding or leeching torrents."""
active = [t for t in all_torrents() if t.downrate or t.uprate] active = [t for t in all_torrents() if t.downrate or t.uprate]
return active 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; padding-left: 1em;
} }
.totalSize, .state, .downrate, .downPercent, .uprate, .tracker { .state, .downrate, .uprate, .tracker {
text-align: center; text-align: center;
width: 10%; width: 10%;
} }

View File

@ -11,15 +11,7 @@ function get_active_torrents() {
torrents = JSON.parse(httpRequest.responseText); torrents = JSON.parse(httpRequest.responseText);
let html_str = ''; let html_str = '';
for (let i = 0; i < torrents.length; i++) { for (let i = 0; i < torrents.length; i++) {
html_str += '<tr>' 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 += '<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; document.getElementById('torrents').children[1].innerHTML = html_str;
}; };

View File

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