nzbget.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. trait NZBGetHomepageItem
  3. {
  4. public function testConnectionNZBGet()
  5. {
  6. if (empty($this->config['nzbgetURL'])) {
  7. $this->setAPIResponse('error', 'NZBGet URL is not defined', 422);
  8. return false;
  9. }
  10. try {
  11. $url = $this->qualifyURL($this->config['nzbgetURL']);
  12. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  13. $urlGroups = $url . '/jsonrpc/listgroups';
  14. if ($this->config['nzbgetUsername'] !== '' && $this->decrypt($this->config['nzbgetPassword']) !== '') {
  15. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['nzbgetUsername'], $this->decrypt($this->config['nzbgetPassword']))));
  16. $options = array_merge($options, $credentials);
  17. }
  18. $response = Requests::get($urlGroups, array(), $options);
  19. if ($response->success) {
  20. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  21. return true;
  22. } else {
  23. $this->setAPIResponse('success', 'NZBGet: An Error Occurred', 500);
  24. return false;
  25. }
  26. } catch (Requests_Exception $e) {
  27. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  28. $this->setAPIResponse('error', $e->getMessage(), 500);
  29. return false;
  30. }
  31. }
  32. public function getNzbgetHomepageQueue()
  33. {
  34. if (!$this->config['homepageNzbgetEnabled']) {
  35. $this->setAPIResponse('error', 'NZBGet homepage item is not enabled', 409);
  36. return false;
  37. }
  38. if (!$this->qualifyRequest($this->config['homepageNzbgetAuth'])) {
  39. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  40. return false;
  41. }
  42. if (empty($this->config['nzbgetURL'])) {
  43. $this->setAPIResponse('error', 'NZBGet URL is not defined', 422);
  44. return false;
  45. }
  46. try {
  47. $url = $this->qualifyURL($this->config['nzbgetURL']);
  48. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  49. $urlGroups = $url . '/jsonrpc/listgroups';
  50. $urlHistory = $url . '/jsonrpc/history';
  51. if ($this->config['nzbgetUsername'] !== '' && $this->decrypt($this->config['nzbgetPassword']) !== '') {
  52. $credentials = array('auth' => new Requests_Auth_Basic(array($this->config['nzbgetUsername'], $this->decrypt($this->config['nzbgetPassword']))));
  53. $options = array_merge($options, $credentials);
  54. }
  55. $response = Requests::get($urlGroups, array(), $options);
  56. if ($response->success) {
  57. $api['content']['queueItems'] = json_decode($response->body, true);
  58. }
  59. $response = Requests::get($urlHistory, array(), $options);
  60. if ($response->success) {
  61. $api['content']['historyItems'] = json_decode($response->body, true);
  62. }
  63. $api['content'] = isset($api['content']) ? $api['content'] : false;
  64. $this->setAPIResponse('success', null, 200, $api);
  65. return $api;
  66. } catch (Requests_Exception $e) {
  67. $this->writeLog('error', 'NZBGet Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  68. $this->setAPIResponse('error', $e->getMessage(), 500);
  69. return false;
  70. }
  71. }
  72. }