Browse Source

update class for arrs to check version of api

CauseFX 5 years ago
parent
commit
55de41fb4f

+ 3 - 3
api/homepage/lidarr.php

@@ -162,7 +162,7 @@ trait LidarrHomepageItem
 		$list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
 		foreach ($list as $key => $value) {
 			try {
-				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], true);
+				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr');
 				$results = $downloader->getSystemStatus();
 				$downloadList = json_decode($results, true);
 				if (is_array($downloadList) || is_object($downloadList)) {
@@ -242,7 +242,7 @@ trait LidarrHomepageItem
 		$list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
 		foreach ($list as $key => $value) {
 			try {
-				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], true);
+				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr');
 				$results = $downloader->getQueue();
 				$downloadList = json_decode($results, true);
 				if (is_array($downloadList) || is_object($downloadList)) {
@@ -275,7 +275,7 @@ trait LidarrHomepageItem
 		$list = $this->csvHomepageUrlToken($this->config['lidarrURL'], $this->config['lidarrToken']);
 		foreach ($list as $key => $value) {
 			try {
-				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], true);
+				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'lidarr');
 				$results = $downloader->getCalendar($startDate, $endDate);
 				$result = json_decode($results, true);
 				if (is_array($result) || is_object($result)) {

+ 3 - 3
api/homepage/radarr.php

@@ -214,7 +214,7 @@ trait RadarrHomepageItem
 		$list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
 		foreach ($list as $key => $value) {
 			try {
-				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
+				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'radarr');
 				$results = $downloader->getSystemStatus();
 				$downloadList = json_decode($results, true);
 				if (is_array($downloadList) || is_object($downloadList)) {
@@ -313,7 +313,7 @@ trait RadarrHomepageItem
 		$list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
 		foreach ($list as $key => $value) {
 			try {
-				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
+				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'radarr');
 				$results = $downloader->getQueue();
 				$downloadList = json_decode($results, true);
 				if (is_array($downloadList) || is_object($downloadList)) {
@@ -346,7 +346,7 @@ trait RadarrHomepageItem
 		$list = $this->csvHomepageUrlToken($this->config['radarrURL'], $this->config['radarrToken']);
 		foreach ($list as $key => $value) {
 			try {
-				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
+				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'radarr');
 				$results = $downloader->getCalendar($startDate, $endDate, $this->config['radarrUnmonitored']);
 				$result = json_decode($results, true);
 				if (is_array($result) || is_object($result)) {

+ 3 - 3
api/homepage/sonarr.php

@@ -196,7 +196,7 @@ trait SonarrHomepageItem
 		$list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
 		foreach ($list as $key => $value) {
 			try {
-				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
+				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr');
 				$results = $downloader->getSystemStatus();
 				$downloadList = json_decode($results, true);
 				if (is_array($downloadList) || is_object($downloadList)) {
@@ -295,7 +295,7 @@ trait SonarrHomepageItem
 		$list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
 		foreach ($list as $key => $value) {
 			try {
-				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
+				$downloader = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr');
 				$results = $downloader->getQueue();
 				$downloadList = json_decode($results, true);
 				if (is_array($downloadList) || is_object($downloadList)) {
@@ -328,7 +328,7 @@ trait SonarrHomepageItem
 		$list = $this->csvHomepageUrlToken($this->config['sonarrURL'], $this->config['sonarrToken']);
 		foreach ($list as $key => $value) {
 			try {
-				$sonarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
+				$sonarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token'], 'sonarr');
 				$sonarr = $sonarr->getCalendar($startDate, $endDate, $this->config['sonarrUnmonitored']);
 				$result = json_decode($sonarr, true);
 				if (is_array($result) || is_object($result)) {

+ 2 - 1
api/vendor/kryptonit3/sonarr/composer.json

@@ -9,7 +9,8 @@
         }
     ],
     "require": {
-        "guzzlehttp/guzzle": "^6.1"
+        "guzzlehttp/guzzle": "^6.1",
+        "composer/semver": "^1.4"
     },
     "autoload": {
         "psr-4": {

+ 63 - 11
api/vendor/kryptonit3/sonarr/src/Sonarr.php

@@ -3,6 +3,7 @@
 namespace Kryptonit3\Sonarr;
 
 use GuzzleHttp\Client;
+use Composer\Semver\Comparator;
 
 class Sonarr
 {
@@ -11,11 +12,11 @@ class Sonarr
     protected $httpAuthUsername;
     protected $httpAuthPassword;
 
-    public function __construct($url, $apiKey, $lidarr = false, $httpAuthUsername = null, $httpAuthPassword = null)
+    public function __construct($url, $apiKey, $type = 'sonarr', $httpAuthUsername = null, $httpAuthPassword = null)
     {
         $this->url = rtrim($url, '/\\'); // Example: http://127.0.0.1:8989 (no trailing forward-backward slashes)
 	    $this->apiKey = $apiKey;
-	    $this->lidarr = $lidarr;
+	    $this->type = strtolower($type);
         $this->httpAuthUsername = $httpAuthUsername;
         $this->httpAuthPassword = $httpAuthPassword;
     }
@@ -65,7 +66,7 @@ class Sonarr
         if ( $sonarrUnmonitored == 'true' ) {
             $uriData['unmonitored'] = 'true';
 		}
-	    if ( $this->lidarr == true ) {
+	    if ( $this->type == 'lidarr' ) {
 		    $uriData['includeArtist'] = 'true';
 	    }
 	    $response = [
@@ -582,7 +583,7 @@ class Sonarr
             'data' => []
         ];
 
-        return $this->processRequest($response);
+        return $this->preProcessRequest($response);
     }
 
     /**
@@ -606,29 +607,33 @@ class Sonarr
                 $this->httpAuthPassword
             ];
         }
-	    $lidarr = ( $this->lidarr == true ) ? 'v1/' : '';
+		if($this->type == 'lidarr'){
+			$params['version'] = 'v1/';
+		}
+	    $version = $params['version'] ?? '';
+        
         if ( $params['type'] == 'get' ) {
-            $url = $this->url . '/api/' . $lidarr . $params['uri'] . '?' . http_build_query($params['data']);
+            $url = $this->url . '/api/' . $version . $params['uri'] . '?' . http_build_query($params['data']);
 
             return $client->get($url, $options);
         }
 
         if ( $params['type'] == 'put' ) {
-            $url = $this->url . '/api/' . $lidarr . $params['uri'];
+            $url = $this->url . '/api/' . $version . $params['uri'];
             $options['json'] = $params['data'];
 
             return $client->put($url, $options);
         }
 
         if ( $params['type'] == 'post' ) {
-            $url = $this->url . '/api/' . $lidarr . $params['uri'];
+            $url = $this->url . '/api/' . $version . $params['uri'];
             $options['json'] = $params['data'];
 
             return $client->post($url, $options);
         }
 
         if ( $params['type'] == 'delete' ) {
-            $url = $this->url . '/api/' . $lidarr . $params['uri'] . '?' . http_build_query($params['data']);
+            $url = $this->url . '/api/' . $version . $params['uri'] . '?' . http_build_query($params['data']);
 
             return $client->delete($url, $options);
         }
@@ -642,12 +647,39 @@ class Sonarr
      */
     protected function processRequest(array $request)
     {
+	    try {
+		    $versionCheck = $this->getSystemStatus();
+		    $versionCheck = json_decode($versionCheck)['version'];
+		    $compare = new Comparator;
+		    switch ($this->type){
+			    case 'sonarr':
+				    $versionCheck = '';
+				    break;
+			    case 'radarr':
+			    	$versionCheck =  ($compare->lessThan('2.9.9', $versionCheck)) ? '' : 'v3/';
+			    	break;
+			    case 'lidarr':
+				    $versionCheck = 'v1/';
+				    break;
+			    default:
+				    $versionCheck = '';
+		    }
+	    } catch ( \Exception $e ) {
+		    return json_encode(array(
+			    'error' => array(
+				    'msg' => $e->getMessage(),
+				    'code' => $e->getCode(),
+			    ),
+		    ));
+		    exit();
+	    }
         try {
             $response = $this->_request(
                 [
                     'uri' => $request['uri'],
                     'type' => $request['type'],
-                    'data' => $request['data']
+                    'data' => $request['data'],
+	                'version' => $versionCheck
                 ]
             );
         } catch ( \Exception $e ) {
@@ -657,12 +689,32 @@ class Sonarr
                     'code' => $e->getCode(),
                 ),
             ));
-
             exit();
         }
 
         return $response->getBody()->getContents();
     }
+    protected function preProcessRequest(array $request)
+    {
+	    try {
+		    $response = $this->_request(
+			    [
+				    'uri' => $request['uri'],
+				    'type' => $request['type'],
+				    'data' => $request['data']
+			    ]
+		    );
+	    } catch ( \Exception $e ) {
+		    return json_encode(array(
+			    'error' => array(
+				    'msg' => $e->getMessage(),
+				    'code' => $e->getCode(),
+			    ),
+		    ));
+		
+		    exit();
+	    }
+    }
 
     /**
      * Verify date is in proper format

+ 31 - 21
js/functions.js

@@ -5733,6 +5733,11 @@ function buildDownloaderItem(array, source, type='none'){
 			}
 			if(array.content.queueItems == 0){
 				queue = '<tr><td class="max-texts" lang="en">Nothing in queue</td></tr>';
+				break;
+			}
+			if(array.content.queueItems.records == 0){
+				queue = '<tr><td class="max-texts" lang="en">Nothing in queue</td></tr>';
+				break;
 			}
 			$.each(array.content.queueItems, function(i,v) {
 				count = count + 1;
@@ -5764,28 +5769,33 @@ function buildDownloaderItem(array, source, type='none'){
 			}
 			if(array.content.queueItems == 0){
 				queue = '<tr><td class="max-texts" lang="en">Nothing in queue</td></tr>';
+				break;
 			}
-			$.each(array.content.queueItems, function(i,v) {
-				if(v.hasOwnProperty('movie')) {
-					count = count + 1;
-					var percent = Math.floor(((v.size - v.sizeleft) / v.size) * 100);
-					percent = (isNaN(percent)) ? '0' : percent;
-					var size = v.size != -1 ? humanFileSize(v.size, false) : "?";
-					v.name = v.movie.title;
-					queue += `
-	                <tr>
-	                    <td class="max-texts">` + v.name + `</td>
-	                    <td class="hidden-xs sonarr-` + cleanClass(v.status) + `">` + v.status + `</td>
-	                    <td class="hidden-xs">` + size + `</td>
-	                    <td class="hidden-xs"><span class="label label-info">` + v.protocol + `</span></td>
-	                    <td class="text-right">
-	                        <div class="progress progress-lg m-b-0">
-	                            <div class="progress-bar progress-bar-info" style="width: ` + percent + `%;" role="progressbar">` + percent + `%</div>
-	                        </div>
-	                    </td>
-	                </tr>
-	                `;
-				}
+			if(array.content.queueItems.records == 0){
+				queue = '<tr><td class="max-texts" lang="en">Nothing in queue</td></tr>';
+				break;
+			}
+			let queueSet = (typeof array.content.queueItems.records == 'undefined') ? array.content.queueItems : array.content.queueItems.records;
+			$.each(queueSet, function(i,v) {
+				count = count + 1;
+				var percent = Math.floor(((v.size - v.sizeleft) / v.size) * 100);
+				percent = (isNaN(percent)) ? '0' : percent;
+				var size = v.size != -1 ? humanFileSize(v.size, false) : "?";
+				v.name = (typeof v.movie == 'undefined') ? v.title : v.movie.title;
+				queue += `
+                <tr>
+                    <td class="max-texts">` + v.name + `</td>
+                    <td class="hidden-xs sonarr-` + cleanClass(v.status) + `">` + v.status + `</td>
+                    <td class="hidden-xs">` + size + `</td>
+                    <td class="hidden-xs"><span class="label label-info">` + v.protocol + `</span></td>
+                    <td class="text-right">
+                        <div class="progress progress-lg m-b-0">
+                            <div class="progress-bar progress-bar-info" style="width: ` + percent + `%;" role="progressbar">` + percent + `%</div>
+                        </div>
+                    </td>
+                </tr>
+                `;
+
 			});
 			if(queue == ''){
 				queue = '<tr><td class="max-texts" lang="en">Nothing in queue</td></tr>';