PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/nzbhandler/NzbHandler_Nzbget.php

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