plex.php 28 KB

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