tautulli.php 13 KB

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