| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- trait TautulliHomepageItem
- {
- public function tautulliSettingsArray()
- {
- return array(
- 'name' => 'Tautulli',
- 'enabled' => strpos('personal', $this->config['license']) !== false,
- 'image' => 'plugins/images/tabs/tautulli.png',
- 'category' => 'Monitor',
- 'settings' => array(
- 'Enable' => array(
- array(
- 'type' => 'switch',
- 'name' => 'homepageTautulliEnabled',
- 'label' => 'Enable',
- 'value' => $this->config['homepageTautulliEnabled']
- ),
- array(
- 'type' => 'select',
- 'name' => 'homepageTautulliAuth',
- 'label' => 'Minimum Authentication',
- 'value' => $this->config['homepageTautulliAuth'],
- 'options' => $this->groupOptions
- )
- ),
- 'Options' => array(
- array(
- 'type' => 'input',
- 'name' => 'tautulliHeader',
- 'label' => 'Title',
- 'value' => $this->config['tautulliHeader'],
- 'help' => 'Sets the title of this homepage module'
- ),
- array(
- 'type' => 'switch',
- 'name' => 'tautulliHeaderToggle',
- 'label' => 'Toggle Title',
- 'value' => $this->config['tautulliHeaderToggle'],
- 'help' => 'Shows/hides the title of this homepage module'
- )
- ),
- 'Connection' => array(
- array(
- 'type' => 'input',
- 'name' => 'tautulliURL',
- 'label' => 'URL',
- 'value' => $this->config['tautulliURL'],
- 'help' => 'URL for Tautulli API, include the IP, the port and the base URL (e.g. /tautulli/) in the URL',
- 'placeholder' => 'http://<ip>:<port>'
- ),
- array(
- 'type' => 'password-alt',
- 'name' => 'tautulliApikey',
- 'label' => 'API Key',
- 'value' => $this->config['tautulliApikey']
- ),
- array(
- 'type' => 'select',
- 'name' => 'homepageTautulliRefresh',
- 'label' => 'Refresh Seconds',
- 'value' => $this->config['homepageTautulliRefresh'],
- 'options' => $this->timeOptions()
- ),
- ),
- 'Library Stats' => array(
- array(
- 'type' => 'switch',
- 'name' => 'tautulliLibraries',
- 'label' => 'Libraries',
- 'value' => $this->config['tautulliLibraries'],
- 'help' => 'Shows/hides the card with library information.',
- ),
- array(
- 'type' => 'select',
- 'name' => 'homepageTautulliLibraryAuth',
- 'label' => 'Minimum Authentication',
- 'value' => $this->config['homepageTautulliLibraryAuth'],
- 'options' => $this->groupOptions
- ),
- ),
- 'Viewing Stats' => array(
- array(
- 'type' => 'switch',
- 'name' => 'tautulliPopularMovies',
- 'label' => 'Popular Movies',
- 'value' => $this->config['tautulliPopularMovies'],
- 'help' => 'Shows/hides the card with Popular Movies information.',
- ),
- array(
- 'type' => 'switch',
- 'name' => 'tautulliPopularTV',
- 'label' => 'Popular TV',
- 'value' => $this->config['tautulliPopularTV'],
- 'help' => 'Shows/hides the card with Popular TV information.',
- ),
- array(
- 'type' => 'switch',
- 'name' => 'tautulliTopMovies',
- 'label' => 'Top Movies',
- 'value' => $this->config['tautulliTopMovies'],
- 'help' => 'Shows/hides the card with Top Movies information.',
- ),
- array(
- 'type' => 'switch',
- 'name' => 'tautulliTopTV',
- 'label' => 'Top TV',
- 'value' => $this->config['tautulliTopTV'],
- 'help' => 'Shows/hides the card with Top TV information.',
- ),
- array(
- 'type' => 'select',
- 'name' => 'homepageTautulliViewsAuth',
- 'label' => 'Minimum Authentication',
- 'value' => $this->config['homepageTautulliViewsAuth'],
- 'options' => $this->groupOptions
- ),
- ),
- 'Misc Stats' => array(
- array(
- 'type' => 'switch',
- 'name' => 'tautulliTopUsers',
- 'label' => 'Top Users',
- 'value' => $this->config['tautulliTopUsers'],
- 'help' => 'Shows/hides the card with Top Users information.',
- ),
- array(
- 'type' => 'switch',
- 'name' => 'tautulliTopPlatforms',
- 'label' => 'Top Platforms',
- 'value' => $this->config['tautulliTopPlatforms'],
- 'help' => 'Shows/hides the card with Top Platforms information.',
- ),
- array(
- 'type' => 'select',
- 'name' => 'homepageTautulliMiscAuth',
- 'label' => 'Minimum Authentication',
- 'value' => $this->config['homepageTautulliMiscAuth'],
- 'options' => $this->groupOptions
- ),
- array(
- 'type' => 'switch',
- 'name' => 'tautulliFriendlyName',
- 'label' => 'Use Friendly Name',
- 'value' => $this->config['tautulliFriendlyName'],
- 'help' => 'Use the friendly name set in tautulli for users.',
- ),
- ),
- 'Test Connection' => array(
- array(
- 'type' => 'blank',
- 'label' => 'Please Save before Testing'
- ),
- array(
- 'type' => 'button',
- 'label' => '',
- 'icon' => 'fa fa-flask',
- 'class' => 'pull-right',
- 'text' => 'Test Connection',
- 'attr' => 'onclick="testAPIConnection(\'tautulli\')"'
- ),
- )
- )
- );
- }
-
- public function testConnectionTautulli()
- {
- if (empty($this->config['tautulliURL'])) {
- $this->setAPIResponse('error', 'Tautulli URL is not defined', 422);
- return false;
- }
- if (empty($this->config['tautulliApikey'])) {
- $this->setAPIResponse('error', 'Tautulli Token is not defined', 422);
- return false;
- }
- $url = $this->qualifyURL($this->config['tautulliURL']);
- $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
- try {
- $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
- $options = $this->requestOptions($this->config['tautulliURL'], false, $this->config['homepageTautulliRefresh']);
- $homestats = Requests::get($homestatsUrl, [], $options);
- if ($homestats->success) {
- $this->setAPIResponse('success', 'API Connection succeeded', 200);
- return true;
- } else {
- $this->setAPIResponse('error', 'Tautulli Error Occurred - Check URL or Credentials', 409);
- return false;
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Tautulli Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
- $this->setAPIResponse('error', $e->getMessage(), 500);
- return false;
- }
- }
-
- public function tautulliHomepagePermissions($key = null)
- {
- $permissions = [
- 'main' => [
- 'enabled' => [
- 'homepageTautulliEnabled'
- ],
- 'auth' => [
- 'homepageTautulliAuth'
- ],
- 'not_empty' => [
- 'tautulliURL',
- 'tautulliApikey'
- ]
- ]
- ];
- if (array_key_exists($key, $permissions)) {
- return $permissions[$key];
- } elseif ($key == 'all') {
- return $permissions;
- } else {
- return [];
- }
- }
-
- public function homepageOrdertautulli()
- {
- if ($this->homepageItemPermissions($this->tautulliHomepagePermissions('main'))) {
- return '
- <div id="' . __FUNCTION__ . '">
- <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Tautulli...</h2></div>
- <script>
- // Tautulli
- homepageTautulli("' . $this->config['homepageTautulliRefresh'] . '");
- // End Tautulli
- </script>
- </div>
- ';
- }
- }
-
- public function getTautulliHomepageData()
- {
- if (!$this->homepageItemPermissions($this->tautulliHomepagePermissions('main'), true)) {
- return false;
- }
- $api = [];
- $url = $this->qualifyURL($this->config['tautulliURL']);
- $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
- $height = $this->getCacheImageSize('h');
- $width = $this->getCacheImageSize('w');
- $nowPlayingHeight = $this->getCacheImageSize('nph');
- $nowPlayingWidth = $this->getCacheImageSize('npw');
- try {
- $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
- $options = $this->requestOptions($this->config['tautulliURL'], false, $this->config['homepageTautulliRefresh']);
- $homestats = Requests::get($homestatsUrl, [], $options);
- if ($homestats->success) {
- $homestats = json_decode($homestats->body, true);
- $api['homestats'] = $homestats['response'];
- // Cache art & thumb for first result in each tautulli API result
- $categories = ['top_movies', 'top_tv', 'popular_movies', 'popular_tv'];
- foreach ($categories as $cat) {
- $key = array_search($cat, array_column($api['homestats']['data'], 'stat_id'));
- $img = $api['homestats']['data'][$key]['rows'][0];
- $this->cacheImage($url . '/pms_image_proxy?img=' . $img['art'] . '&rating_key=' . $img['rating_key'] . '&width=' . $nowPlayingWidth . '&height=' . $nowPlayingHeight, $img['rating_key'] . '-np');
- $this->cacheImage($url . '/pms_image_proxy?img=' . $img['thumb'] . '&rating_key=' . $img['rating_key'] . '&width=' . $width . '&height=' . $height, $img['rating_key'] . '-list');
- $img['art'] = 'plugins/images/cache/' . $img['rating_key'] . '-np.jpg';
- $img['thumb'] = 'plugins/images/cache/' . $img['rating_key'] . '-list.jpg';
- $api['homestats']['data'][$key]['rows'][0] = $img;
- }
- // Cache the platform icon
- $key = array_search('top_platforms', array_column($api['homestats']['data'], 'stat_id'));
- $platform = $api['homestats']['data'][$key]['rows'][0]['platform_name'];
- $this->cacheImage($url . '/images/platforms/' . $platform . '.svg', 'tautulli-' . $platform, 'svg');
- }
- $libstatsUrl = $apiURL . '&cmd=get_libraries';
- $options = $this->requestOptions($this->config['tautulliURL'], false, $this->config['homepageTautulliRefresh']);
- $libstats = Requests::get($libstatsUrl, [], $options);
- if ($libstats->success) {
- $libstats = json_decode($libstats->body, true);
- $api['libstats'] = $libstats['response'];
- $categories = ['movie.svg', 'show.svg', 'artist.svg'];
- foreach ($categories as $cat) {
- $parts = explode('.', $cat);
- $this->cacheImage($url . '/images/libraries/' . $cat, 'tautulli-' . $parts[0], $parts[1]);
- }
- }
- $api['options'] = [
- 'url' => $url,
- 'libraries' => $this->config['tautulliLibraries'],
- 'topMovies' => $this->config['tautulliTopMovies'],
- 'topTV' => $this->config['tautulliTopTV'],
- 'topUsers' => $this->config['tautulliTopUsers'],
- 'topPlatforms' => $this->config['tautulliTopPlatforms'],
- 'popularMovies' => $this->config['tautulliPopularMovies'],
- 'popularTV' => $this->config['tautulliPopularTV'],
- 'title' => $this->config['tautulliHeaderToggle'],
- 'friendlyName' => $this->config['tautulliFriendlyName'],
- ];
- $ids = []; // Array of stat_ids to remove from the returned array
- if (!$this->qualifyRequest($this->config['homepageTautulliLibraryAuth'])) {
- $api['options']['libraries'] = false;
- unset($api['libstats']);
- }
- if (!$this->qualifyRequest($this->config['homepageTautulliViewsAuth'])) {
- $api['options']['topMovies'] = false;
- $api['options']['topTV'] = false;
- $api['options']['popularMovies'] = false;
- $api['options']['popularTV'] = false;
- $ids = array_merge(['top_movies', 'popular_movies', 'popular_tv', 'top_tv'], $ids);
- $api['homestats']['data'] = array_values($api['homestats']['data']);
- }
- if (!$this->qualifyRequest($this->config['homepageTautulliMiscAuth'])) {
- $api['options']['topUsers'] = false;
- $api['options']['topPlatforms'] = false;
- $ids = array_merge(['top_platforms', 'top_users'], $ids);
- $api['homestats']['data'] = array_values($api['homestats']['data']);
- }
- $ids = array_merge(['top_music', 'popular_music', 'last_watched', 'most_concurrent'], $ids);
- foreach ($ids as $id) {
- if ($key = array_search($id, array_column($api['homestats']['data'], 'stat_id'))) {
- unset($api['homestats']['data'][$key]);
- $api['homestats']['data'] = array_values($api['homestats']['data']);
- }
- }
- } catch (Requests_Exception $e) {
- $this->writeLog('error', 'Tautulli Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
- $this->setAPIResponse('error', $e->getMessage(), 500);
- return false;
- };
- $api = isset($api) ? $api : false;
- $this->setAPIResponse('success', null, 200, $api);
- return $api;
- }
- }
|