/
main.js
174 lines (161 loc) · 4.33 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
function bytesToSize(bytes) {
var sizes = ['B', 'KB', 'MB', 'GB', 'TB'];
if (bytes == 0) return '0 B';
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + ' ' + sizes[i];
}
function formatFloat(f, eps) {
if (!eps) eps = 2;
eps = Math.pow(10, eps);
return parseInt(f * eps) / eps;
}
function makeRatio(tx, rx) {
var r = "0.0";
if ( rx > 0 ) {
if ( tx > 0 ) {
r = "" + formatFloat(tx / rx);
}
} else if ( tx > 0 ) {
r = "∞";
}
return r;
}
var Torrent = function(data) {
this.Name = data.Name;
this.State = data.State;
this.Infohash = data.Infohash;
this.Peers = function() { return data.Peers ? data.Peers.length : 0; };
this.Speed = function() {
var tx = 0, rx = 0;
if (data.Peers)
data.Peers.forEach(function(p){tx += p.TX; rx += p.RX;});
return "↑ " + bytesToSize(tx) +"/s ↓ " + bytesToSize(rx) + "/s";
};
this.RX = function() {
var rx = 0;
if(data.Peers) data.Peers.forEach(function(p) { rx += p.RX; });
return rx;
};
this.TX = function() {
var tx = 0;
if(data.Peers) data.Peers.forEach(function(p) { tx += p.TX; });
return tx;
};
this.Data = function() {
return data;
};
this.TotalSize = function() {
var total_size = 0;
if(data.Files) {
data.Files.forEach(function(f){ total_size += f.FileInfo.Length });
}
return bytesToSize(total_size);
};
this.Progress = data.Progress * 100;
this.Ratio = function() {
return "("+makeRatio(data.TX, data.RX) + " ratio)";
};
this.changeTorrent = function(action)
{
viewModel._apicall({method: "XD.ChangeTorrent", action: action, infohash: this.Infohash, swarm: "0"}, function(data){
console.log(data);
});
}.bind(this);
this.remove = function()
{
this.changeTorrent("remove");
}.bind(this);
this.start = function()
{
this.changeTorrent("start");
}.bind(this);
this.stop = function()
{
this.changeTorrent("stop");
}.bind(this);
this.toggle = function()
{
if(this.Stopped())
this.start();
else
this.stop();
}.bind(this);
this.Stopped = function()
{
return data.State == "stopped";
};
this.StatusButton = function()
{
if (this.Stopped())
{
return "▶";
}
else
{
return "⏸";
}
};
}
var viewModel = {
_url: window.location.protocol + "//" + window.location.host + "/ecksdee/api",
_apicall: function(call, cb)
{
$.ajax({
type: "POST",
url: this._url,
contentType: "text/json; charset=UTF-8",
data: JSON.stringify(call),
success: function(j, text, xhr) {
// console.log(call, j);
cb(JSON.parse(j));
}
});
},
torrents: ko.observableArray(),
torrentURL: ko.observable(),
torrentFilter: ko.observable('all').bind(this),
setFilter: function(state) {
this.torrentFilter(state); main(); },
addTorrent: function()
{
var _this = this;
this._apicall({method: "XD.AddTorrent", swarm: "0", url: this.torrentURL()}, function(data){
if (!data.error) _this.torrentURL("");
});
},
torrentStates: ['all', 'downloading', 'seeding'],
globalInfo: function()
{
var rx = 0;
var tx = 0;
var peers = 0;
var rtx = 0;
var rrx = 0;
var count = 0;
this.torrents().forEach(function(t) {
rx += t.RX();
tx += t.TX();
rrx += t.Data().RX;
rtx += t.Data().TX;
peers += t.Peers();
count ++;
});
return peers+" peers connected on "+ count+ " torrents (" + makeRatio(rtx, rrx) + " ratio) ↑ " + bytesToSize(tx) +"/s ↓ " + bytesToSize(rx) + "/s";
}
};
function main()
{
viewModel._apicall({method: "XD.SwarmStatus"}, function(data){
viewModel.torrents.removeAll();
for (var prop in data) {
if (viewModel.torrentFilter() != 'all' & viewModel.torrentFilter() != data[prop].State)
continue;
viewModel.torrents.push(new Torrent(data[prop]));
}
});
}
window.onload = function()
{
ko.applyBindings(viewModel);
main(); setInterval(main, 1000);
}