mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2026-10-23 06:22:30 +00:00
start all stop all
This commit is contained in:
1 parent
4ea44184d1
commit
d54d08487f
2 files changed
+118
-115
No files matched your search
@@ -1,117 +1,92 @@
|
||||
class TorrentClient {
|
||||
constructor(host = "127.0.0.1", port = 9191, path = "mytorrents") {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
setConnection(host, port, path) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
getEndpoint() {
|
||||
return `http://${this.host}:${this.port}${this.path}`;
|
||||
}
|
||||
|
||||
getStatusString(status) {
|
||||
switch (status) {
|
||||
case 4:
|
||||
return "DOWNLOADING";
|
||||
case 6:
|
||||
return "SEEDING";
|
||||
case 0:
|
||||
return "STOPPED";
|
||||
case 2:
|
||||
return "CHECKING";
|
||||
default:
|
||||
return "QUEUED";
|
||||
constructor(host = '127.0.0.1', port = 9191, path = 'mytorrents') {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.path = path;
|
||||
}
|
||||
}
|
||||
|
||||
formatBytes(bytesPerSec) {
|
||||
if (!bytesPerSec) return "0 B/s";
|
||||
if (bytesPerSec > 1024 * 1024) {
|
||||
return (bytesPerSec / (1024 * 1024)).toFixed(2) + " MB/s";
|
||||
setConnection(host, port, path) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
this.path = path;
|
||||
}
|
||||
return (bytesPerSec / 1024).toFixed(2) + " KB/s";
|
||||
}
|
||||
|
||||
async addTorrent(b64metainfo) {
|
||||
const res = await fetch(this.getEndpoint(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
method: "torrent-add",
|
||||
arguments: { metainfo: b64metainfo },
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
getEndpoint() {
|
||||
return `http://${this.host}:${this.port}${this.path}`;
|
||||
}
|
||||
|
||||
async removeTorrent(id) {
|
||||
const res = await fetch(this.getEndpoint(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
method: "torrent-remove",
|
||||
arguments: { ids: [id], "delete-local-data": true },
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
getStatusString(status) {
|
||||
switch (status) {
|
||||
case 4: return 'DOWNLOADING';
|
||||
case 6: return 'SEEDING';
|
||||
case 0: return 'STOPPED';
|
||||
case 2: return 'CHECKING';
|
||||
default: return 'QUEUED';
|
||||
}
|
||||
}
|
||||
|
||||
async getTorrents() {
|
||||
const res = await fetch(this.getEndpoint(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
method: "torrent-get",
|
||||
arguments: {
|
||||
fields: [
|
||||
"id",
|
||||
"name",
|
||||
"status",
|
||||
"rateDownload",
|
||||
"rateUpload",
|
||||
"totalSize",
|
||||
"percentDone",
|
||||
"files",
|
||||
"peers",
|
||||
"trackers",
|
||||
"trackerStats",
|
||||
],
|
||||
},
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
async stopTorrent(ids = []) {
|
||||
const res = await fetch(this.getEndpoint(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
method: "torrent-stop",
|
||||
arguments: ids.length ? { ids } : {},
|
||||
tag: 666,
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
formatBytes(bytesPerSec) {
|
||||
if (!bytesPerSec) return '0 B/s';
|
||||
if (bytesPerSec > 1024 * 1024) {
|
||||
return (bytesPerSec / (1024 * 1024)).toFixed(2) + ' MB/s';
|
||||
}
|
||||
return (bytesPerSec / 1024).toFixed(2) + ' KB/s';
|
||||
}
|
||||
|
||||
async startTorrent(ids = []) {
|
||||
async addTorrent(b64metainfo) {
|
||||
const res = await fetch(this.getEndpoint(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ 'method': 'torrent-add', 'arguments': { 'metainfo': b64metainfo } }),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async removeTorrent(id) {
|
||||
const res = await fetch(this.getEndpoint(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ 'method': 'torrent-remove', 'arguments': { 'ids': [id], 'delete-local-data': true } }),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
|
||||
async getTorrents() {
|
||||
const res = await fetch(this.getEndpoint(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
'method': 'torrent-get',
|
||||
'arguments': { "fields": ["id", "name", "status", "rateDownload", "rateUpload", "totalSize", "percentDone", "files", "peers", "trackers", "trackerStats"] }
|
||||
}),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
async stopTorrent(ids = []) {
|
||||
const res = await fetch(this.getEndpoint(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
method: "torrent-start",
|
||||
arguments: ids.length ? { ids } : {},
|
||||
tag: 666,
|
||||
}),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
'method': 'torrent-stop',
|
||||
'arguments': ids.length ? { ids } : {},
|
||||
'tag': 666
|
||||
}),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
}
|
||||
|
||||
async startTorrent(ids = []) {
|
||||
const res = await fetch(this.getEndpoint(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
'method': 'torrent-start',
|
||||
'arguments': ids.length ? { ids } : {},
|
||||
'tag': 666
|
||||
}),
|
||||
headers: { 'Content-Type': 'application/json' }
|
||||
});
|
||||
return res.json();
|
||||
}
|
||||
}
|
||||
|
||||
//window.tjs = new TorrentClient();
|
||||
@@ -6,8 +6,12 @@
|
||||
<button onclick="document.getElementById('torrentf').click()">
|
||||
Add torrent
|
||||
</button>
|
||||
<button onclick="stopAll()">stopAll</button>
|
||||
<button onclick="startAll()">startAll</button>
|
||||
<button onclick="stopAll()">
|
||||
stopAll
|
||||
</button>
|
||||
<button onclick="startAll()">
|
||||
startAll
|
||||
</button>
|
||||
<ul id="torrents"></ul>
|
||||
</div>
|
||||
</fieldset>
|
||||
@@ -27,14 +31,36 @@
|
||||
|
||||
<script>
|
||||
let currentTorrentsData = [];
|
||||
async function stopAll() {
|
||||
const conf = confirm("Are sure to stop all torrents?");
|
||||
|
||||
if (window.tjs && conf) await window.tjs.stopTorrent();
|
||||
async function stopAll()
|
||||
{
|
||||
const conf = confirm("Are sure to stop all torrents?");
|
||||
// console.log(conf)
|
||||
if(conf && window.tjs)
|
||||
{
|
||||
const r = await window.tjs.getTorrents()
|
||||
//console.log(`torrents: ${r}`)
|
||||
if (!r?.result?.torrents) return;
|
||||
r.result.torrents.forEach(async (t) => {
|
||||
console.log(`stop torrent => ${t.id}`)
|
||||
await window.tjs.stopTorrent([t.id]);
|
||||
});
|
||||
}
|
||||
//if (window.tjs && conf) await window.tjs.stopTorrent();
|
||||
}
|
||||
async function startAll() {
|
||||
const conf = confirm("Are sure to start all torrents?");
|
||||
if (window.tjs && conf) await window.tjs.startTorrent();
|
||||
async function startAll()
|
||||
{
|
||||
const conf = confirm("Are sure to start all torrents?");
|
||||
if(conf && window.tjs)
|
||||
{
|
||||
const r = await window.tjs.getTorrents()
|
||||
//console.log(`torrents: ${r}`)
|
||||
if (!r?.result?.torrents) return;
|
||||
r.result.torrents.forEach( async (t) =>{
|
||||
console.log(`start torrent => ${t.id}`)
|
||||
await window.tjs.startTorrent([t.id]);
|
||||
});
|
||||
}
|
||||
//if (window.tjs && conf) await window.tjs.startTorrent();
|
||||
}
|
||||
setInterval(async () => {
|
||||
const statusIndicator = document.getElementById("connection_status");
|
||||
@@ -73,9 +99,11 @@
|
||||
: el.status;
|
||||
const totalSize = el.totalSize || 0; // bytes
|
||||
const ETA = el.rateDownload
|
||||
? ((totalSize * (1 - el.percentDone)) / el.rateDownload / 60).toFixed(
|
||||
1,
|
||||
) + " min"
|
||||
? (
|
||||
(totalSize * (1 - el.percentDone)) /
|
||||
el.rateDownload /
|
||||
60
|
||||
).toFixed(1) + " min"
|
||||
: "-";
|
||||
const name = el.name || "|unknown name, maybe a bad torrent|";
|
||||
|
||||
|
||||
Reference in new issue
Block a user