Skip to content

Commit

Permalink
Fix-for-Transmission4 (#765)
Browse files Browse the repository at this point in the history
* Fix-for-Transmission4

* php-cs-fix
  • Loading branch information
GOUKI9999 committed Aug 17, 2024
1 parent 6a952ea commit 8255133
Showing 1 changed file with 16 additions and 48 deletions.
64 changes: 16 additions & 48 deletions Transmission/Transmission.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ class Transmission extends \App\SupportedApps implements \App\EnhancedApps
public $attrs = [];
public $vars;

//protected $login_first = true; // Uncomment if api requests need to be authed first
protected $method = "POST"; // Uncomment if requests to the API should be set by POST
protected $method = "POST";

public function __construct()
{
//$this->jar = new \GuzzleHttp\Cookie\CookieJar; // Uncomment if cookies need to be set
$body["method"] = "torrent-get";
$body["arguments"] = [
"fields" => ["percentDone", "status", "rateDownload", "rateUpload"],
Expand All @@ -22,13 +20,13 @@ public function __construct()
"http_errors" => false,
"timeout" => 5,
"body" => json_encode($body),
"verify" => false,
];
}

public function test()
{
$test = $this->sendTest();

echo $test->status;
}

Expand All @@ -37,18 +35,18 @@ public function livestats()
$status = "inactive";
$res = $this->sendRequest();
if ($res == null) {
//Log::debug('Transmission connection failed');
return "";
}

$details = json_decode($res->getBody());
if (!isset($details->arguments)) {
//Log::debug('Failed to fetch data from Transmission');
return "";
}

$data = [];

error_log(json_encode($details, JSON_PRETTY_PRINT));

$torrents = $details->arguments->torrents;
$seeding_torrents = 0;
$leeching_torrents = 0;
Expand All @@ -69,18 +67,8 @@ public function livestats()
$status = "active";
}

$data["download_rate"] = format_bytes(
$rateDownload,
false,
" <span>",
"/s</span>"
);
$data["upload_rate"] = format_bytes(
$rateUpload,
false,
" <span>",
"/s</span>"
);
$data["download_rate"] = format_bytes($rateDownload, false, " <span>", "/s</span>");
$data["upload_rate"] = format_bytes($rateUpload, false, " <span>", "/s</span>");
$data["seed_count"] = $seeding_torrents;
$data["leech_count"] = $leeching_torrents;

Expand All @@ -90,37 +78,21 @@ public function livestats()
private function sendTest()
{
$this->setClientOptions();
$test = parent::appTest(
$this->url("transmission/rpc"),
$this->attrs,
$this->vars
);
$test = parent::appTest($this->url("transmission/rpc"), $this->attrs, $this->vars);
if ($test->code === 409) {
$this->setClientOptions();
$test = parent::appTest(
$this->url("transmission/rpc"),
$this->attrs,
$this->vars
);
$test = parent::appTest($this->url("transmission/rpc"), $this->attrs, $this->vars);
}
return $test;
}

private function sendRequest()
{
$this->setClientOptions();
$res = parent::execute(
$this->url("transmission/rpc"),
$this->attrs,
$this->vars
);
$res = parent::execute($this->url("transmission/rpc"), $this->attrs, $this->vars);
if ($res->getStatusCode() === 409) {
$this->setClientOptions();
$res = parent::execute(
$this->url("transmission/rpc"),
$this->attrs,
$this->vars
);
$res = parent::execute($this->url("transmission/rpc"), $this->attrs, $this->vars);
}
return $res;
}
Expand All @@ -134,27 +106,23 @@ private function setClientOptions()
$this->config->password,
"Basic",
],
"verify" => false,
];
} else {
$this->attrs = [
"verify" => false,
];
}
$res = parent::execute(
$this->url("transmission/rpc"),
$this->attrs,
$this->vars
);
$res = parent::execute($this->url("transmission/rpc"), $this->attrs, $this->vars);

try {
//print_r($res);
$xtId = $res->getHeaderLine("X-Transmission-Session-Id");
if ($xtId != null) {
$this->attrs["headers"] = [
"X-Transmission-Session-Id" => $xtId,
];
} else {
//Log::error("Unable to get Transmission session information");
//Log::debug("Status Code: ".$res->getStatusCode());
}
} catch (\GuzzleHttp\Exception\ConnectException $e) {
//Log::error("Failed connection to Transmission");
return false;
}
return true;
Expand Down

0 comments on commit 8255133

Please sign in to comment.