tautulli.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. trait TautulliHomepageItem
  3. {
  4. public function tautulliSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Tautulli',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/tautulli.png',
  10. 'category' => 'Monitor',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageTautulliEnabled'),
  21. $this->settingsOption('auth', 'homepageTautulliAuth'),
  22. ],
  23. 'Options' => [
  24. $this->settingsOption('title', 'tautulliHeader'),
  25. $this->settingsOption('toggle-title', 'tautulliHeaderToggle'),
  26. $this->settingsOption('refresh', 'homepageTautulliRefresh'),
  27. ],
  28. 'Connection' => [
  29. $this->settingsOption('multiple-url', 'tautulliURL'),
  30. $this->settingsOption('multiple-api-key', 'tautulliApikey'),
  31. $this->settingsOption('disable-cert-check', 'tautulliDisableCertCheck'),
  32. $this->settingsOption('use-custom-certificate', 'tautulliUseCustomCertificate'),
  33. ],
  34. 'API SOCKS' => [
  35. $this->settingsOption('socks', 'tautulli'),
  36. $this->settingsOption('blank'),
  37. $this->settingsOption('enable', 'tautulliSocksEnabled'),
  38. $this->settingsOption('auth', 'tautulliSocksAuth'),
  39. ],
  40. 'Library Stats' => [
  41. $this->settingsOption('switch', 'tautulliLibraries', ['label' => 'Libraries', 'help' => 'Shows/hides the card with library information.']),
  42. $this->settingsOption('auth', 'homepageTautulliLibraryAuth'),
  43. ],
  44. 'Viewing Stats' => [
  45. $this->settingsOption('switch', 'tautulliPopularMovies', ['label' => 'Popular Movies', 'help' => 'Shows/hides the card with Popular Movie information.']),
  46. $this->settingsOption('switch', 'tautulliPopularTV', ['label' => 'Popular TV', 'help' => 'Shows/hides the card with Popular TV information.']),
  47. $this->settingsOption('switch', 'tautulliTopMovies', ['label' => 'Top Movies', 'help' => 'Shows/hides the card with Top Movies information.']),
  48. $this->settingsOption('switch', 'tautulliTopTV', ['label' => 'Top TV', 'help' => 'Shows/hides the card with Top TV information.']),
  49. $this->settingsOption('auth', 'homepageTautulliViewsAuth'),
  50. ],
  51. 'Misc Stats' => [
  52. $this->settingsOption('switch', 'tautulliTopUsers', ['label' => 'Top Users', 'help' => 'Shows/hides the card with Top Users information.']),
  53. $this->settingsOption('switch', 'tautulliTopPlatforms', ['label' => 'Top Platforms', 'help' => 'Shows/hides the card with Top Platforms information.']),
  54. $this->settingsOption('auth', 'homepageTautulliMiscAuth'),
  55. $this->settingsOption('switch', 'tautulliFriendlyName', ['label' => 'Use Friendly Name', 'help' => 'Use the friendly name set in tautulli for users.']),
  56. ],
  57. 'Test Connection' => [
  58. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  59. $this->settingsOption('test', 'tautulli'),
  60. ]
  61. ]
  62. ];
  63. return array_merge($homepageInformation, $homepageSettings);
  64. }
  65. public function testConnectionTautulli()
  66. {
  67. $this->setLoggerChannel('Tautulli Homepage');
  68. if (empty($this->config['tautulliURL'])) {
  69. $this->setAPIResponse('error', 'Tautulli URL is not defined', 422);
  70. return false;
  71. }
  72. if (empty($this->config['tautulliApikey'])) {
  73. $this->setAPIResponse('error', 'Tautulli Token is not defined', 422);
  74. return false;
  75. }
  76. $url = $this->qualifyURL($this->config['tautulliURL']);
  77. $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
  78. try {
  79. $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
  80. $options = $this->requestOptions($this->config['tautulliURL'], $this->config['homepageTautulliRefresh'], $this->config['tautulliDisableCertCheck'], $this->config['tautulliUseCustomCertificate']);
  81. $homestats = Requests::get($homestatsUrl, [], $options);
  82. if ($homestats->success) {
  83. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  84. return true;
  85. } else {
  86. $this->setAPIResponse('error', 'Tautulli Error Occurred - Check URL or Credentials', 409);
  87. return false;
  88. }
  89. } catch (Requests_Exception $e) {
  90. $this->critical($e, [$url]);
  91. $this->setAPIResponse('error', $e->getMessage(), 500);
  92. return false;
  93. }
  94. }
  95. public function tautulliHomepagePermissions($key = null)
  96. {
  97. $permissions = [
  98. 'main' => [
  99. 'enabled' => [
  100. 'homepageTautulliEnabled'
  101. ],
  102. 'auth' => [
  103. 'homepageTautulliAuth'
  104. ],
  105. 'not_empty' => [
  106. 'tautulliURL',
  107. 'tautulliApikey'
  108. ]
  109. ]
  110. ];
  111. return $this->homepageCheckKeyPermissions($key, $permissions);
  112. }
  113. public function homepageOrdertautulli()
  114. {
  115. if ($this->homepageItemPermissions($this->tautulliHomepagePermissions('main'))) {
  116. return '
  117. <div id="' . __FUNCTION__ . '">
  118. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Tautulli...</h2></div>
  119. <script>
  120. // Tautulli
  121. homepageTautulli("' . $this->config['homepageTautulliRefresh'] . '");
  122. // End Tautulli
  123. </script>
  124. </div>
  125. ';
  126. }
  127. }
  128. public function getTautulliHomepageData()
  129. {
  130. $this->setLoggerChannel('Tautulli Homepage');
  131. if (!$this->homepageItemPermissions($this->tautulliHomepagePermissions('main'), true)) {
  132. return false;
  133. }
  134. $api = [];
  135. $url = $this->qualifyURL($this->config['tautulliURL']);
  136. $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
  137. $height = $this->getCacheImageSize('h');
  138. $width = $this->getCacheImageSize('w');
  139. $nowPlayingHeight = $this->getCacheImageSize('nph');
  140. $nowPlayingWidth = $this->getCacheImageSize('npw');
  141. try {
  142. $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
  143. $options = $this->requestOptions($this->config['tautulliURL'], $this->config['homepageTautulliRefresh'], $this->config['tautulliDisableCertCheck'], $this->config['tautulliUseCustomCertificate']);
  144. $homestats = Requests::get($homestatsUrl, [], $options);
  145. if ($homestats->success) {
  146. $homestats = json_decode($homestats->body, true);
  147. $api['homestats'] = $homestats['response'];
  148. // Cache art & thumb for first result in each tautulli API result
  149. $categories = ['top_movies', 'top_tv', 'popular_movies', 'popular_tv'];
  150. foreach ($categories as $cat) {
  151. $key = array_search($cat, array_column($api['homestats']['data'], 'stat_id'));
  152. $img = $api['homestats']['data'][$key]['rows'][0];
  153. $this->cacheImage($url . '/pms_image_proxy?img=' . $img['art'] . '&rating_key=' . $img['rating_key'] . '&width=' . $nowPlayingWidth . '&height=' . $nowPlayingHeight, $img['rating_key'] . '-np');
  154. $this->cacheImage($url . '/pms_image_proxy?img=' . $img['thumb'] . '&rating_key=' . $img['rating_key'] . '&width=' . $width . '&height=' . $height, $img['rating_key'] . '-list');
  155. $img['art'] = 'plugins/images/cache/' . $img['rating_key'] . '-np.jpg';
  156. $img['thumb'] = 'plugins/images/cache/' . $img['rating_key'] . '-list.jpg';
  157. $api['homestats']['data'][$key]['rows'][0] = $img;
  158. }
  159. // Cache the platform icon
  160. $key = array_search('top_platforms', array_column($api['homestats']['data'], 'stat_id'));
  161. $platform = $api['homestats']['data'][$key]['rows'][0]['platform_name'];
  162. $this->cacheImage($url . '/images/platforms/' . $platform . '.svg', 'tautulli-' . $platform, 'svg');
  163. }
  164. $libstatsUrl = $apiURL . '&cmd=get_libraries_table';
  165. $options = $this->requestOptions($this->config['tautulliURL'], $this->config['homepageTautulliRefresh'], $this->config['tautulliDisableCertCheck'], $this->config['tautulliUseCustomCertificate']);
  166. $libstats = Requests::get($libstatsUrl, [], $options);
  167. if ($libstats->success) {
  168. $libstats = json_decode($libstats->body, true);
  169. $api['libstats'] = $libstats['response']['data'];
  170. $categories = ['movie.svg', 'show.svg', 'artist.svg'];
  171. foreach ($categories as $cat) {
  172. $parts = explode('.', $cat);
  173. $this->cacheImage($url . '/images/libraries/' . $cat, 'tautulli-' . $parts[0], $parts[1]);
  174. }
  175. }
  176. $api['options'] = [
  177. 'url' => $url,
  178. 'libraries' => $this->config['tautulliLibraries'],
  179. 'topMovies' => $this->config['tautulliTopMovies'],
  180. 'topTV' => $this->config['tautulliTopTV'],
  181. 'topUsers' => $this->config['tautulliTopUsers'],
  182. 'topPlatforms' => $this->config['tautulliTopPlatforms'],
  183. 'popularMovies' => $this->config['tautulliPopularMovies'],
  184. 'popularTV' => $this->config['tautulliPopularTV'],
  185. 'title' => $this->config['tautulliHeaderToggle'],
  186. 'friendlyName' => $this->config['tautulliFriendlyName'],
  187. ];
  188. $ids = []; // Array of stat_ids to remove from the returned array
  189. if (!$this->qualifyRequest($this->config['homepageTautulliLibraryAuth'])) {
  190. $api['options']['libraries'] = false;
  191. unset($api['libstats']);
  192. }
  193. if (!$this->qualifyRequest($this->config['homepageTautulliViewsAuth'])) {
  194. $api['options']['topMovies'] = false;
  195. $api['options']['topTV'] = false;
  196. $api['options']['popularMovies'] = false;
  197. $api['options']['popularTV'] = false;
  198. $ids = array_merge(['top_movies', 'popular_movies', 'popular_tv', 'top_tv'], $ids);
  199. $api['homestats']['data'] = array_values($api['homestats']['data']);
  200. }
  201. if (!$this->qualifyRequest($this->config['homepageTautulliMiscAuth'])) {
  202. $api['options']['topUsers'] = false;
  203. $api['options']['topPlatforms'] = false;
  204. $ids = array_merge(['top_platforms', 'top_users'], $ids);
  205. $api['homestats']['data'] = array_values($api['homestats']['data']);
  206. }
  207. $ids = array_merge(['top_music', 'popular_music', 'last_watched', 'most_concurrent'], $ids);
  208. foreach ($ids as $id) {
  209. if ($key = array_search($id, array_column($api['homestats']['data'], 'stat_id'))) {
  210. unset($api['homestats']['data'][$key]);
  211. $api['homestats']['data'] = array_values($api['homestats']['data']);
  212. }
  213. }
  214. } catch (Requests_Exception $e) {
  215. $this->critical($e, [$url]);
  216. $this->setAPIResponse('error', $e->getMessage(), 500);
  217. return false;
  218. };
  219. $api = isset($api) ? $api : false;
  220. $this->setAPIResponse('success', null, 200, $api);
  221. return $api;
  222. }
  223. }