tautulli.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. ),
  142. 'Test Connection' => array(
  143. array(
  144. 'type' => 'blank',
  145. 'label' => 'Please Save before Testing'
  146. ),
  147. array(
  148. 'type' => 'button',
  149. 'label' => '',
  150. 'icon' => 'fa fa-flask',
  151. 'class' => 'pull-right',
  152. 'text' => 'Test Connection',
  153. 'attr' => 'onclick="testAPIConnection(\'tautulli\')"'
  154. ),
  155. )
  156. )
  157. );
  158. }
  159. public function testConnectionTautulli()
  160. {
  161. if (empty($this->config['tautulliURL'])) {
  162. $this->setAPIResponse('error', 'Tautulli URL is not defined', 422);
  163. return false;
  164. }
  165. if (empty($this->config['tautulliApikey'])) {
  166. $this->setAPIResponse('error', 'Tautulli Token is not defined', 422);
  167. return false;
  168. }
  169. $url = $this->qualifyURL($this->config['tautulliURL']);
  170. $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
  171. try {
  172. $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
  173. $homestats = Requests::get($homestatsUrl, [], []);
  174. if ($homestats->success) {
  175. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  176. return true;
  177. } else {
  178. $this->setAPIResponse('error', 'Tautulli Error Occurred - Check URL or Credentials', 409);
  179. return false;
  180. }
  181. } catch (Requests_Exception $e) {
  182. $this->writeLog('error', 'Tautulli Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  183. $this->setAPIResponse('error', $e->getMessage(), 500);
  184. return false;
  185. }
  186. }
  187. public function tautulliHomepagePermissions($key = null)
  188. {
  189. $permissions = [
  190. 'main' => [
  191. 'enabled' => [
  192. 'homepageTautulliEnabled'
  193. ],
  194. 'auth' => [
  195. 'homepageTautulliAuth'
  196. ],
  197. 'not_empty' => [
  198. 'tautulliURL',
  199. 'tautulliApikey'
  200. ]
  201. ]
  202. ];
  203. if (array_key_exists($key, $permissions)) {
  204. return $permissions[$key];
  205. } elseif ($key == 'all') {
  206. return $permissions;
  207. } else {
  208. return [];
  209. }
  210. }
  211. public function homepageOrdertautulli()
  212. {
  213. if ($this->homepageItemPermissions($this->tautulliHomepagePermissions('main'))) {
  214. return '
  215. <div id="' . __FUNCTION__ . '">
  216. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Tautulli...</h2></div>
  217. <script>
  218. // Tautulli
  219. homepageTautulli("' . $this->config['homepageTautulliRefresh'] . '");
  220. // End Tautulli
  221. </script>
  222. </div>
  223. ';
  224. }
  225. }
  226. public function getTautulliHomepageData()
  227. {
  228. if (!$this->homepageItemPermissions($this->tautulliHomepagePermissions('main'), true)) {
  229. return false;
  230. }
  231. $api = [];
  232. $url = $this->qualifyURL($this->config['tautulliURL']);
  233. $apiURL = $url . '/api/v2?apikey=' . $this->config['tautulliApikey'];
  234. $height = $this->getCacheImageSize('h');
  235. $width = $this->getCacheImageSize('w');
  236. $nowPlayingHeight = $this->getCacheImageSize('nph');
  237. $nowPlayingWidth = $this->getCacheImageSize('npw');
  238. try {
  239. $homestatsUrl = $apiURL . '&cmd=get_home_stats&grouping=1';
  240. $homestats = Requests::get($homestatsUrl, [], []);
  241. if ($homestats->success) {
  242. $homestats = json_decode($homestats->body, true);
  243. $api['homestats'] = $homestats['response'];
  244. // Cache art & thumb for first result in each tautulli API result
  245. $categories = ['top_movies', 'top_tv', 'popular_movies', 'popular_tv'];
  246. foreach ($categories as $cat) {
  247. $key = array_search($cat, array_column($api['homestats']['data'], 'stat_id'));
  248. $img = $api['homestats']['data'][$key]['rows'][0];
  249. $this->cacheImage($url . '/pms_image_proxy?img=' . $img['art'] . '&rating_key=' . $img['rating_key'] . '&width=' . $nowPlayingWidth . '&height=' . $nowPlayingHeight, $img['rating_key'] . '-np');
  250. $this->cacheImage($url . '/pms_image_proxy?img=' . $img['thumb'] . '&rating_key=' . $img['rating_key'] . '&width=' . $width . '&height=' . $height, $img['rating_key'] . '-list');
  251. $img['art'] = 'plugins/images/cache/' . $img['rating_key'] . '-np.jpg';
  252. $img['thumb'] = 'plugins/images/cache/' . $img['rating_key'] . '-list.jpg';
  253. $api['homestats']['data'][$key]['rows'][0] = $img;
  254. }
  255. // Cache the platform icon
  256. $key = array_search('top_platforms', array_column($api['homestats']['data'], 'stat_id'));
  257. $platform = $api['homestats']['data'][$key]['rows'][0]['platform_name'];
  258. $this->cacheImage($url . '/images/platforms/' . $platform . '.svg', 'tautulli-' . $platform, 'svg');
  259. }
  260. $libstatsUrl = $apiURL . '&cmd=get_libraries';
  261. $libstats = Requests::get($libstatsUrl, [], []);
  262. if ($libstats->success) {
  263. $libstats = json_decode($libstats->body, true);
  264. $api['libstats'] = $libstats['response'];
  265. $categories = ['movie.svg', 'show.svg', 'artist.svg'];
  266. foreach ($categories as $cat) {
  267. $parts = explode('.', $cat);
  268. $this->cacheImage($url . '/images/libraries/' . $cat, 'tautulli-' . $parts[0], $parts[1]);
  269. }
  270. }
  271. $api['options'] = [
  272. 'url' => $url,
  273. 'libraries' => $this->config['tautulliLibraries'],
  274. 'topMovies' => $this->config['tautulliTopMovies'],
  275. 'topTV' => $this->config['tautulliTopTV'],
  276. 'topUsers' => $this->config['tautulliTopUsers'],
  277. 'topPlatforms' => $this->config['tautulliTopPlatforms'],
  278. 'popularMovies' => $this->config['tautulliPopularMovies'],
  279. 'popularTV' => $this->config['tautulliPopularTV'],
  280. 'title' => $this->config['tautulliHeaderToggle'],
  281. ];
  282. $ids = []; // Array of stat_ids to remove from the returned array
  283. if (!$this->qualifyRequest($this->config['homepageTautulliLibraryAuth'])) {
  284. $api['options']['libraries'] = false;
  285. unset($api['libstats']);
  286. }
  287. if (!$this->qualifyRequest($this->config['homepageTautulliViewsAuth'])) {
  288. $api['options']['topMovies'] = false;
  289. $api['options']['topTV'] = false;
  290. $api['options']['popularMovies'] = false;
  291. $api['options']['popularTV'] = false;
  292. $ids = array_merge(['top_movies', 'popular_movies', 'popular_tv', 'top_tv'], $ids);
  293. $api['homestats']['data'] = array_values($api['homestats']['data']);
  294. }
  295. if (!$this->qualifyRequest($this->config['homepageTautulliMiscAuth'])) {
  296. $api['options']['topUsers'] = false;
  297. $api['options']['topPlatforms'] = false;
  298. $ids = array_merge(['top_platforms', 'top_users'], $ids);
  299. $api['homestats']['data'] = array_values($api['homestats']['data']);
  300. }
  301. $ids = array_merge(['top_music', 'popular_music', 'last_watched', 'most_concurrent'], $ids);
  302. foreach ($ids as $id) {
  303. if ($key = array_search($id, array_column($api['homestats']['data'], 'stat_id'))) {
  304. unset($api['homestats']['data'][$key]);
  305. $api['homestats']['data'] = array_values($api['homestats']['data']);
  306. }
  307. }
  308. } catch (Requests_Exception $e) {
  309. $this->writeLog('error', 'Tautulli Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  310. $this->setAPIResponse('error', $e->getMessage(), 500);
  311. return false;
  312. };
  313. $api = isset($api) ? $api : false;
  314. $this->setAPIResponse('success', null, 200, $api);
  315. return $api;
  316. }
  317. }