Skip to content

Instantly share code, notes, and snippets.

@kubijo
Created January 11, 2020 22:12
Show Gist options
  • Save kubijo/541534be496cd1e5e5c2bee9ab41d3a9 to your computer and use it in GitHub Desktop.
Save kubijo/541534be496cd1e5e5c2bee9ab41d3a9 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
const host = "192.168.0.241:9091";
const header = "Cleanup completed transmition torrents";
// Modules
const { execSync } = require("child_process");
// Utils
const call = args =>
execSync(`transmission-remote ${host} ${args}`, {
encoding: "utf-8"
});
const red = text => `\x1b[31m${text}\x1b[0m`;
const green = text => `\x1b[32m${text}\x1b[0m`;
const padLeft = (txt, length) =>
`${" ".repeat(length - String(txt).length)}${txt}`;
// Intro
console.log(header);
console.log("=".repeat(header.length), "\n");
// Get all torrents
const info = call("--list");
// Print the output for user info
console.log(info);
//
// Parse the output info list of objects
//
let [headers, ...rows] = info
// ends with newline
.trim()
// split by row
.split("\n")
// remove summary row
.slice(0, -1)
// cleanup columns
.map(x => x.trim());
headers = headers
.toLowerCase()
// columns delimited by more than two spaces
.split(/ {2,}/)
// cleanup extra padding
.map(x => x.trim());
rows = rows.map(r => r.split(/ {2,}/).map(x => x.trim()));
const data = rows.map(d => {
const res = {};
headers.forEach((k, i) => {
res[k] = d[i];
});
return res;
});
//
// Delete all torrents that are 100% done and stopped
//
data.forEach(({ id, done, name, status }) => {
if (!Number.isInteger(parseInt(id, 10))) return;
const isDone =
/100%/i.test(done) && /Seeding|Stopped|Finished|Idle/i.test(status);
if (isDone) {
console.log(green(`✓ ${name}`));
call(`--torrent "${id}" --remove`);
} else {
console.log(red(`✗ ${padLeft(done, 5)} ${name}`));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment