committed by
							
								 GitHub
								GitHub
							
						
					
				
				 5 changed files with 137 additions and 0 deletions
			
			
		| @ -0,0 +1,122 @@ | |||||
|  | <?php namespace App\SupportedApps; | ||||
|  | 
 | ||||
|  | use GuzzleHttp\Exception\GuzzleException; | ||||
|  | use GuzzleHttp\Client; | ||||
|  | use Illuminate\Support\Facades\Log; | ||||
|  | 
 | ||||
|  | class CouchPotato implements Contracts\Applications, Contracts\Livestats | ||||
|  | { | ||||
|  | 
 | ||||
|  |     private $_client; | ||||
|  | 
 | ||||
|  |     public function __construct() | ||||
|  |     { | ||||
|  |         $this->_client = new Client( | ||||
|  |             ['http_errors' => false, | ||||
|  |             'timeout' => 10] | ||||
|  |         ); | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     public function defaultColour() | ||||
|  |     { | ||||
|  |         return '#363840'; | ||||
|  |     } | ||||
|  |     public function icon() | ||||
|  |     { | ||||
|  |         return 'supportedapps/couchPotato.png'; | ||||
|  |     } | ||||
|  |     public function configDetails() | ||||
|  |     { | ||||
|  |         return 'couchpotato'; | ||||
|  |     } | ||||
|  |     public function testConfig() | ||||
|  |     { | ||||
|  |         $res = $this->sendRequest(); | ||||
|  |         if ($res == null) { | ||||
|  |             echo 'CouchPotato connection failed'; | ||||
|  |             return; | ||||
|  |         } | ||||
|  |         switch($res->getStatusCode()) { | ||||
|  |         case 200: | ||||
|  |             echo "Successfully connected to CouchPotato"; | ||||
|  |             break; | ||||
|  |         case 401: | ||||
|  |             echo 'Failed: Invalid credentials'; | ||||
|  |             break; | ||||
|  |         case 404: | ||||
|  |             echo 'Failed: Please make sure your URL is correct and includes the port'; | ||||
|  |             break; | ||||
|  |         case 409: | ||||
|  |             echo 'Failed: Incorrect session id'; | ||||
|  |             break; | ||||
|  |         default: | ||||
|  |             echo 'Something went wrong... Code: '.$res->getStatusCode(); | ||||
|  |             break; | ||||
|  |         } | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     public function executeConfig() | ||||
|  |     { | ||||
|  |         $html = ''; | ||||
|  |         $res = $this->sendRequest(); | ||||
|  |         if ($res == null) { | ||||
|  |             Log::debug('CouchPotato connection failed'); | ||||
|  |             return ''; | ||||
|  |         } | ||||
|  |         $data = json_decode($res->getBody()); | ||||
|  |         if (! isset($data->movies)) { | ||||
|  |             Log::debug('Failed to fetch data from CouchPotato'); | ||||
|  |             return ''; | ||||
|  |         } | ||||
|  |         $movies = $data->movies; | ||||
|  |         $wantedMovies = $availableMovies = 0; | ||||
|  |         foreach ($movies as $v) { | ||||
|  |             switch ($v->status) { | ||||
|  |             case 'active': | ||||
|  |                 $wantedMovies++; | ||||
|  |                 break; | ||||
|  |             case 'done': | ||||
|  |                 $availableMovies++; | ||||
|  |                 break; | ||||
|  |             default: | ||||
|  |                 Log::warning('Unexpected CouchPotato status received: '.$v['status']); | ||||
|  |                 break; | ||||
|  |             } | ||||
|  |         } | ||||
|  | 
 | ||||
|  |         $html = ' | ||||
|  |         <ul class="livestats"> | ||||
|  |             <li><span class="title">Wanted</span><sub>'.$wantedMovies.'</sub></li> | ||||
|  |             <li><span class="title">Available</span><sub>'.$availableMovies.'</sub></li> | ||||
|  |         </ul> | ||||
|  |         '; | ||||
|  |         return json_encode(['status' => 'inactive', 'html' => $html]); | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     private function sendRequest() | ||||
|  |     { | ||||
|  |         $res = null; | ||||
|  |         try{ | ||||
|  |             $res = $this->_client->request( | ||||
|  |                 'GET', | ||||
|  |                 $this->getApiUrl() | ||||
|  |             ); | ||||
|  |         }catch(\GuzzleHttp\Exception\BadResponseException $e){ | ||||
|  |             Log::error("Connection to {$e->getRequest()->getUrl()} failed"); | ||||
|  |             Log::debug($e->getMessage()); | ||||
|  |             $res = $e->getRequest(); | ||||
|  |         }catch(\GuzzleHttp\Exception\ConnectException $e) { | ||||
|  |             Log::error("CouchPotato connection refused"); | ||||
|  |             Log::debug($e->getMessage()); | ||||
|  |         } | ||||
|  |         return $res; | ||||
|  |     } | ||||
|  | 
 | ||||
|  |     private function getApiUrl() | ||||
|  |     { | ||||
|  |         $url = $this->config->url; | ||||
|  |         $url = rtrim($url, '/'); | ||||
|  |         $apiUrl = $url.'/api/'.$this->config->apikey.'/movie.list'; | ||||
|  |         return $apiUrl; | ||||
|  |     } | ||||
|  | } | ||||
| @ -0,0 +1,13 @@ | |||||
|  | <h2>{{ __('app.apps.config') }} ({{ __('app.optional') }})</h2> | ||||
|  | <div class="items"> | ||||
|  |     <input type="hidden" name="config[enabled]" value="1" /> | ||||
|  |     <input type="hidden" data-config="dataonly" class="config-item" name="config[dataonly]" value="1" /> | ||||
|  |     <input type="hidden" data-config="type" class="config-item" name="config[type]" value="\App\SupportedApps\CouchPotato" /> | ||||
|  |     <div class="input"> | ||||
|  |         <label>{{ __('app.apps.apikey') }}</label> | ||||
|  |         {!! Form::text('config[apikey]', null, array('placeholder' => __('app.apps.apikey'), 'data-config' => 'apikey', 'class' => 'form-control config-item')) !!} | ||||
|  |     </div> | ||||
|  |     <div class="input"> | ||||
|  |         <button style="margin-top: 32px;" class="btn test" id="test_config">Test</button> | ||||
|  |     </div> | ||||
|  | </div> | ||||
| After Width: | Height: | Size: 5.2 KiB | 
					Loading…
					
					
				
		Reference in new issue