1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
<?php
class NzbHandler_Nzbget extends NzbHandler_abs
{
private $_host = null;
private $_timeout = null;
private $_url = null;
private $_credentials = null;
function __construct(SpotSettings $settings, array $nzbHandling)
{
parent::__construct($settings, 'NZBGet', 'D/L', $nzbHandling);
$nzbget = $nzbHandling['nzbget'];
$this->_host = $nzbget['host'];
$this->_timeout = $nzbget['timeout'];
$this->_url = "http://" . $nzbget['host'] . ":" . $nzbget['port'] . "/jsonrpc";
$this->_credentials = base64_encode($nzbget['username'] . ":" . $nzbget['password']);
} # __construct
public function processNzb($fullspot, $nzblist)
{
$filename = $this->cleanForFileSystem($fullspot['title']) . '.nzb';
# nzbget does not support zip files, must merge
$nzb = $this->mergeNzbList($nzblist);
$category = $this->convertCatToSabnzbdCat($fullspot);
return $this->uploadNzb($filename, $category, false, $nzb);
} # processNzb
private function sendRequest($method, $args)
{
$reqarr = array('version' => '1.1', 'method' => $method, 'params' => $args);
$content = json_encode($reqarr);
# creeer de header
$header = array("Host: ". $this->_host,
"Authorization: Basic " . $this->_credentials,
"Content-type: application/json",
"Content-Length: " . strlen($content));
$output = $this->sendHttpRequest('POST', $this->_url, $header, $content, $this->_timeout);
if ($output === false)
{
error_log("ERROR: Could not decode json-data for NZBGet method '" . $method ."'");
throw new Exception("ERROR: Could not decode json-data for NZBGet method '" . $method ."'");
}
$response = json_decode($output, true);
if (is_array($response) && isset($response['error']) && isset($response['error']['code']))
{
error_log("NZBGet RPC: Method '" . $method . "', " . $response['error']['message'] . " (" . $response['error']['code'] . ")");
throw new Exception("NZBGet RPC: Method '" . $method . "', " . $response['error']['message'] . " (" . $response['error']['code'] . ")");
}
else if (is_array($response) && isset($response['result']))
{
$response = $response['result'];
}
return $response;
} # sendRequest
# NzbHandler API functions
/**
* Check if handler is available
* @return boolean
*/
public function isAvailable()
{
try {
$this -> getStatus ( );
} catch ( Exception $e ) {
return false;
}
return true;
}
/*
* Return the supported API functions for this NzbHandler imlementation
*/
public function hasApiSupport()
{
$api = "getStatus,pauseQueue,resumeQueue,setSpeedLimit,moveDown,moveUp"
. ",moveTop,moveBottom,setCategory,delete,pause,resume,getVersion";
// add functions for NZBGet v0.8.0 and higher
if ($this->getVersion() >= "0.8.0")
{
$api .= ",setPriority,rename";
}
return $api;
} # hasApiSupport
/*
* NZBGet API method: append
* Add an NZB file to download queue
*/
public function uploadNzb($filename, $category, $addToTop, $nzb)
{
$args = array($filename, $category, $addToTop, base64_encode($nzb));
return $this->sendrequest('append', $args);
} # nzbgetApi_append
/*
* NZBGet API method: status
* The getStatus() method returns a JSON object containing the following
* name/value pairs:
*
* queue.status
* queue.paused
* queue.speedlimit
* queue.freediskspace
* queue.totaldiskspace
* queue.bytepersec
* queue.secondsremaining
* queue.mbsize
* queue.mbremaining
* queue.nrofdownloads
* download[].paused
* download[].id
* download[].filename
* download[].category
* download[].mbsize
* download[].mbremaining
* download[].percentage
*/
public function getStatus()
{
$status = $this->sendrequest('status', null);
$listgroups = $this->sendrequest('listgroups', null);
$result = array();
if ($status['ServerPaused'] != true)
{
$result['queue']['status'] = ($status['ServerStandBy'] == true)?"Idle":"Active";
}
else
{
$result['queue']['status'] = 'Paused';
}
$result['queue']['paused'] = $status['ServerPaused'];
$result['queue']['speedlimit'] = round($status['DownloadLimit']/1024);
$result['queue']['freediskspace'] = "-";
$result['queue']['totaldiskspace'] = "-";
$result['queue']['bytepersec'] = $status['DownloadRate'];
$result['queue']['mbsize'] = 0;
$result['queue']['mbremaining'] = $status['RemainingSizeMB'];
$secondsremaining = 0;
if ($status['DownloadRate'] != 0)
{
if ($status['RemainingSizeLo'] < 0)
{
$secondsremaining = $status['RemainingSizeMB'] / ($status['DownloadRate'] / 1024 /1024);
}
else
{
$secondsremaining = $status['RemainingSizeLo'] / $status['DownloadRate'];
}
}
$result['queue']['secondsremaining'] = (int)($secondsremaining);
$downloads = array();
for ($i = 0; $i < count($listgroups); $i++)
{
$downloads[$i]['paused'] = ($listgroups[$i]['PausedSizeLo'] > 0);
$downloads[$i]['id'] = $listgroups[$i]['LastID'];
$downloads[$i]['filename'] = $listgroups[$i]['NZBNicename'];
$downloads[$i]['category'] = $listgroups[$i]['Category'];
$downloads[$i]['mbsize'] = $listgroups[$i]['FileSizeMB'];
$downloads[$i]['mbremaining'] = $listgroups[$i]['RemainingSizeMB'];
$downloads[$i]['percentage'] = 0;
if ($listgroups[$i]['FileSizeMB'] > 0)
{
$downloads[$i]['percentage'] = round((($listgroups[$i]['FileSizeMB'] - $listgroups[$i]['RemainingSizeMB']) / $listgroups[$i]['FileSizeMB']) * 100);
}
$result['queue']['mbsize'] = $result['queue']['mbsize'] + $downloads[$i]['mbsize'];
}
$result['queue']['slots'] = $downloads;
$result['queue']['nrofdownloads'] = count($downloads);
return $result;
} # getStatus
/*
* NZBGet API method: pause
* Pause the download queue
*/
public function pauseQueue()
{
return $this->sendrequest('pause', null);
} #pauseQueue
/*
* NZBGet API method: resume
* Resume the download queue when paused
*/
public function resumeQueue()
{
return $this->sendrequest('resume', null);
} # resumeQueue
/*
* NZBGet API method: rate
* Set the maximum download rate
*/
public function setSpeedLimit(int $limit)
{
$args = array((int)$limit);
return $this->sendrequest('rate', $args);
} # setSpeedLimit
/*
* NZBGet API method: editqueue
* Move a download one position down in the queue
*/
public function moveDown($id)
{
$args = array('groupmoveoffset', (int)1, '', (int)$id);
return $this->sendrequest('editqueue', $args);
} # moveDown
/*
* NZBGet API method: editqueue
* Move a download one position up in the queue
*/
public function moveUp($id)
{
$args = array('groupmoveoffset', (int)-1, '', (int)$id);
return $this->sendrequest('editqueue', $args);
} # moveUp
/*
* NZBGet API method: editqueue
* Move a download to the top of the queue
*/
public function moveTop($id)
{
$args = array('groupmovetop', 0, '', (int)$id);
return $this->sendrequest('editqueue', $args);
} # moveTop
/*
* NZBGet API method: editqueue
* Move a download to the bottom of the queue
*/
public function moveBottom($id)
{
$args = array('groupmovebottom', 0, '', (int)$id);
return $this->sendrequest('editqueue', $args);
} # moveBottom
/*
* NZBGet API method: editqueue
* Set the category for a download
*/
public function setCategory($id, $category)
{
$args = array('groupsetcategory', (int)0, $category, (int)$id);
return $this->sendrequest('editqueue', $args);
} # setCategory
/*
* NZBGet API method: editqueue
* Set the priority for a download
* Only supported when using NZBGet v0.8.0 (or higher)
*/
public function setPriority($id, $priority)
{
if ($this->getVersion() < "0.8.0") return false;
# parse integer value a string
$priority = '' + $priority;
$args = array('groupsetpriority', (int)0, $priority, (int)$id);
return $this->sendrequest('editqueue', $args);
} # setPriority
/*
* NZBGet API method: -
* Not implemented yet. Could be added using the editqueue method and using the
* GroupSetParameter parameter to set a postprocessing parameter. This would however
* also require support in the used post-process script.
*/
public function setPassword($id, $password)
{
return false;
} # setPassword
/*
* NZBGet API method: editqueue
* Delete a download from the queue
*/
public function delete($id)
{
$args = array('groupdelete', (int)0, '', (int)$id);
return $this->sendrequest('editqueue', $args);
} # delete
/*
* NZBGet API method: editqueue
* Rename a download
* Only supported when using NZBGet v0.8.0 (or higher)
*/
public function rename($id, $name)
{
if ($this->getVersion() < "0.8.0") return false;
$name = cleanForFileSystem($name);
$args = array('groupsetname', (int)0, $name, (int)$id);
return $this->sendrequest('editqueue', $args);
} # rename
/*
* NZBGet API method: editqueue
* Pause a download in the queue
*/
public function pause($id)
{
$args = array('grouppause', (int)0, '', (int)$id);
return $this->sendrequest('editqueue', $args);
} # pause
/*
* NZBGet API method: editqueue
* Resume a paused download in the queue
*/
public function resume($id)
{
$args = array('groupresume', (int)0, '', (int)$id);
return $this->sendrequest('editqueue', $args);
} # resume
/*
* NZBGet API method: -
* Since NZBGet will simply create a category directory if it does not exist yet,
* NZBGet does not have a fixed list of categories. Therefor we'll use the list of
* categories defined in SpotWeb.
* The 'readonly' name/value pair is set to false to allow for a template to offer a
* free text field so that the user can assign a category name not defined in the
* category list.
*/
public function getCategories()
{
$result = parent::getCategories();
// allow adding of adhoc categories
$result['readonly'] = false;
return $result;
} # getCategories
/*
* NZBGet API method: version
* Returns the version of NZBGet
*/
public function getVersion()
{
return $this->sendrequest('version', null);
} # getVersion
} # class NzbHandler_Nzbget
|