plex.php 30 KB

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