plex.php 27 KB

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