Heimdall/app/SupportedApps/Nzbget.php

82 lines
2.5 KiB
PHP
Raw Normal View History

<?php namespace App\SupportedApps;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Client;
class Nzbget implements Contracts\Applications, Contracts\Livestats {
public $config;
public function defaultColour()
{
return '#253827';
}
2018-02-05 20:59:38 +00:00
public function icon()
{
return 'supportedapps/nzbget.png';
}
2018-02-05 23:08:37 +00:00
public function configDetails()
{
return 'nzbget';
}
public function testConfig()
{
$res = $this->buildRequest('status');
switch($res->getStatusCode()) {
case 200:
echo 'Successfully connected to the API';
break;
case 401:
echo 'Failed: Invalid credentials';
break;
2018-02-08 19:28:23 +00:00
case 404:
echo 'Failed: Please make sure your URL is correct and that there is a trailing slash';
break;
default:
2018-02-08 19:24:30 +00:00
echo 'Something went wrong... Code: '.$res->getStatusCode();
break;
}
}
public function executeConfig()
{
$output = '';
2018-02-08 15:50:53 +00:00
$res = $this->buildRequest('status');
$data = json_decode($res->getBody());
2018-02-08 18:53:52 +00:00
//$data->result->RemainingSizeMB = '10000000';
//$data->result->DownloadRate = '100000000';
$size = $data->result->RemainingSizeMB;
$rate = $data->result->DownloadRate;
$queue_size = format_bytes($size*1000*1000, false, ' <span>', '</span>');
$current_speed = format_bytes($rate, false, ' <span>');
2018-02-08 15:50:53 +00:00
if($size > 0 || $rate > 0) {
$output = '
<ul class="livestats">
<li><span class="title">Queue</span><strong>'.$queue_size.'</strong></li>
<li><span class="title">Speed</span><strong>'.$current_speed.'/s</span></strong></li>
</ul>
';
}
2018-02-08 15:50:53 +00:00
return $output;
2018-02-05 23:08:37 +00:00
}
public function buildRequest($endpoint)
{
$config = $this->config;
$url = $config->url;
$username = $config->username;
$password = $config->password;
$rebuild_url = str_replace('http://', 'http://'.$username.':'.$password.'@', $url);
$rebuild_url = str_replace('https://', 'https://'.$username.':'.$password.'@', $rebuild_url);
$rebuild_url = rtrim($rebuild_url, '/');
$api_url = $rebuild_url.'/jsonrpc/'.$endpoint;
$client = new Client(['http_errors' => false]);
$res = $client->request('GET', $api_url);
return $res;
}
2018-02-05 20:59:38 +00:00
}