webpage auto-updates

This commit is contained in:
iou1name 2019-02-15 10:01:33 -05:00
parent 2580294cbf
commit 98461c808f
3 changed files with 45 additions and 0 deletions

View File

@ -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)

34
static/aberrant.js Normal file
View File

@ -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 += '<tr><td class="name">' + torrents[i].name + '</td><td class="state">' + torrents[i].state + '</td><td class="downrate">' + speedrate(torrents[i].downrate) + '</td><td class="uprate">' + speedrate(torrents[i].uprate) + '</td><td class="tracker">' + torrents[i].tracker + "</td>";
}
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;
}

View File

@ -3,6 +3,11 @@
<head>
<title>Aberrant</title>
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='aberrant.css') }}">
<script type="text/javascript" src="{{ url_for('static', filename='aberrant.js') }}"></script>
<script>
const get_torrents_uri = "{{ url_for('get_active_torrents') }}";
</script>
<script>window.onload = load;</script>
</head>
<body>
<table id="torrents">