-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #81 from lawrencecurtis/qBittorrent
Added qbittorent scripts
- Loading branch information
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
Scripts/Flow/Applications/qBittorrent/qBittorrent - Renamer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
Scripts/Flow/Applications/qBittorrent/qBittorrent - Seeding check.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |