Skip to content

Commit

Permalink
Merge pull request #81 from lawrencecurtis/qBittorrent
Browse files Browse the repository at this point in the history
Added qbittorent scripts
  • Loading branch information
revenz authored Jan 14, 2025
2 parents 1332c54 + 67bb423 commit f5aa28d
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
101 changes: 101 additions & 0 deletions Scripts/Flow/Applications/qBittorrent/qBittorrent - Renamer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**
* @name qBittorrent - Renamer
* @description Renames the current file in qBittorrent, Run after move / delete original.
* This requires you don't have authentication on qBittorrent or different path mappings
* @author lawrence
* @uid 4130f59f-3871-4c24-9a0d-15966da6eb7c
* @revision 1
* @param {string} URI qBittorent root URI and port (e.g. http://qbitorrent:8080)
* @output Item Renamed
* @output Item not found or not renamed
*/
function Script(URI) {
URI = URI || Variables["QBittorrent.URI"];

const qbittorrent = new QBittorrent(URI);
return qbittorrent.check(
Flow.UnMapPath(Variables.folder.Orig.Name),
Variables.file.Orig.Name,
Variables.file.Name
);
}

class QBittorrent {
constructor(uri) {
if (!URI) {
Flow.Fail("qBittorrent: No URI specified");
}

this.uri = uri;
}

getUrl(endpoint) {
let url = "" + this.uri;
if (url.endsWith("/") === false) url += "/";
return `${url}api/v2/${endpoint}`;
}

getJson(endpoint) {
let url = this.getUrl(endpoint);
let response = http.GetAsync(url).Result;

let body = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode) {
Flow.Fail("Unable to fetch: " + endpoint + "\n" + body);
}
return JSON.parse(body);
}

postJson(endpoint, data) {
let url = this.getUrl(endpoint);
let response = http.PostAsync(url, data).Result;

let body = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode) {
Flow.Fail("Unable to rename: " + endpoint + "\n" + body);
}
return body;
}

check(path, oldName, newName) {
let queue = this.getJson("torrents/info");
path = path.replace(/\W/gi, "");
if (!queue) return 2;

let renamed = false;

queue.forEach((item) => {
if (item.content_path.replace(/\W/gi, "").includes(path)) {
Logger.ILog(`Identified item ${item.name} from ${item.content_path}`);

if (oldName != newName) {
let files = this.getJson(`torrents/files?hash=${item.hash}`);
files.forEach((file) => {
if (file.name.includes(oldName)) {
newName = file.name.replace(oldName, newName);

var Dick = System.Collections.Generic.Dictionary(
System.String,
System.String
);
var list = new Dick();
list.Add("hash", item.hash);
list.Add("oldPath", file.name);
list.Add("newPath", newName);
let data = FormUrlEncodedContent(list);

this.postJson("torrents/renameFile", data);
renamed = true;
}
});
} else {
Logger.DLog("Did not rename, item has the same name or is seeding");
}
}
});

if (renamed) return 1;

return 2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* @name qBittorrent - Seeding check
* @description Checks if the current file is seeding in qBittorrent so you can 'reprocess' later
* This requires you don't have authentication on qBittorrent or different path mappings
* @author lawrence
* @uid 48c1518d-83fc-416d-972d-8caf7defe424
* @revision 1
* @param {string} URI qBittorent root URI and port (e.g. http://qbitorrent:8080)
* @output Item is seeding
* @output Item not found or seeding
*/
function Script(URI) {
URI = URI || Variables["QBittorrent.URI"];

const qbittorrent = new QBittorrent(URI);
return qbittorrent.check(Flow.UnMapPath(Variables.folder.Orig.Name));
}

class QBittorrent {
constructor(uri) {
if (!URI) {
Flow.Fail("qBittorrent: No URI specified");
}

this.uri = uri;
}

getUrl(endpoint) {
let url = "" + this.uri;
if (url.endsWith("/") === false) url += "/";
return `${url}api/v2/${endpoint}`;
}

getJson(endpoint) {
let url = this.getUrl(endpoint);
let response = http.GetAsync(url).Result;

let body = response.Content.ReadAsStringAsync().Result;
if (!response.IsSuccessStatusCode) {
Flow.Fail("Unable to fetch: " + endpoint + "\n" + body);
}
return JSON.parse(body);
}

check(path) {
let seeding = false;
let queue = this.getJson("torrents/info");
path = path.replace(/\W/gi, "");
if (!queue) return 2;

queue.forEach((item) => {
if (item.content_path.replace(/\W/gi, "").includes(path)) {
if (item.state.includes("upload")) seeding = true;

Logger.ILog(
`Identified item ${item.name} from ${item.content_path}`
);
}
});

if (seeding) return 1;

return 2;
}
}

0 comments on commit f5aa28d

Please sign in to comment.