tautulli.php 12 KB

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