diff --git a/aberrant.py b/aberrant.py index 476ff55..7009fa2 100644 --- a/aberrant.py +++ b/aberrant.py @@ -2,6 +2,7 @@ """ The primary module for serving the Aberrant application. """ +import json import signal from flask import Flask, render_template, send_from_directory @@ -26,5 +27,10 @@ def send_static(path): """Sends static files.""" return send_from_directory("static", path) +@app.route("/get_active_torrents") +def get_active_torrents(): + """Returns all active torrents formatted as JSON.""" + return json.dumps([vars(t) for t in rtorrent.get_active()]) + if __name__ == "__main__": app.run(host='0.0.0.0', port=5250) diff --git a/static/aberrant.js b/static/aberrant.js new file mode 100644 index 0000000..487a824 --- /dev/null +++ b/static/aberrant.js @@ -0,0 +1,34 @@ +function load() { + let intervalID = window.setInterval(get_active_torrents, 20000); +} + +function get_active_torrents() { + let httpRequest; + httpRequest = new XMLHttpRequest(); + httpRequest.onreadystatechange = function() { + if (httpRequest.readyState !== XMLHttpRequest.DONE) { return; } + if (httpRequest.status !== 200) { return; } + torrents = JSON.parse(httpRequest.responseText); + let html_str = ''; + for (let i = 0; i < torrents.length; i++) { + html_str += '' + torrents[i].name + '' + torrents[i].state + '' + speedrate(torrents[i].downrate) + '' + speedrate(torrents[i].uprate) + '' + torrents[i].tracker + ""; + } + document.getElementById('torrents').children[1].innerHTML = html_str; + }; + httpRequest.open('GET', get_torrents_uri, true); + httpRequest.send(); +} + +function speedrate(rate) { + let unit = 'B/s'; + if (rate > 1024) { + rate /= 1024; + unit = 'KiB/s'; + } + if (rate > 1024) { + rate /= 1024; + unit = 'MiB/s'; + } + rate = Math.round(rate*10)/10; // different from Python's round() + return rate + unit; +} diff --git a/templates/index.html b/templates/index.html index 8e9aa36..c919a12 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,6 +3,11 @@ Aberrant + + +