tautulli.php 12 KB

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