tautulli.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. <?php
  2. trait TautulliHomepageItem
  3. {
  4. public function tautulliSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'Tautulli',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/tautulli.png',
  10. 'category' => 'Monitor',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageTautulliEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageTautulliEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageTautulliAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageTautulliAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Options' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'tautulliHeader',
  31. 'label' => 'Title',
  32. 'value' => $this->config['tautulliHeader'],
  33. 'help' => 'Sets the title of this homepage module'
  34. ),
  35. array(
  36. 'type' => 'switch',
  37. 'name' => 'tautulliHeaderToggle',
  38. 'label' => 'Toggle Title',
  39. 'value' => $this->config['tautulliHeaderToggle'],
  40. 'help' => 'Shows/hides the title of this homepage module'
  41. )
  42. ),
  43. 'Connection' => array(
  44. array(
  45. 'type' => 'input',
  46. 'name' => 'tautulliURL',
  47. 'label' => 'URL',
  48. 'value' => $this->config['tautulliURL'],
  49. 'help' => 'URL for Tautulli API, include the IP, the port and the base URL (e.g. /tautulli/) in the URL',
  50. 'placeholder' => 'http://<ip>:<port>'
  51. ),
  52. array(
  53. 'type' => 'password-alt',
  54. 'name' => 'tautulliApikey',
  55. 'label' => 'API Key',
  56. 'value' => $this->config['tautulliApikey']
  57. ),
  58. array(
  59. 'type' => 'select',
  60. 'name' => 'homepageTautulliRefresh',
  61. 'label' => 'Refresh Seconds',
  62. 'value' => $this->config['homepageTautulliRefresh'],
  63. 'options' => $this->timeOptions()
  64. ),
  65. ),
  66. 'Library Stats' => array(
  67. array(
  68. 'type' => 'switch',
  69. 'name' => 'tautulliLibraries',
  70. 'label' => 'Libraries',
  71. 'value' => $this->config['tautulliLibraries'],
  72. 'help' => 'Shows/hides the card with library information.',
  73. ),
  74. array(
  75. 'type' => 'select',
  76. 'name' => 'homepageTautulliLibraryAuth',
  77. 'label' => 'Minimum Authentication',
  78. 'value' => $this->config['homepageTautulliLibraryAuth'],
  79. 'options' => $this->groupOptions
  80. ),
  81. ),
  82. 'Viewing Stats' => array(
  83. array(
  84. 'type' => 'switch',
  85. 'name' => 'tautulliPopularMovies',
  86. 'label' => 'Popular Movies',
  87. 'value' => $this->config['tautulliPopularMovies'],
  88. 'help' => 'Shows/hides the card with Popular Movies information.',
  89. ),
  90. array(
  91. 'type' => 'switch',
  92. 'name' => 'tautulliPopularTV',
  93. 'label' => 'Popular TV',
  94. 'value' => $this->config['tautulliPopularTV'],
  95. 'help' => 'Shows/hides the card with Popular TV information.',
  96. ),
  97. array(
  98. 'type' => 'switch',
  99. 'name' => 'tautulliTopMovies',
  100. 'label' => 'Top Movies',
  101. 'value' => $this->config['tautulliTopMovies'],
  102. 'help' => 'Shows/hides the card with Top Movies information.',
  103. ),
  104. array(
  105. 'type' => 'switch',
  106. 'name' => 'tautulliTopTV',
  107. 'label' => 'Top TV',
  108. 'value' => $this->config['tautulliTopTV'],
  109. 'help' => 'Shows/hides the card with Top TV information.',
  110. ),
  111. array(
  112. 'type' => 'select',
  113. 'name' => 'homepageTautulliViewsAuth',
  114. 'label' => 'Minimum Authentication',
  115. 'value' => $this->config['homepageTautulliViewsAuth'],
  116. 'options' => $this->groupOptions
  117. ),
  118. ),
  119. 'Misc Stats' => array(
  120. array(
  121. 'type' => 'switch',
  122. 'name' => 'tautulliTopUsers',
  123. 'label' => 'Top Users',
  124. 'value' => $this->config['tautulliTopUsers'],
  125. 'help' => 'Shows/hides the card with Top Users information.',
  126. ),
  127. array(
  128. 'type' => 'switch',
  129. 'name' => 'tautulliTopPlatforms',
  130. 'label' => 'Top Platforms',
  131. 'value' => $this->config['tautulliTopPlatforms'],
  132. 'help' => 'Shows/hides the card with Top Platforms information.',
  133. ),
  134. array(
  135. 'type' => 'select',
  136. 'name' => 'homepageTautulliMiscAuth',
  137. 'label' => 'Minimum Authentication',
  138. 'value' => $this->config['homepageTautulliMiscAuth'],
  139. 'options' => $this->groupOptions
  140. ),
  141. array(
  142. 'type' => 'switch',
  143. 'name' => 'tautulliFriendlyName',
  144. 'label' => 'Use Friendly Name',
  145. 'value' => $this->config['tautulliFriendlyName'],
  146. 'help' => 'Use the friendly name set in tautulli for users.',
  147. ),
  148. ),
  149. 'Test Connection' => array(
  150. array(
  151. 'type' => 'blank',
  152. 'label' => 'Please Save before Testing'
  153. ),
  154. array(
  155. 'type' => 'button',
  156. 'label' => '',
  157. 'icon' => 'fa fa-flask',
  158. 'class' => 'pull-right',
  159. 'text' => 'Test Connection',
  160. 'attr' => 'onclick="testAPIConnection(\'tautulli\')"'
  161. ),
  162. )
  163. )
  164. );
  165. }
  166. public function testConnectionTautulli()
  167. {
  168. if (empty($this->config['tautulliURL'])) {
  169. $this->setAPIResponse('error', 'Tautulli URL is not defined', 422);
  170. return false;
  171. }
  172. if (empty($this->config['tautulliApikey'])) {
  173. $this->setAPIResponse('error', 'Tautulli Token is not defined', 422);
  174. return false;
  175. }
  176. $url = $this->qualifyURL($this->config['tautulliURL']);
  177. $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
  178. try {
  179. $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
  180. $homestats = Requests::get($homestatsUrl, [], []);
  181. if ($homestats->success) {
  182. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  183. return true;
  184. } else {
  185. $this->setAPIResponse('error', 'Tautulli Error Occurred - Check URL or Credentials', 409);
  186. return false;
  187. }
  188. } catch (Requests_Exception $e) {
  189. $this->writeLog('error', 'Tautulli Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  190. $this->setAPIResponse('error', $e->getMessage(), 500);
  191. return false;
  192. }
  193. }
  194. public function tautulliHomepagePermissions($key = null)
  195. {
  196. $permissions = [
  197. 'main' => [
  198. 'enabled' => [
  199. 'homepageTautulliEnabled'
  200. ],
  201. 'auth' => [
  202. 'homepageTautulliAuth'
  203. ],
  204. 'not_empty' => [
  205. 'tautulliURL',
  206. 'tautulliApikey'
  207. ]
  208. ]
  209. ];
  210. if (array_key_exists($key, $permissions)) {
  211. return $permissions[$key];
  212. } elseif ($key == 'all') {
  213. return $permissions;
  214. } else {
  215. return [];
  216. }
  217. }
  218. public function homepageOrdertautulli()
  219. {
  220. if ($this->homepageItemPermissions($this->tautulliHomepagePermissions('main'))) {
  221. return '
  222. <div id="' . __FUNCTION__ . '">
  223. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Tautulli...</h2></div>
  224. <script>
  225. // Tautulli
  226. homepageTautulli("' . $this->config['homepageTautulliRefresh'] . '");
  227. // End Tautulli
  228. </script>
  229. </div>
  230. ';
  231. }
  232. }
  233. public function getTautulliHomepageData()
  234. {
  235. if (!$this->homepageItemPermissions($this->tautulliHomepagePermissions('main'), true)) {
  236. return false;
  237. }
  238. $api = [];
  239. $url = $this->qualifyURL($this->config['tautulliURL']);
  240. $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
  241. $height = $this->getCacheImageSize('h');
  242. $width = $this->getCacheImageSize('w');
  243. $nowPlayingHeight = $this->getCacheImageSize('nph');
  244. $nowPlayingWidth = $this->getCacheImageSize('npw');
  245. try {
  246. $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
  247. $homestats = Requests::get($homestatsUrl, [], []);
  248. if ($homestats->success) {
  249. $homestats = json_decode($homestats->body, true);
  250. $api['homestats'] = $homestats['response'];
  251. // Cache art & thumb for first result in each tautulli API result
  252. $categories = ['top_movies', 'top_tv', 'popular_movies', 'popular_tv'];
  253. foreach ($categories as $cat) {
  254. $key = array_search($cat, array_column($api['homestats']['data'], 'stat_id'));
  255. $img = $api['homestats']['data'][$key]['rows'][0];
  256. $this->cacheImage($url . '/pms_image_proxy?img=' . $img['art'] . '&rating_key=' . $img['rating_key'] . '&width=' . $nowPlayingWidth . '&height=' . $nowPlayingHeight, $img['rating_key'] . '-np');
  257. $this->cacheImage($url . '/pms_image_proxy?img=' . $img['thumb'] . '&rating_key=' . $img['rating_key'] . '&width=' . $width . '&height=' . $height, $img['rating_key'] . '-list');
  258. $img['art'] = 'plugins/images/cache/' . $img['rating_key'] . '-np.jpg';
  259. $img['thumb'] = 'plugins/images/cache/' . $img['rating_key'] . '-list.jpg';
  260. $api['homestats']['data'][$key]['rows'][0] = $img;
  261. }
  262. // Cache the platform icon
  263. $key = array_search('top_platforms', array_column($api['homestats']['data'], 'stat_id'));
  264. $platform = $api['homestats']['data'][$key]['rows'][0]['platform_name'];
  265. $this->cacheImage($url . '/images/platforms/' . $platform . '.svg', 'tautulli-' . $platform, 'svg');
  266. }
  267. $libstatsUrl = $apiURL . '&cmd=get_libraries';
  268. $libstats = Requests::get($libstatsUrl, [], []);
  269. if ($libstats->success) {
  270. $libstats = json_decode($libstats->body, true);
  271. $api['libstats'] = $libstats['response'];
  272. $categories = ['movie.svg', 'show.svg', 'artist.svg'];
  273. foreach ($categories as $cat) {
  274. $parts = explode('.', $cat);
  275. $this->cacheImage($url . '/images/libraries/' . $cat, 'tautulli-' . $parts[0], $parts[1]);
  276. }
  277. }
  278. $api['options'] = [
  279. 'url' => $url,
  280. 'libraries' => $this->config['tautulliLibraries'],
  281. 'topMovies' => $this->config['tautulliTopMovies'],
  282. 'topTV' => $this->config['tautulliTopTV'],
  283. 'topUsers' => $this->config['tautulliTopUsers'],
  284. 'topPlatforms' => $this->config['tautulliTopPlatforms'],
  285. 'popularMovies' => $this->config['tautulliPopularMovies'],
  286. 'popularTV' => $this->config['tautulliPopularTV'],
  287. 'title' => $this->config['tautulliHeaderToggle'],
  288. 'friendlyName' => $this->config['tautulliFriendlyName'],
  289. ];
  290. $ids = []; // Array of stat_ids to remove from the returned array
  291. if (!$this->qualifyRequest($this->config['homepageTautulliLibraryAuth'])) {
  292. $api['options']['libraries'] = false;
  293. unset($api['libstats']);
  294. }
  295. if (!$this->qualifyRequest($this->config['homepageTautulliViewsAuth'])) {
  296. $api['options']['topMovies'] = false;
  297. $api['options']['topTV'] = false;
  298. $api['options']['popularMovies'] = false;
  299. $api['options']['popularTV'] = false;
  300. $ids = array_merge(['top_movies', 'popular_movies', 'popular_tv', 'top_tv'], $ids);
  301. $api['homestats']['data'] = array_values($api['homestats']['data']);
  302. }
  303. if (!$this->qualifyRequest($this->config['homepageTautulliMiscAuth'])) {
  304. $api['options']['topUsers'] = false;
  305. $api['options']['topPlatforms'] = false;
  306. $ids = array_merge(['top_platforms', 'top_users'], $ids);
  307. $api['homestats']['data'] = array_values($api['homestats']['data']);
  308. }
  309. $ids = array_merge(['top_music', 'popular_music', 'last_watched', 'most_concurrent'], $ids);
  310. foreach ($ids as $id) {
  311. if ($key = array_search($id, array_column($api['homestats']['data'], 'stat_id'))) {
  312. unset($api['homestats']['data'][$key]);
  313. $api['homestats']['data'] = array_values($api['homestats']['data']);
  314. }
  315. }
  316. } catch (Requests_Exception $e) {
  317. $this->writeLog('error', 'Tautulli Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  318. $this->setAPIResponse('error', $e->getMessage(), 500);
  319. return false;
  320. };
  321. $api = isset($api) ? $api : false;
  322. $this->setAPIResponse('success', null, 200, $api);
  323. return $api;
  324. }
  325. }