tautulli.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. $libraryList = [['name' => 'Refresh page to update List', 'value' => '', 'disabled' => true]];
  17. if (!empty($this->config['tautulliApikey']) && !empty($this->config['tautulliURL'])) {
  18. $libraryList = [];
  19. $loop = $this->tautulliLibraryList();
  20. if ($loop) {
  21. $loop = $loop['libraries'];
  22. foreach ($loop as $key => $value) {
  23. $libraryList[] = ['name' => $key, 'value' => $value];
  24. }
  25. }
  26. }
  27. $homepageSettings = [
  28. 'debug' => true,
  29. 'settings' => [
  30. 'Enable' => [
  31. $this->settingsOption('enable', 'homepageTautulliEnabled'),
  32. $this->settingsOption('auth', 'homepageTautulliAuth'),
  33. ],
  34. 'Options' => [
  35. $this->settingsOption('title', 'tautulliHeader'),
  36. $this->settingsOption('toggle-title', 'tautulliHeaderToggle'),
  37. $this->settingsOption('refresh', 'homepageTautulliRefresh'),
  38. ],
  39. 'Connection' => [
  40. $this->settingsOption('multiple-url', 'tautulliURL'),
  41. $this->settingsOption('multiple-api-key', 'tautulliApikey'),
  42. $this->settingsOption('disable-cert-check', 'tautulliDisableCertCheck'),
  43. $this->settingsOption('use-custom-certificate', 'tautulliUseCustomCertificate'),
  44. ],
  45. 'API SOCKS' => [
  46. $this->settingsOption('socks', 'tautulli'),
  47. $this->settingsOption('blank'),
  48. $this->settingsOption('enable', 'tautulliSocksEnabled'),
  49. $this->settingsOption('auth', 'tautulliSocksAuth'),
  50. ],
  51. 'Library Stats' => [
  52. $this->settingsOption('switch', 'tautulliLibraries', ['label' => 'Libraries', 'help' => 'Shows/hides the card with library information.']),
  53. $this->settingsOption('auth', 'homepageTautulliLibraryAuth'),
  54. $this->settingsOption('plex-library-exclude', 'homepageTautulliLibraryStatsExclude', ['options' => $libraryList]),
  55. ],
  56. 'Viewing Stats' => [
  57. $this->settingsOption('switch', 'tautulliPopularMovies', ['label' => 'Popular Movies', 'help' => 'Shows/hides the card with Popular Movie information.']),
  58. $this->settingsOption('switch', 'tautulliPopularTV', ['label' => 'Popular TV', 'help' => 'Shows/hides the card with Popular TV information.']),
  59. $this->settingsOption('switch', 'tautulliTopMovies', ['label' => 'Top Movies', 'help' => 'Shows/hides the card with Top Movies information.']),
  60. $this->settingsOption('switch', 'tautulliTopTV', ['label' => 'Top TV', 'help' => 'Shows/hides the card with Top TV information.']),
  61. $this->settingsOption('auth', 'homepageTautulliViewsAuth'),
  62. $this->settingsOption('plex-library-exclude', 'homepageTautulliViewingStatsExclude', ['options' => $libraryList]),
  63. ],
  64. 'Misc Stats' => [
  65. $this->settingsOption('switch', 'tautulliTopUsers', ['label' => 'Top Users', 'help' => 'Shows/hides the card with Top Users information.']),
  66. $this->settingsOption('switch', 'tautulliTopPlatforms', ['label' => 'Top Platforms', 'help' => 'Shows/hides the card with Top Platforms information.']),
  67. $this->settingsOption('auth', 'homepageTautulliMiscAuth'),
  68. $this->settingsOption('switch', 'tautulliFriendlyName', ['label' => 'Use Friendly Name', 'help' => 'Use the friendly name set in tautulli for users.']),
  69. ],
  70. 'Test Connection' => [
  71. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  72. $this->settingsOption('test', 'tautulli'),
  73. ]
  74. ]
  75. ];
  76. return array_merge($homepageInformation, $homepageSettings);
  77. }
  78. public function testConnectionTautulli()
  79. {
  80. $this->setLoggerChannel('Tautulli Homepage');
  81. if (empty($this->config['tautulliURL'])) {
  82. $this->setAPIResponse('error', 'Tautulli URL is not defined', 422);
  83. return false;
  84. }
  85. if (empty($this->config['tautulliApikey'])) {
  86. $this->setAPIResponse('error', 'Tautulli Token is not defined', 422);
  87. return false;
  88. }
  89. $url = $this->qualifyURL($this->config['tautulliURL']);
  90. $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
  91. try {
  92. $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
  93. $options = $this->requestOptions($this->config['tautulliURL'], $this->config['homepageTautulliRefresh'], $this->config['tautulliDisableCertCheck'], $this->config['tautulliUseCustomCertificate'], ['follow_redirects' => false]);
  94. $homestats = Requests::get($homestatsUrl, [], $options);
  95. if ($homestats->success) {
  96. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  97. return true;
  98. } else {
  99. $this->setAPIResponse('error', 'Tautulli Error Occurred - Check URL or Credentials', 409);
  100. return false;
  101. }
  102. } catch (Requests_Exception $e) {
  103. $this->logger->critical($e, [$url]);
  104. $this->setResponse(500, $e->getMessage());
  105. return false;
  106. }
  107. }
  108. public function tautulliHomepagePermissions($key = null)
  109. {
  110. $permissions = [
  111. 'main' => [
  112. 'enabled' => [
  113. 'homepageTautulliEnabled'
  114. ],
  115. 'auth' => [
  116. 'homepageTautulliAuth'
  117. ],
  118. 'not_empty' => [
  119. 'tautulliURL',
  120. 'tautulliApikey'
  121. ]
  122. ]
  123. ];
  124. return $this->homepageCheckKeyPermissions($key, $permissions);
  125. }
  126. public function homepageOrdertautulli()
  127. {
  128. if ($this->homepageItemPermissions($this->tautulliHomepagePermissions('main'))) {
  129. return '
  130. <div id="' . __FUNCTION__ . '">
  131. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Tautulli...</h2></div>
  132. <script>
  133. // Tautulli
  134. homepageTautulli("' . $this->config['homepageTautulliRefresh'] . '");
  135. // End Tautulli
  136. </script>
  137. </div>
  138. ';
  139. }
  140. }
  141. public function getTautulliHomepageData()
  142. {
  143. $this->setLoggerChannel('Tautulli Homepage');
  144. if (!$this->homepageItemPermissions($this->tautulliHomepagePermissions('main'), true)) {
  145. return false;
  146. }
  147. $api = [];
  148. $url = $this->qualifyURL($this->config['tautulliURL']);
  149. $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
  150. $height = $this->getCacheImageSize('h');
  151. $width = $this->getCacheImageSize('w');
  152. $nowPlayingHeight = $this->getCacheImageSize('nph');
  153. $nowPlayingWidth = $this->getCacheImageSize('npw');
  154. $api['options'] = [
  155. 'url' => $url,
  156. 'libraries' => $this->config['tautulliLibraries'],
  157. 'topMovies' => $this->config['tautulliTopMovies'],
  158. 'topTV' => $this->config['tautulliTopTV'],
  159. 'topUsers' => $this->config['tautulliTopUsers'],
  160. 'topPlatforms' => $this->config['tautulliTopPlatforms'],
  161. 'popularMovies' => $this->config['tautulliPopularMovies'],
  162. 'popularTV' => $this->config['tautulliPopularTV'],
  163. 'title' => $this->config['tautulliHeaderToggle'],
  164. 'friendlyName' => $this->config['tautulliFriendlyName'],
  165. ];
  166. try {
  167. $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
  168. $options = $this->requestOptions($this->config['tautulliURL'], $this->config['homepageTautulliRefresh'], $this->config['tautulliDisableCertCheck'], $this->config['tautulliUseCustomCertificate']);
  169. $homestats = Requests::get($homestatsUrl, [], $options);
  170. if ($homestats->success) {
  171. $homepageTautulliViewingStatsExclude = explode(",", $this->config['homepageTautulliViewingStatsExclude']);
  172. $homestats = json_decode($homestats->body, true);
  173. foreach ($homestats['response']['data'] as $s => $stats) {
  174. foreach ($stats['rows'] as $i => $v) {
  175. if (array_key_exists('section_id', $v)) {
  176. if (in_array($v['section_id'], $homepageTautulliViewingStatsExclude)) {
  177. unset($homestats['response']['data'][$s]['rows'][$i]);
  178. }
  179. }
  180. }
  181. }
  182. $homestats['response']['data'] = array_values($homestats['response']['data']);
  183. $api['homestats'] = $homestats['response'];
  184. // Cache art & thumb for first result in each tautulli API result
  185. $categories = ['top_movies', 'top_tv', 'popular_movies', 'popular_tv'];
  186. foreach ($categories as $cat) {
  187. $key = array_search($cat, array_column($api['homestats']['data'], 'stat_id'));
  188. if (count($api['homestats']['data'][$key]['rows']) > 0) {
  189. $img = $api['homestats']['data'][$key]['rows'][0];
  190. $this->cacheImage($url . '/pms_image_proxy?img=' . $img['art'] . '&rating_key=' . $img['rating_key'] . '&width=' . $nowPlayingWidth . '&height=' . $nowPlayingHeight, $img['rating_key'] . '-np');
  191. $this->cacheImage($url . '/pms_image_proxy?img=' . $img['thumb'] . '&rating_key=' . $img['rating_key'] . '&width=' . $width . '&height=' . $height, $img['rating_key'] . '-list');
  192. $img['art'] = 'data/cache/' . $img['rating_key'] . '-np.jpg';
  193. $img['thumb'] = 'data/cache/' . $img['rating_key'] . '-list.jpg';
  194. $api['homestats']['data'][$key]['rows'][0] = $img;
  195. }
  196. }
  197. // Cache the platform icon
  198. if (count($api['homestats']['data'][$key]['rows']) > 0) {
  199. $key = array_search('top_platforms', array_column($api['homestats']['data'], 'stat_id'));
  200. $platform = $api['homestats']['data'][$key]['rows'][0]['platform_name'];
  201. $this->cacheImage($url . '/images/platforms/' . $platform . '.svg', 'tautulli-' . $platform, 'svg');
  202. }
  203. $libstatsUrl = $apiURL . '&cmd=get_libraries_table';
  204. $options = $this->requestOptions($this->config['tautulliURL'], $this->config['homepageTautulliRefresh'], $this->config['tautulliDisableCertCheck'], $this->config['tautulliUseCustomCertificate']);
  205. $libstats = Requests::get($libstatsUrl, [], $options);
  206. if ($libstats->success) {
  207. $homepageTautulliLibraryStatsExclude = explode(',', $this->config['homepageTautulliLibraryStatsExclude']);
  208. $libstats = json_decode($libstats->body, true);
  209. foreach ($libstats['response']['data']['data'] as $i => $v) {
  210. if (array_key_exists('section_id', $v)) {
  211. if (in_array($v['section_id'], $homepageTautulliLibraryStatsExclude)) {
  212. unset($libstats['response']['data']['data'][$i]);
  213. }
  214. }
  215. }
  216. $libstats['response']['data']['data'] = array_values($libstats['response']['data']['data']);
  217. $api['libstats'] = $libstats['response']['data'];
  218. $categories = ['movie.svg', 'show.svg', 'artist.svg'];
  219. foreach ($categories as $cat) {
  220. $parts = explode('.', $cat);
  221. $this->cacheImage($url . '/images/libraries/' . $cat, 'tautulli-' . $parts[0], $parts[1]);
  222. }
  223. }
  224. $ids = []; // Array of stat_ids to remove from the returned array
  225. if (!$this->qualifyRequest($this->config['homepageTautulliLibraryAuth'])) {
  226. $api['options']['libraries'] = false;
  227. unset($api['libstats']);
  228. }
  229. if (!$this->qualifyRequest($this->config['homepageTautulliViewsAuth'])) {
  230. $api['options']['topMovies'] = false;
  231. $api['options']['topTV'] = false;
  232. $api['options']['popularMovies'] = false;
  233. $api['options']['popularTV'] = false;
  234. $ids = array_merge(['top_movies', 'popular_movies', 'popular_tv', 'top_tv'], $ids);
  235. $api['homestats']['data'] = array_values($api['homestats']['data']);
  236. }
  237. if (!$this->qualifyRequest($this->config['homepageTautulliMiscAuth'])) {
  238. $api['options']['topUsers'] = false;
  239. $api['options']['topPlatforms'] = false;
  240. $ids = array_merge(['top_platforms', 'top_users'], $ids);
  241. $api['homestats']['data'] = array_values($api['homestats']['data']);
  242. }
  243. $ids = array_merge(['top_music', 'popular_music', 'last_watched', 'most_concurrent'], $ids);
  244. foreach ($ids as $id) {
  245. if ($key = array_search($id, array_column($api['homestats']['data'], 'stat_id'))) {
  246. unset($api['homestats']['data'][$key]);
  247. $api['homestats']['data'] = array_values($api['homestats']['data']);
  248. }
  249. }
  250. }
  251. } catch (Requests_Exception $e) {
  252. $this->logger->critical($e, [$url]);
  253. $this->setResponse(500, $e->getMessage());
  254. return false;
  255. }
  256. $api = $api ?? false;
  257. $this->setAPIResponse('success', null, 200, $api);
  258. return $api;
  259. }
  260. public function tautulliLibraryList()
  261. {
  262. $url = $this->qualifyURL($this->config['tautulliURL']);
  263. $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
  264. if (!empty($this->config['tautulliApikey']) && !empty($this->config['tautulliURL'])) {
  265. $liblistUrl = $apiURL . '&cmd=get_libraries';
  266. $options = $this->requestOptions($this->config['tautulliURL'], 10, $this->config['tautulliDisableCertCheck'], $this->config['tautulliUseCustomCertificate']);
  267. try {
  268. $liblist = Requests::get($liblistUrl, [], $options);
  269. $libraryList = array();
  270. if ($liblist->success) {
  271. $liblist = json_decode($liblist->body, true);
  272. foreach ($liblist['response']['data'] as $lib) {
  273. $libraryList['libraries'][(string)$lib['section_name']] = (string)$lib["section_id"];
  274. }
  275. $libraryList = array_change_key_case($libraryList, CASE_LOWER);
  276. return $libraryList;
  277. }
  278. } catch (Requests_Exception $e) {
  279. $this->setLoggerChannel('Tautulli')->error($e);
  280. return false;
  281. }
  282. }
  283. return false;
  284. }
  285. }