Last active
May 23, 2019 14:24
-
-
Save divinity76/9175f22a52891a78ea0abd5dc1f2ad98 to your computer and use it in GitHub Desktop.
controlling qBitTorrent from php
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
<?php | |
declare (strict_types = 1); | |
require_once('hhb_.inc.php'); // need hhb_curl from https://github.com/divinity76/hhb_.inc.php/blob/master/hhb_.inc.php | |
class QbitTorrent | |
{ | |
protected $username = "admin"; | |
protected $password = "123456"; | |
protected $url = "http://127.0.0.1:8080"; | |
/* @var hhb_curl $hc */ | |
public $hc; | |
function __construct(string $username, string $password, string $url) | |
{ | |
$this->username = $username; | |
$this->password = $password; | |
$this->url = $url; | |
$this->hc = new hhb_curl('', true); | |
$this->login(); | |
} | |
function __destruct() | |
{ | |
$this->logout(); | |
unset($this->hc); // appropriate time to destruct. | |
} | |
protected function login() | |
{ | |
$hc = &$this->hc; | |
$res = $hc->setopt_array(array( | |
CURLOPT_URL => $this->url . '/api/v2/auth/login', | |
CURLOPT_POST => 1, | |
CURLOPT_POSTFIELDS => http_build_query(array( | |
'username' => $this->username, | |
'password' => $this->password, | |
)) | |
))->exec()->getStdOut(); | |
$hcode = $hc->getinfo(CURLINFO_HTTP_CODE); | |
if ($hcode === 403) { | |
throw new \RuntimeException("login http 403: User's IP is banned for too many failed login attempts - {$res}"); | |
} | |
if ($hcode !== 200) { | |
throw new \LogicException("per API documentation the login response code will ALWAYS be either 200 or 403! but {$hcode} recieved! - response: {$res}"); | |
} | |
$cookies = $hc->getResponseCookies(); | |
if (empty($cookies['SID'])) { | |
throw new \RuntimeException("did not get authentication cookie when trying to log in!"); | |
} | |
// logged in! | |
} | |
protected function logout() | |
{ | |
$this->hc->setopt(CURLOPT_HTTPGET, 1)->exec($this->url . "/api/v2/auth/logout"); | |
} | |
public function get_downloading_torrents():array{ | |
$this->hc->setopt_array(array( | |
CURLOPT_HTTPGET=>1, | |
CURLOPT_URL=>$this->url.'/api/v2/torrents/info?filter=downloading', | |
))->exec()->getStdOut(); | |
} | |
} | |
/* @var QbitTorrent $q */ | |
$q; | |
init(); | |
function init() | |
{ | |
error_reporting(E_ALL); | |
hhb_init(); | |
stream_set_blocking(STDIN, true); | |
$username = ask("WebUI username", "admin"); | |
$password = ask("WebUI password", "123456"); | |
$url = rtrim(ask("WebUI URL", "http://127.0.0.1:8080"), "\/"); | |
$q = new QbitTorrent($username, $password, $url); | |
} | |
function ask(string $question, string $default): string | |
{ | |
$shown_default = (empty($default) ? "emptystring" : "\"{$default}\""); | |
echo "enter \"{$question}\" and press enter (or just press enter and it will default to {$shown_default} )\n"; | |
echo "{$question}: "; | |
$ret = fgets(STDIN); | |
$ret = trim($ret); | |
if (!strlen($ret)) { | |
$ret = $default; | |
} | |
echo "set to \"{$ret}\".\n"; | |
return $ret; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment