[qBitTorrent] Add 'Rename from folder' script
This commit is contained in:
parent
32dfc69be0
commit
f61ad00274
12
common.php
12
common.php
@ -86,15 +86,19 @@
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes text to file lol
|
||||
* Writes specified data to a file
|
||||
*
|
||||
* @param string $path Path to file (including filename)
|
||||
* @param string $text Text to write
|
||||
* @param string|array $text Data to write. If it is an array, it will be JSON encoded.
|
||||
*/
|
||||
function fileWrite($path = '', $text = '')
|
||||
function fileWrite(string $path = '', string|array $data = '')
|
||||
{
|
||||
if (gettype($data) === 'array') {
|
||||
$data = json_encode($data, JSON_PRETTY_PRINT);
|
||||
}
|
||||
|
||||
$file = fopen($path, 'w');
|
||||
fwrite($file, $text);
|
||||
fwrite($file, $data);
|
||||
fclose($file);
|
||||
}
|
||||
|
||||
|
@ -347,4 +347,32 @@ class Client
|
||||
throw new Exception('Failed to edit tracker URL for torrent: ' . $hash);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the torrent name for a specific torrent.
|
||||
*
|
||||
* @link https://github.com/qbittorrent/qBittorrent/wiki/WebUI-API-(qBittorrent-4.1)#set-torrent-name
|
||||
*
|
||||
* @param string $hash
|
||||
* @param string $newName
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setTorrentName(string $hash, string $newName) : void
|
||||
{
|
||||
$path = 'torrents/rename';
|
||||
|
||||
$options = [
|
||||
RequestOptions::FORM_PARAMS => [
|
||||
'hash' => $hash,
|
||||
'name' => $newName,
|
||||
],
|
||||
];
|
||||
|
||||
$response = $this->request($path, 'POST', $options);
|
||||
|
||||
if ($response->getStatusCode() !== 200) {
|
||||
throw new Exception('Failed to rename torrent: ' . $hash);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
64
qBitTorrentRenameFromFolder.php
Normal file
64
qBitTorrentRenameFromFolder.php
Normal file
@ -0,0 +1,64 @@
|
||||
<?php
|
||||
/**
|
||||
* Script meant to rename torrents in qBitTorrent based on the first folder name.
|
||||
*
|
||||
* I had a lot of single video files that would be something like "Blah.Blah.S01E01.mkv" from when I used Deluge,
|
||||
* and I wanted to rename them to be more consistent with the rest of the torrents.
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/common.php';
|
||||
require_once __DIR__ . '/lib/qBitTorrent.php';
|
||||
|
||||
use Decicus\qBitTorrent\Client as QBitClient;
|
||||
|
||||
$qBitClient = new QBitClient();
|
||||
$torrents = $qBitClient->getBasicTorrentsInfo();
|
||||
|
||||
// Filter by file extension
|
||||
$regex = '/\.(mp4|mkv|m4v|mov|wmv)$/i';
|
||||
|
||||
$torrents = array_filter($torrents, function ($torrent) use ($regex) {
|
||||
$name = strtolower(trim($torrent['name']));
|
||||
return preg_match($regex, $name);
|
||||
});
|
||||
|
||||
// Sort by name
|
||||
usort($torrents, function ($a, $b) {
|
||||
return strcmp($a['name'], $b['name']);
|
||||
});
|
||||
|
||||
$changedNames = [];
|
||||
|
||||
foreach ($torrents as $torrent) {
|
||||
$filePath = $torrent['content_path'];
|
||||
$rootPath = $torrent['save_path'];
|
||||
$filePath = str_replace($rootPath . '/', '', $filePath);
|
||||
$segments = explode('/', $filePath);
|
||||
|
||||
$currentName = $torrent['name'];
|
||||
$newName = $segments[0] ?? '';
|
||||
|
||||
if (empty($newName)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if ($currentName === $newName) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$changedNames[] = [
|
||||
'old' => $currentName,
|
||||
'new' => $newName,
|
||||
'hash' => $torrent['hash'],
|
||||
];
|
||||
|
||||
$qBitClient->setTorrentName($torrent['hash'], $newName);
|
||||
echo sprintf('Changed: %s -> %s%s', $currentName, $newName, PHP_EOL);
|
||||
}
|
||||
|
||||
echo 'Changed names: ' . count($changedNames) . PHP_EOL;
|
||||
|
||||
$filename = sprintf('%s/output/%s_renames.json', __DIR__, time());
|
||||
fileWrite($filename, $changedNames);
|
||||
|
||||
echo 'Output written to: ' . $filename . PHP_EOL;
|
Loading…
x
Reference in New Issue
Block a user