plex.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  1. <?php
  2. trait PlexHomepageItem
  3. {
  4. public function plexSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'Plex',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/plex.png',
  10. 'category' => 'Media Server',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $libraryList = [['name' => 'Refresh page to update List', 'value' => '', 'disabled' => true]];
  17. if ($this->config['plexID'] !== '' && $this->config['plexToken'] !== '') {
  18. $loop = $this->plexLibraryList('key');
  19. if ($loop) {
  20. $loop = $loop['libraries'];
  21. foreach ($loop as $key => $value) {
  22. $libraryList[] = ['name' => $key, 'value' => $value];
  23. }
  24. }
  25. }
  26. $homepageSettings = [
  27. 'docs' => $this->docs('features/homepage/plex-homepage-item'),
  28. 'debug' => true,
  29. 'settings' => [
  30. 'Enable' => [
  31. $this->settingsOption('enable', 'homepagePlexEnabled'),
  32. $this->settingsOption('auth', 'homepagePlexAuth'),
  33. ],
  34. 'Connection' => [
  35. $this->settingsOption('url', 'plexURL'),
  36. $this->settingsOption('blank'),
  37. $this->settingsOption('disable-cert-check', 'plexDisableCertCheck'),
  38. $this->settingsOption('use-custom-certificate', 'plexUseCustomCertificate'),
  39. $this->settingsOption('token', 'plexToken'),
  40. $this->settingsOption('button', '', ['label' => 'Get Plex Token', 'icon' => 'fa fa-ticket', 'text' => 'Retrieve', 'attr' => 'onclick="PlexOAuth(oAuthSuccess,oAuthError, null, \'#homepage-Plex-form [name=plexToken]\')"']),
  41. $this->settingsOption('password-alt', 'plexID', ['label' => 'Plex Machine']),
  42. $this->settingsOption('button', '', ['label' => 'Get Plex Machine', 'icon' => 'fa fa-id-badge', 'text' => 'Retrieve', 'attr' => 'onclick="showPlexMachineForm(\'#homepage-Plex-form [name=plexID]\')"']),
  43. ],
  44. 'Active Streams' => [
  45. $this->settingsOption('enable', 'homepagePlexStreams'),
  46. $this->settingsOption('auth', 'homepagePlexStreamsAuth'),
  47. $this->settingsOption('switch', 'homepageShowStreamNames', ['label' => 'User Information']),
  48. $this->settingsOption('auth', 'homepageShowStreamNamesAuth'),
  49. $this->settingsOption('refresh', 'homepageStreamRefresh'),
  50. $this->settingsOption('plex-library-exclude', 'homepagePlexStreamsExclude', ['options' => $libraryList]),
  51. ],
  52. 'Recent Items' => [
  53. $this->settingsOption('enable', 'homepagePlexRecent'),
  54. $this->settingsOption('auth', 'homepagePlexRecentAuth'),
  55. $this->settingsOption('plex-library-exclude', 'homepagePlexRecentExclude', ['options' => $libraryList]),
  56. $this->settingsOption('limit', 'homepageRecentLimit'),
  57. $this->settingsOption('refresh', 'homepageRecentRefresh'),
  58. ],
  59. 'Media Search' => [
  60. $this->settingsOption('enable', 'mediaSearch'),
  61. $this->settingsOption('auth', 'mediaSearchAuth'),
  62. $this->settingsOption('plex-library-exclude', 'homepagePlexSearchExclude', ['options' => $libraryList]),
  63. $this->settingsOption('media-search-server', 'mediaSearchType'),
  64. ],
  65. 'Playlists' => [
  66. $this->settingsOption('enable', 'homepagePlexPlaylist'),
  67. $this->settingsOption('auth', 'homepagePlexPlaylistAuth'),
  68. ],
  69. 'Misc Options' => [
  70. $this->settingsOption('input', 'plexTabName', ['label' => 'Plex Tab Name', 'placeholder' => 'Only use if you have Plex in a reverse proxy']),
  71. $this->settingsOption('input', 'plexTabURL', ['label' => 'Plex Tab WAN URL', 'placeholder' => 'http(s)://domain.com/plex']),
  72. $this->settingsOption('image-cache-quality', 'cacheImageSize'),
  73. $this->settingsOption('blank'),
  74. $this->settingsOption('switch', 'homepageUseCustomStreamNames', ['label' => 'Use Tautulli custom names for users']),
  75. ],
  76. 'Test Connection' => [
  77. $this->settingsOption('blank', null, ['label' => 'Please Save before Testing']),
  78. $this->settingsOption('test', 'plex'),
  79. ]
  80. ]
  81. ];
  82. return array_merge($homepageInformation, $homepageSettings);
  83. }
  84. public function testConnectionPlex()
  85. {
  86. if (!empty($this->config['plexURL']) && !empty($this->config['plexToken'])) {
  87. $url = $this->qualifyURL($this->config['plexURL']) . "/servers?X-Plex-Token=" . $this->config['plexToken'];
  88. try {
  89. $options = $this->requestOptions($url, null, $this->config['plexDisableCertCheck'], $this->config['plexUseCustomCertificate']);
  90. $response = Requests::get($url, [], $options);
  91. libxml_use_internal_errors(true);
  92. if ($response->success) {
  93. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  94. return true;
  95. } else {
  96. $this->setAPIResponse('error', 'URL and/or Token not setup correctly', 422);
  97. return false;
  98. }
  99. } catch (Requests_Exception $e) {
  100. $this->setAPIResponse('error', $e->getMessage(), 500);
  101. return false;
  102. }
  103. } else {
  104. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  105. return 'URL and/or Token not setup';
  106. }
  107. }
  108. public function plexHomepagePermissions($key = null)
  109. {
  110. $permissions = [
  111. 'streams' => [
  112. 'enabled' => [
  113. 'homepagePlexEnabled',
  114. 'homepagePlexStreams'
  115. ],
  116. 'auth' => [
  117. 'homepagePlexAuth',
  118. 'homepagePlexStreamsAuth'
  119. ],
  120. 'not_empty' => [
  121. 'plexURL',
  122. 'plexToken',
  123. 'plexID'
  124. ]
  125. ],
  126. 'recent' => [
  127. 'enabled' => [
  128. 'homepagePlexEnabled',
  129. 'homepagePlexRecent'
  130. ],
  131. 'auth' => [
  132. 'homepagePlexAuth',
  133. 'homepagePlexRecentAuth'
  134. ],
  135. 'not_empty' => [
  136. 'plexURL',
  137. 'plexToken',
  138. 'plexID'
  139. ]
  140. ],
  141. 'playlists' => [
  142. 'enabled' => [
  143. 'homepagePlexEnabled',
  144. 'homepagePlexPlaylist'
  145. ],
  146. 'auth' => [
  147. 'homepagePlexAuth',
  148. 'homepagePlexPlaylistAuth'
  149. ],
  150. 'not_empty' => [
  151. 'plexURL',
  152. 'plexToken',
  153. 'plexID'
  154. ]
  155. ],
  156. 'metadata' => [
  157. 'enabled' => [
  158. 'homepagePlexEnabled'
  159. ],
  160. 'auth' => [
  161. 'homepagePlexAuth'
  162. ],
  163. 'not_empty' => [
  164. 'plexURL',
  165. 'plexToken',
  166. 'plexID'
  167. ]
  168. ],
  169. 'search' => [
  170. 'enabled' => [
  171. 'homepagePlexEnabled',
  172. 'mediaSearch'
  173. ],
  174. 'auth' => [
  175. 'homepagePlexAuth',
  176. 'mediaSearchAuth'
  177. ],
  178. 'not_empty' => [
  179. 'plexURL',
  180. 'plexToken',
  181. 'plexID'
  182. ]
  183. ]
  184. ];
  185. return $this->homepageCheckKeyPermissions($key, $permissions);
  186. }
  187. public function homepageOrderplexnowplaying()
  188. {
  189. if ($this->homepageItemPermissions($this->plexHomepagePermissions('streams'))) {
  190. return '
  191. <div id="' . __FUNCTION__ . '">
  192. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Now Playing...</h2></div>
  193. <script>
  194. // Plex Stream
  195. homepageStream("plex", "' . $this->config['homepageStreamRefresh'] . '");
  196. // End Plex Stream
  197. </script>
  198. </div>
  199. ';
  200. }
  201. }
  202. public function homepageOrderplexrecent()
  203. {
  204. if ($this->homepageItemPermissions($this->plexHomepagePermissions('recent'))) {
  205. return '
  206. <div id="' . __FUNCTION__ . '">
  207. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Recent...</h2></div>
  208. <script>
  209. // Plex Recent
  210. homepageRecent("plex", "' . $this->config['homepageRecentRefresh'] . '");
  211. // End Plex Recent
  212. </script>
  213. </div>
  214. ';
  215. }
  216. }
  217. public function homepageOrderplexplaylist()
  218. {
  219. if ($this->homepageItemPermissions($this->plexHomepagePermissions('playlists'))) {
  220. return '
  221. <div id="' . __FUNCTION__ . '">
  222. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Playlists...</h2></div>
  223. <script>
  224. // Plex Playlist
  225. homepagePlaylist("plex");
  226. // End Plex Playlist
  227. </script>
  228. </div>
  229. ';
  230. }
  231. }
  232. public function getPlexHomepageStreams()
  233. {
  234. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('streams'), true)) {
  235. return false;
  236. }
  237. if ($this->demo) {
  238. return $this->demoData('plex/plex-streams.json');
  239. }
  240. $this->setTautulliFriendlyNames();
  241. $ignore = array();
  242. $exclude = explode(',', $this->config['homepagePlexStreamsExclude']);
  243. $resolve = true;
  244. $url = $this->qualifyURL($this->config['plexURL']);
  245. $url = $url . "/status/sessions?X-Plex-Token=" . $this->config['plexToken'];
  246. $options = $this->requestOptions($url, $this->config['homepageStreamRefresh'], $this->config['plexDisableCertCheck'], $this->config['plexUseCustomCertificate']);
  247. try {
  248. $response = Requests::get($url, [], $options);
  249. libxml_use_internal_errors(true);
  250. if ($response->success) {
  251. $items = array();
  252. $plex = simplexml_load_string($response->body);
  253. foreach ($plex as $child) {
  254. if (!in_array($child['type'], $ignore) && !in_array($child['librarySectionID'], $exclude) && isset($child['librarySectionID'])) {
  255. $items[] = $this->resolvePlexItem($child);
  256. }
  257. }
  258. $api['content'] = ($resolve) ? $items : $plex;
  259. $api['plexID'] = $this->config['plexID'];
  260. $api['showNames'] = true;
  261. $api['group'] = '1';
  262. $this->setAPIResponse('success', null, 200, $api);
  263. return $api;
  264. } else {
  265. $this->setAPIResponse('error', null, 401, []);
  266. return [];
  267. }
  268. } catch (Exception $e) {
  269. $this->setAPIResponse('error', null, 422, [$e->getMessage()]);
  270. return false;
  271. }
  272. }
  273. public function getPlexHomepageRecent()
  274. {
  275. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('recent'), true)) {
  276. return false;
  277. }
  278. $ignore = array();
  279. $exclude = explode(',', $this->config['homepagePlexRecentExclude']);
  280. $resolve = true;
  281. $url = $this->qualifyURL($this->config['plexURL']);
  282. $urls['movie'] = $url . "/hubs/home/recentlyAdded?X-Plex-Token=" . $this->config['plexToken'] . "&X-Plex-Container-Start=0&X-Plex-Container-Size=" . $this->config['homepageRecentLimit'] . "&type=1";
  283. $urls['tv'] = $url . "/hubs/home/recentlyAdded?X-Plex-Token=" . $this->config['plexToken'] . "&X-Plex-Container-Start=0&X-Plex-Container-Size=" . $this->config['homepageRecentLimit'] . "&type=2";
  284. $urls['music'] = $url . "/hubs/home/recentlyAdded?X-Plex-Token=" . $this->config['plexToken'] . "&X-Plex-Container-Start=0&X-Plex-Container-Size=" . $this->config['homepageRecentLimit'] . "&type=8";
  285. try {
  286. foreach ($urls as $k => $v) {
  287. $options = $this->requestOptions($url, $this->config['homepageRecentRefresh'], $this->config['plexDisableCertCheck'], $this->config['plexUseCustomCertificate']);
  288. $response = Requests::get($v, [], $options);
  289. libxml_use_internal_errors(true);
  290. if ($response->success) {
  291. $items = array();
  292. $plex = simplexml_load_string($response->body);
  293. foreach ($plex as $child) {
  294. if (!in_array($child['type'], $ignore) && !in_array($child['librarySectionID'], $exclude) && isset($child['librarySectionID'])) {
  295. $items[] = $this->resolvePlexItem($child);
  296. }
  297. }
  298. if (isset($api)) {
  299. $api['content'] = array_merge($api['content'], ($resolve) ? $items : $plex);
  300. } else {
  301. $api['content'] = ($resolve) ? $items : $plex;
  302. }
  303. }
  304. }
  305. if (isset($api['content'])) {
  306. usort($api['content'], function ($a, $b) {
  307. return $b['addedAt'] <=> $a['addedAt'];
  308. });
  309. }
  310. $api['plexID'] = $this->config['plexID'];
  311. $api['showNames'] = true;
  312. $api['group'] = '1';
  313. $this->setAPIResponse('success', null, 200, $api);
  314. return $api;
  315. } catch (Exception $e) {
  316. $this->setAPIResponse('error', null, 422, [$e->getMessage()]);
  317. return false;
  318. }
  319. }
  320. public function getPlexHomepagePlaylists()
  321. {
  322. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('playlists'), true)) {
  323. return false;
  324. }
  325. $url = $this->qualifyURL($this->config['plexURL']);
  326. $url = $url . "/playlists?X-Plex-Token=" . $this->config['plexToken'];
  327. $options = $this->requestOptions($url, null, $this->config['plexDisableCertCheck'], $this->config['plexUseCustomCertificate']);
  328. try {
  329. $response = Requests::get($url, [], $options);
  330. libxml_use_internal_errors(true);
  331. if ($response->success) {
  332. $items = array();
  333. $plex = simplexml_load_string($response->body);
  334. foreach ($plex as $child) {
  335. if ($child['playlistType'] == "video" && strpos(strtolower($child['title']), 'private') === false) {
  336. $playlistTitleClean = preg_replace("/(\W)+/", "", (string)$child['title']);
  337. $playlistURL = $this->qualifyURL($this->config['plexURL']);
  338. $playlistURL = $playlistURL . $child['key'] . "?X-Plex-Token=" . $this->config['plexToken'];
  339. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  340. $playlistResponse = Requests::get($playlistURL, array(), $options);
  341. if ($playlistResponse->success) {
  342. $playlistResponse = simplexml_load_string($playlistResponse->body);
  343. $items[$playlistTitleClean]['title'] = (string)$child['title'];
  344. foreach ($playlistResponse->Video as $playlistItem) {
  345. $items[$playlistTitleClean][] = $this->resolvePlexItem($playlistItem);
  346. }
  347. }
  348. }
  349. }
  350. $api['content'] = $items;
  351. $api['plexID'] = $this->config['plexID'];
  352. $api['showNames'] = true;
  353. $api['group'] = '1';
  354. $this->setAPIResponse('success', null, 200, $api);
  355. return $api;
  356. } else {
  357. $this->setAPIResponse('error', 'Plex API error', 500);
  358. return false;
  359. }
  360. } catch (Exception $e) {
  361. $this->setAPIResponse('error', null, 422, [$e->getMessage()]);
  362. return false;
  363. }
  364. }
  365. public function getPlexHomepageMetadata($array)
  366. {
  367. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('metadata'), true)) {
  368. return false;
  369. }
  370. if ($this->demo) {
  371. return $this->demoData('plex/plex-metadata.json');
  372. }
  373. $key = $array['key'] ?? null;
  374. if (!$key) {
  375. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  376. return false;
  377. }
  378. $ignore = array();
  379. $resolve = true;
  380. $url = $this->qualifyURL($this->config['plexURL']);
  381. $url = $url . "/library/metadata/" . $key . "?X-Plex-Token=" . $this->config['plexToken'];
  382. $options = $this->requestOptions($url, null, $this->config['plexDisableCertCheck'], $this->config['plexUseCustomCertificate']);
  383. try {
  384. $response = Requests::get($url, [], $options);
  385. libxml_use_internal_errors(true);
  386. if ($response->success) {
  387. $items = array();
  388. $plex = simplexml_load_string($response->body);
  389. foreach ($plex as $child) {
  390. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  391. $items[] = $this->resolvePlexItem($child);
  392. }
  393. }
  394. $api['content'] = ($resolve) ? $items : $plex;
  395. $api['plexID'] = $this->config['plexID'];
  396. $api['showNames'] = true;
  397. $api['group'] = '1';
  398. $this->setAPIResponse('success', null, 200, $api);
  399. return $api;
  400. }
  401. } catch (Exception $e) {
  402. $this->setAPIResponse('error', null, 422, [$e->getMessage()]);
  403. return false;
  404. }
  405. }
  406. public function getPlexHomepageSearch($query)
  407. {
  408. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('search'), true)) {
  409. return false;
  410. }
  411. $query = $query ?? null;
  412. if (!$query) {
  413. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  414. return false;
  415. }
  416. $ignore = array('artist', 'episode');
  417. $exclude = explode(',', $this->config['homepagePlexSearchExclude']);
  418. $resolve = true;
  419. $url = $this->qualifyURL($this->config['plexURL']);
  420. $url = $url . "/search?query=" . rawurlencode($query) . "&X-Plex-Token=" . $this->config['plexToken'];
  421. $options = $this->requestOptions($url, null, $this->config['plexDisableCertCheck'], $this->config['plexUseCustomCertificate']);
  422. try {
  423. $response = Requests::get($url, [], $options);
  424. libxml_use_internal_errors(true);
  425. if ($response->success) {
  426. $items = array();
  427. $plex = simplexml_load_string($response->body);
  428. foreach ($plex as $child) {
  429. if (!in_array($child['type'], $ignore) && !in_array($child['librarySectionID'], $exclude) && isset($child['librarySectionID'])) {
  430. $items[] = $this->resolvePlexItem($child);
  431. }
  432. }
  433. $api['content'] = ($resolve) ? $items : $plex;
  434. $api['plexID'] = $this->config['plexID'];
  435. $api['showNames'] = true;
  436. $api['group'] = '1';
  437. $this->setAPIResponse('success', null, 200, $api);
  438. return $api;
  439. }
  440. } catch (Exception $e) {
  441. $this->setAPIResponse('error', null, 422, [$e->getMessage()]);
  442. return false;
  443. }
  444. }
  445. public function resolvePlexItem($item)
  446. {
  447. // Static Height & Width
  448. $height = $this->getCacheImageSize('h');
  449. $width = $this->getCacheImageSize('w');
  450. $nowPlayingHeight = $this->getCacheImageSize('nph');
  451. $nowPlayingWidth = $this->getCacheImageSize('npw');
  452. // Cache Directories
  453. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  454. $cacheDirectoryWeb = 'data/cache/';
  455. // Types
  456. switch ($item['type']) {
  457. case 'show':
  458. $plexItem['type'] = 'tv';
  459. $plexItem['title'] = (string)$item['title'];
  460. $plexItem['secondaryTitle'] = (string)$item['year'];
  461. $plexItem['summary'] = (string)$item['summary'];
  462. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  463. $plexItem['thumb'] = (string)$item['thumb'];
  464. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  465. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  466. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  467. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  468. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  469. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  470. break;
  471. case 'season':
  472. $plexItem['type'] = 'tv';
  473. $plexItem['title'] = (string)$item['parentTitle'];
  474. $plexItem['secondaryTitle'] = (string)$item['title'];
  475. $plexItem['summary'] = (string)$item['parentSummary'];
  476. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  477. $plexItem['thumb'] = (string)$item['thumb'];
  478. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  479. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  480. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  481. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  482. break;
  483. case 'episode':
  484. $useImage = (isset($item['live']) ? 'plugins/images/homepage/livetv.png' : null);
  485. $plexItem['type'] = 'tv';
  486. $plexItem['title'] = (string)$item['grandparentTitle'];
  487. $plexItem['secondaryTitle'] = (string)$item['parentTitle'] . ' - Episode ' . (string)$item['index'];
  488. $plexItem['summary'] = (string)$item['title'];
  489. $plexItem['ratingKey'] = (string)($item['parentRatingKey'] ?? $item['ratingKey']);
  490. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  491. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  492. $plexItem['nowPlayingThumb'] = (string)$item['grandparentArt'];
  493. $plexItem['nowPlayingKey'] = (string)$item['grandparentRatingKey'] . "-np";
  494. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  495. $plexItem['nowPlayingBottom'] = 'S' . (string)$item['parentIndex'] . ' · E' . (string)$item['index'];
  496. $plexItem['metadataKey'] = (string)($item['grandparentRatingKey'] ?? $item['parentRatingKey'] ?? $item['ratingKey']);
  497. break;
  498. case 'clip':
  499. $useImage = (isset($item['live']) ? "plugins/images/homepage/livetv.png" : null);
  500. $plexItem['type'] = 'clip';
  501. $plexItem['title'] = (isset($item['live']) ? 'Live TV' : (string)$item['title']);
  502. $plexItem['secondaryTitle'] = '';
  503. $plexItem['summary'] = (string)$item['summary'];
  504. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  505. $plexItem['thumb'] = (string)$item['thumb'];
  506. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  507. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  508. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  509. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  510. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  511. break;
  512. case 'album':
  513. case 'track':
  514. $plexItem['type'] = 'music';
  515. $plexItem['title'] = (string)$item['parentTitle'];
  516. $plexItem['secondaryTitle'] = (string)$item['title'];
  517. $plexItem['summary'] = (string)$item['title'];
  518. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  519. $plexItem['thumb'] = (string)$item['thumb'];
  520. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  521. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  522. $plexItem['nowPlayingKey'] = (string)$item['parentRatingKey'] . "-np";
  523. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  524. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  525. $plexItem['metadataKey'] = isset($item['grandparentRatingKey']) ? (string)$item['grandparentRatingKey'] : (string)$item['parentRatingKey'];
  526. break;
  527. default:
  528. $useImage = (isset($item['live']) ? 'plugins/images/homepage/livetv.png' : null);
  529. $plexItem['type'] = 'movie';
  530. $plexItem['title'] = (string)$item['title'];
  531. $plexItem['secondaryTitle'] = (string)$item['year'];
  532. $plexItem['summary'] = (string)$item['summary'];
  533. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  534. $plexItem['thumb'] = (string)$item['thumb'];
  535. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  536. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  537. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  538. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  539. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  540. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  541. }
  542. $plexItem['originalType'] = $item['type'];
  543. $plexItem['uid'] = (string)$item['ratingKey'];
  544. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  545. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  546. $plexItem['addedAt'] = isset($item['addedAt']) ? (int)$item['addedAt'] : null;
  547. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  548. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress'] - $plexItem['watched']) : '';
  549. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision'] : '';
  550. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  551. $plexItem['session'] = (string)$item->Session['id'];
  552. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  553. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  554. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  555. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  556. $plexItem['user'] = $this->formatPlexUserName($item);
  557. $plexItem['userThumb'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['thumb'] : "";
  558. $plexItem['userAddress'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->Player['address'] : "x.x.x.x";
  559. $plexItem['address'] = $this->config['plexTabURL'] ? $this->config['plexTabURL'] . "/web/index.html#!/server/" . $this->config['plexID'] . "/details?key=/library/metadata/" . $item['ratingKey'] : "https://app.plex.tv/web/app#!/server/" . $this->config['plexID'] . "/details?key=/library/metadata/" . $item['ratingKey'];
  560. $plexItem['nowPlayingOriginalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '$' . $this->randString();
  561. $plexItem['originalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '$' . $this->randString();
  562. $plexItem['openTab'] = $this->config['plexTabURL'] && $this->config['plexTabName'] ? true : false;
  563. $plexItem['tabName'] = $this->config['plexTabName'] ? $this->config['plexTabName'] : '';
  564. // Stream info
  565. $plexItem['userStream'] = array(
  566. 'platform' => (string)$item->Player['platform'],
  567. 'product' => (string)$item->Player['product'],
  568. 'device' => (string)$item->Player['device'],
  569. 'stream' => isset($item->Media) ? (string)$item->Media->Part['decision'] . ($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)' : '') : '',
  570. 'videoResolution' => (string)$item->Media['videoResolution'],
  571. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  572. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  573. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  574. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  575. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  576. 'videoDecision' => $this->streamType((string)$item->TranscodeSession['videoDecision']),
  577. 'audioDecision' => $this->streamType((string)$item->TranscodeSession['audioDecision']),
  578. 'container' => (string)$item->TranscodeSession['container'],
  579. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  580. );
  581. // Genre catch all
  582. if ($item->Genre) {
  583. $genres = array();
  584. foreach ($item->Genre as $key => $value) {
  585. $genres[] = (string)$value['tag'];
  586. }
  587. }
  588. // Actor catch all
  589. if ($item->Role) {
  590. $actors = array();
  591. foreach ($item->Role as $key => $value) {
  592. if ($value['thumb']) {
  593. $actors[] = array(
  594. 'name' => (string)$value['tag'],
  595. 'role' => (string)$value['role'],
  596. 'thumb' => (string)$value['thumb']
  597. );
  598. }
  599. }
  600. }
  601. // Metadata information
  602. $plexItem['metadata'] = array(
  603. 'guid' => (string)$item['guid'],
  604. 'summary' => (string)$item['summary'],
  605. 'rating' => (string)$item['rating'],
  606. 'duration' => (string)$item['duration'],
  607. 'originallyAvailableAt' => (string)$item['originallyAvailableAt'],
  608. 'year' => (string)$item['year'],
  609. 'studio' => (string)$item['studio'],
  610. 'tagline' => (string)$item['tagline'],
  611. 'genres' => ($item->Genre) ? $genres : '',
  612. 'actors' => ($item->Role) ? $actors : ''
  613. );
  614. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  615. $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb . $plexItem['nowPlayingKey'] . '.jpg';
  616. }
  617. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  618. $plexItem['imageURL'] = $cacheDirectoryWeb . $plexItem['key'] . '.jpg';
  619. }
  620. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  621. $plexItem['nowPlayingImageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '';
  622. }
  623. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['key'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  624. $plexItem['imageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '';
  625. }
  626. if (!$plexItem['nowPlayingThumb']) {
  627. $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/homepage/no-np.png";
  628. $plexItem['nowPlayingKey'] = "no-np";
  629. }
  630. if (!$plexItem['thumb'] || $plexItem['addedAt'] >= (time() - 300)) {
  631. $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/homepage/no-list.png";
  632. $plexItem['key'] = "no-list";
  633. }
  634. if (isset($useImage)) {
  635. $plexItem['useImage'] = $useImage;
  636. }
  637. return $plexItem;
  638. }
  639. public function getTautulliFriendlyNames($bypass = null)
  640. {
  641. $names = [];
  642. if (!$this->qualifyRequest(1) && !$bypass) {
  643. return false;
  644. }
  645. $url = $this->qualifyURL($this->config['tautulliURL']);
  646. $url .= '/api/v2?apikey=' . $this->config['tautulliApikey'];
  647. $url .= '&cmd=get_users';
  648. $options = $this->requestOptions($url, null, $this->config['tautulliDisableCertCheck'], $this->config['tautulliUseCustomCertificate']);
  649. try {
  650. $response = Requests::get($url, [], $options);
  651. $response = json_decode($response->body, true);
  652. foreach ($response['response']['data'] as $user) {
  653. if ($user['user_id'] != 0) {
  654. $names[$user['username']] = $user['friendly_name'];
  655. }
  656. }
  657. } catch (Exception $e) {
  658. $this->setAPIResponse('error', null, 422, [$e->getMessage()]);
  659. }
  660. $this->setAPIResponse('success', null, 200, $names);
  661. return $names;
  662. }
  663. public function setTautulliFriendlyNames()
  664. {
  665. if ($this->config['tautulliURL'] && $this->config['tautulliApikey'] && $this->config['homepageUseCustomStreamNames']) {
  666. $names = $this->getTautulliFriendlyNames(true);
  667. $names = json_encode($names);
  668. if ($names !== $this->config['homepageCustomStreamNames']) {
  669. $this->updateConfig(array('homepageCustomStreamNames' => $names));
  670. $this->config['homepageCustomStreamNames'] = $names;
  671. $this->setLoggerChannel('Tautulli');
  672. $this->logger->debug('Updating Tautulli custom names config item', $names);
  673. }
  674. }
  675. }
  676. private function formatPlexUserName($item)
  677. {
  678. $name = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['title'] : "";
  679. try {
  680. if ($this->config['homepageUseCustomStreamNames']) {
  681. $customNames = json_decode($this->config['homepageCustomStreamNames'], true);
  682. if (array_key_exists($name, $customNames)) {
  683. $name = $customNames[$name];
  684. }
  685. }
  686. } catch (Exception $e) {
  687. // don't do anythig if it goes wrong, like if the JSON is badly formatted
  688. }
  689. return $name;
  690. }
  691. public function plexLibraryList($value = 'id')
  692. {
  693. if (!empty($this->config['plexToken']) && !empty($this->config['plexID'])) {
  694. $url = 'https://plex.tv/api/servers/' . $this->config['plexID'];
  695. try {
  696. $headers = array(
  697. "Accept" => "application/json",
  698. "X-Plex-Token" => $this->config['plexToken']
  699. );
  700. $response = Requests::get($url, $headers, array());
  701. libxml_use_internal_errors(true);
  702. if ($response->success) {
  703. $libraryList = array();
  704. $plex = simplexml_load_string($response->body);
  705. foreach ($plex->Server->Section as $child) {
  706. $libraryList['libraries'][(string)$child['title']] = (string)$child[$value];
  707. }
  708. $libraryList = array_change_key_case($libraryList, CASE_LOWER);
  709. return $libraryList;
  710. }
  711. } catch (Requests_Exception $e) {
  712. $this->writeLog('error', 'Plex Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  713. return false;
  714. };
  715. }
  716. return false;
  717. }
  718. }