plex.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  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. array(
  208. 'type' => 'switch',
  209. 'name' => 'homepageUseCustomStreamNames',
  210. 'label' => 'Use custom names for users',
  211. 'value' => $this->config['homepageUseCustomStreamNames']
  212. ),
  213. array(
  214. 'type' => 'html',
  215. 'name' => 'homepageCustomStreamNamesAce',
  216. 'class' => 'jsonTextarea hidden',
  217. 'label' => 'Custom definitions for user names (JSON Object, with the key being the plex name, and the value what you want to override with)',
  218. 'override' => 12,
  219. 'html' => '<div id="homepageCustomStreamNamesAce" style="height: 300px;">' . htmlentities($this->config['homepageCustomStreamNames']) . '</div>',
  220. ),
  221. array(
  222. 'type' => 'textbox',
  223. 'name' => 'homepageCustomStreamNames',
  224. 'class' => 'jsonTextarea hidden',
  225. 'id' => 'homepageCustomStreamNamesText',
  226. 'label' => '',
  227. 'value' => $this->config['homepageCustomStreamNames'],
  228. )
  229. ),
  230. 'Test Connection' => array(
  231. array(
  232. 'type' => 'blank',
  233. 'label' => 'Please Save before Testing'
  234. ),
  235. array(
  236. 'type' => 'button',
  237. 'label' => '',
  238. 'icon' => 'fa fa-flask',
  239. 'class' => 'pull-right',
  240. 'text' => 'Test Connection',
  241. 'attr' => 'onclick="testAPIConnection(\'plex\')"'
  242. ),
  243. )
  244. )
  245. );
  246. }
  247. public function testConnectionPlex()
  248. {
  249. if (!empty($this->config['plexURL']) && !empty($this->config['plexToken'])) {
  250. $url = $this->qualifyURL($this->config['plexURL']) . "/servers?X-Plex-Token=" . $this->config['plexToken'];
  251. try {
  252. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  253. $response = Requests::get($url, array(), $options);
  254. libxml_use_internal_errors(true);
  255. if ($response->success) {
  256. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  257. return true;
  258. } else {
  259. $this->setAPIResponse('error', 'URL and/or Token not setup correctly', 422);
  260. return false;
  261. }
  262. } catch (Requests_Exception $e) {
  263. $this->setAPIResponse('error', $e->getMessage(), 500);
  264. return false;
  265. }
  266. } else {
  267. $this->setAPIResponse('error', 'URL and/or Token not setup', 422);
  268. return 'URL and/or Token not setup';
  269. }
  270. }
  271. public function plexHomepagePermissions($key = null)
  272. {
  273. $permissions = [
  274. 'streams' => [
  275. 'enabled' => [
  276. 'homepagePlexEnabled',
  277. 'homepagePlexStreams'
  278. ],
  279. 'auth' => [
  280. 'homepagePlexAuth',
  281. 'homepagePlexStreamsAuth'
  282. ],
  283. 'not_empty' => [
  284. 'plexURL',
  285. 'plexToken',
  286. 'plexID'
  287. ]
  288. ],
  289. 'recent' => [
  290. 'enabled' => [
  291. 'homepagePlexEnabled',
  292. 'homepagePlexRecent'
  293. ],
  294. 'auth' => [
  295. 'homepagePlexAuth',
  296. 'homepagePlexRecentAuth'
  297. ],
  298. 'not_empty' => [
  299. 'plexURL',
  300. 'plexToken',
  301. 'plexID'
  302. ]
  303. ],
  304. 'playlists' => [
  305. 'enabled' => [
  306. 'homepagePlexEnabled',
  307. 'homepagePlexPlaylist'
  308. ],
  309. 'auth' => [
  310. 'homepagePlexAuth',
  311. 'homepagePlexPlaylistAuth'
  312. ],
  313. 'not_empty' => [
  314. 'plexURL',
  315. 'plexToken',
  316. 'plexID'
  317. ]
  318. ],
  319. 'metadata' => [
  320. 'enabled' => [
  321. 'homepagePlexEnabled'
  322. ],
  323. 'auth' => [
  324. 'homepagePlexAuth'
  325. ],
  326. 'not_empty' => [
  327. 'plexURL',
  328. 'plexToken',
  329. 'plexID'
  330. ]
  331. ],
  332. 'search' => [
  333. 'enabled' => [
  334. 'homepagePlexEnabled',
  335. 'mediaSearch'
  336. ],
  337. 'auth' => [
  338. 'homepagePlexAuth',
  339. 'mediaSearchAuth'
  340. ],
  341. 'not_empty' => [
  342. 'plexURL',
  343. 'plexToken',
  344. 'plexID'
  345. ]
  346. ]
  347. ];
  348. if (array_key_exists($key, $permissions)) {
  349. return $permissions[$key];
  350. } elseif ($key == 'all') {
  351. return $permissions;
  352. } else {
  353. return [];
  354. }
  355. }
  356. public function homepageOrderplexnowplaying()
  357. {
  358. if ($this->homepageItemPermissions($this->plexHomepagePermissions('streams'))) {
  359. return '
  360. <div id="' . __FUNCTION__ . '">
  361. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Now Playing...</h2></div>
  362. <script>
  363. // Plex Stream
  364. homepageStream("plex", "' . $this->config['homepageStreamRefresh'] . '");
  365. // End Plex Stream
  366. </script>
  367. </div>
  368. ';
  369. }
  370. }
  371. public function homepageOrderplexrecent()
  372. {
  373. if ($this->homepageItemPermissions($this->plexHomepagePermissions('recent'))) {
  374. return '
  375. <div id="' . __FUNCTION__ . '">
  376. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Recent...</h2></div>
  377. <script>
  378. // Plex Recent
  379. homepageRecent("plex", "' . $this->config['homepageRecentRefresh'] . '");
  380. // End Plex Recent
  381. </script>
  382. </div>
  383. ';
  384. }
  385. }
  386. public function homepageOrderplexplaylist()
  387. {
  388. if ($this->homepageItemPermissions($this->plexHomepagePermissions('playlists'))) {
  389. return '
  390. <div id="' . __FUNCTION__ . '">
  391. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Playlists...</h2></div>
  392. <script>
  393. // Plex Playlist
  394. homepagePlaylist("plex");
  395. // End Plex Playlist
  396. </script>
  397. </div>
  398. ';
  399. }
  400. }
  401. public function getPlexHomepageStreams()
  402. {
  403. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('streams'), true)) {
  404. return false;
  405. }
  406. $ignore = array();
  407. $resolve = true;
  408. $url = $this->qualifyURL($this->config['plexURL']);
  409. $url = $url . "/status/sessions?X-Plex-Token=" . $this->config['plexToken'];
  410. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  411. $response = Requests::get($url, array(), $options);
  412. libxml_use_internal_errors(true);
  413. if ($response->success) {
  414. $items = array();
  415. $plex = simplexml_load_string($response->body);
  416. foreach ($plex as $child) {
  417. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  418. $items[] = $this->resolvePlexItem($child);
  419. }
  420. }
  421. $api['content'] = ($resolve) ? $items : $plex;
  422. $api['plexID'] = $this->config['plexID'];
  423. $api['showNames'] = true;
  424. $api['group'] = '1';
  425. $this->setAPIResponse('success', null, 200, $api);
  426. return $api;
  427. }
  428. }
  429. public function getPlexHomepageRecent()
  430. {
  431. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('recent'), true)) {
  432. return false;
  433. }
  434. $ignore = array();
  435. $resolve = true;
  436. $url = $this->qualifyURL($this->config['plexURL']);
  437. $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";
  438. $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";
  439. $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";
  440. foreach ($urls as $k => $v) {
  441. $options = ($this->localURL($v)) ? array('verify' => false) : array();
  442. $response = Requests::get($v, array(), $options);
  443. libxml_use_internal_errors(true);
  444. if ($response->success) {
  445. $items = array();
  446. $plex = simplexml_load_string($response->body);
  447. foreach ($plex as $child) {
  448. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  449. $items[] = $this->resolvePlexItem($child);
  450. }
  451. }
  452. if (isset($api)) {
  453. $api['content'] = array_merge($api['content'], ($resolve) ? $items : $plex);
  454. } else {
  455. $api['content'] = ($resolve) ? $items : $plex;
  456. }
  457. }
  458. }
  459. if (isset($api['content'])) {
  460. usort($api['content'], function ($a, $b) {
  461. return $b['addedAt'] <=> $a['addedAt'];
  462. });
  463. }
  464. $api['plexID'] = $this->config['plexID'];
  465. $api['showNames'] = true;
  466. $api['group'] = '1';
  467. $this->setAPIResponse('success', null, 200, $api);
  468. return $api;
  469. }
  470. public function getPlexHomepagePlaylists()
  471. {
  472. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('playlists'), true)) {
  473. return false;
  474. }
  475. $url = $this->qualifyURL($this->config['plexURL']);
  476. $url = $url . "/playlists?X-Plex-Token=" . $this->config['plexToken'];
  477. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  478. $response = Requests::get($url, array(), $options);
  479. libxml_use_internal_errors(true);
  480. if ($response->success) {
  481. $items = array();
  482. $plex = simplexml_load_string($response->body);
  483. foreach ($plex as $child) {
  484. if ($child['playlistType'] == "video" && strpos(strtolower($child['title']), 'private') === false) {
  485. $playlistTitleClean = preg_replace("/(\W)+/", "", (string)$child['title']);
  486. $playlistURL = $this->qualifyURL($this->config['plexURL']);
  487. $playlistURL = $playlistURL . $child['key'] . "?X-Plex-Token=" . $this->config['plexToken'];
  488. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  489. $playlistResponse = Requests::get($playlistURL, array(), $options);
  490. if ($playlistResponse->success) {
  491. $playlistResponse = simplexml_load_string($playlistResponse->body);
  492. $items[$playlistTitleClean]['title'] = (string)$child['title'];
  493. foreach ($playlistResponse->Video as $playlistItem) {
  494. $items[$playlistTitleClean][] = $this->resolvePlexItem($playlistItem);
  495. }
  496. }
  497. }
  498. }
  499. $api['content'] = $items;
  500. $api['plexID'] = $this->config['plexID'];
  501. $api['showNames'] = true;
  502. $api['group'] = '1';
  503. $this->setAPIResponse('success', null, 200, $api);
  504. return $api;
  505. } else {
  506. $this->setAPIResponse('error', 'Plex API error', 500);
  507. return false;
  508. }
  509. }
  510. public function getPlexHomepageMetadata($array)
  511. {
  512. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('metadata'), true)) {
  513. return false;
  514. }
  515. $key = $array['key'] ?? null;
  516. if (!$key) {
  517. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  518. return false;
  519. }
  520. $ignore = array();
  521. $resolve = true;
  522. $url = $this->qualifyURL($this->config['plexURL']);
  523. $url = $url . "/library/metadata/" . $key . "?X-Plex-Token=" . $this->config['plexToken'];
  524. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  525. $response = Requests::get($url, array(), $options);
  526. libxml_use_internal_errors(true);
  527. if ($response->success) {
  528. $items = array();
  529. $plex = simplexml_load_string($response->body);
  530. foreach ($plex as $child) {
  531. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  532. $items[] = $this->resolvePlexItem($child);
  533. }
  534. }
  535. $api['content'] = ($resolve) ? $items : $plex;
  536. $api['plexID'] = $this->config['plexID'];
  537. $api['showNames'] = true;
  538. $api['group'] = '1';
  539. $this->setAPIResponse('success', null, 200, $api);
  540. return $api;
  541. }
  542. }
  543. public function getPlexHomepageSearch($query)
  544. {
  545. if (!$this->homepageItemPermissions($this->plexHomepagePermissions('search'), true)) {
  546. return false;
  547. }
  548. $query = $query ?? null;
  549. if (!$query) {
  550. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  551. return false;
  552. }
  553. $ignore = array('artist', 'episode');
  554. $resolve = true;
  555. $url = $this->qualifyURL($this->config['plexURL']);
  556. $url = $url . "/search?query=" . rawurlencode($query) . "&X-Plex-Token=" . $this->config['plexToken'];
  557. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  558. $response = Requests::get($url, array(), $options);
  559. libxml_use_internal_errors(true);
  560. if ($response->success) {
  561. $items = array();
  562. $plex = simplexml_load_string($response->body);
  563. foreach ($plex as $child) {
  564. if (!in_array($child['type'], $ignore) && isset($child['librarySectionID'])) {
  565. $items[] = $this->resolvePlexItem($child);
  566. }
  567. }
  568. $api['content'] = ($resolve) ? $items : $plex;
  569. $api['plexID'] = $this->config['plexID'];
  570. $api['showNames'] = true;
  571. $api['group'] = '1';
  572. $this->setAPIResponse('success', null, 200, $api);
  573. return $api;
  574. }
  575. }
  576. public function resolvePlexItem($item)
  577. {
  578. // Static Height & Width
  579. $height = $this->getCacheImageSize('h');
  580. $width = $this->getCacheImageSize('w');
  581. $nowPlayingHeight = $this->getCacheImageSize('nph');
  582. $nowPlayingWidth = $this->getCacheImageSize('npw');
  583. // Cache Directories
  584. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  585. $cacheDirectoryWeb = 'plugins/images/cache/';
  586. // Types
  587. switch ($item['type']) {
  588. case 'show':
  589. $plexItem['type'] = 'tv';
  590. $plexItem['title'] = (string)$item['title'];
  591. $plexItem['secondaryTitle'] = (string)$item['year'];
  592. $plexItem['summary'] = (string)$item['summary'];
  593. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  594. $plexItem['thumb'] = (string)$item['thumb'];
  595. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  596. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  597. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  598. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  599. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  600. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  601. break;
  602. case 'season':
  603. $plexItem['type'] = 'tv';
  604. $plexItem['title'] = (string)$item['parentTitle'];
  605. $plexItem['secondaryTitle'] = (string)$item['title'];
  606. $plexItem['summary'] = (string)$item['parentSummary'];
  607. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  608. $plexItem['thumb'] = (string)$item['thumb'];
  609. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  610. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  611. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  612. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  613. break;
  614. case 'episode':
  615. $plexItem['type'] = 'tv';
  616. $plexItem['title'] = (string)$item['grandparentTitle'];
  617. $plexItem['secondaryTitle'] = (string)$item['parentTitle'];
  618. $plexItem['summary'] = (string)$item['title'];
  619. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  620. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  621. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  622. $plexItem['nowPlayingThumb'] = (string)$item['grandparentArt'];
  623. $plexItem['nowPlayingKey'] = (string)$item['grandparentRatingKey'] . "-np";
  624. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  625. $plexItem['nowPlayingBottom'] = 'S' . (string)$item['parentIndex'] . ' · E' . (string)$item['index'];
  626. $plexItem['metadataKey'] = (string)$item['grandparentRatingKey'];
  627. break;
  628. case 'clip':
  629. $useImage = (isset($item['live']) ? "plugins/images/cache/livetv.png" : null);
  630. $plexItem['type'] = 'clip';
  631. $plexItem['title'] = (isset($item['live']) ? 'Live TV' : (string)$item['title']);
  632. $plexItem['secondaryTitle'] = '';
  633. $plexItem['summary'] = (string)$item['summary'];
  634. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  635. $plexItem['thumb'] = (string)$item['thumb'];
  636. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  637. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  638. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  639. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  640. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  641. break;
  642. case 'album':
  643. case 'track':
  644. $plexItem['type'] = 'music';
  645. $plexItem['title'] = (string)$item['parentTitle'];
  646. $plexItem['secondaryTitle'] = (string)$item['title'];
  647. $plexItem['summary'] = (string)$item['title'];
  648. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  649. $plexItem['thumb'] = (string)$item['thumb'];
  650. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  651. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  652. $plexItem['nowPlayingKey'] = (string)$item['parentRatingKey'] . "-np";
  653. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'] . ' - ' . (string)$item['title'];
  654. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  655. $plexItem['metadataKey'] = isset($item['grandparentRatingKey']) ? (string)$item['grandparentRatingKey'] : (string)$item['parentRatingKey'];
  656. break;
  657. default:
  658. $plexItem['type'] = 'movie';
  659. $plexItem['title'] = (string)$item['title'];
  660. $plexItem['secondaryTitle'] = (string)$item['year'];
  661. $plexItem['summary'] = (string)$item['summary'];
  662. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  663. $plexItem['thumb'] = (string)$item['thumb'];
  664. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  665. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  666. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  667. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  668. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  669. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  670. }
  671. $plexItem['originalType'] = $item['type'];
  672. $plexItem['uid'] = (string)$item['ratingKey'];
  673. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  674. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  675. $plexItem['addedAt'] = isset($item['addedAt']) ? (int)$item['addedAt'] : null;
  676. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  677. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress'] - $plexItem['watched']) : '';
  678. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision'] : '';
  679. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  680. $plexItem['session'] = (string)$item->Session['id'];
  681. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  682. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  683. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  684. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  685. $plexItem['user'] = $this->formatPlexUserName($item);
  686. $plexItem['userThumb'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['thumb'] : "";
  687. $plexItem['userAddress'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->Player['address'] : "x.x.x.x";
  688. $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'];
  689. $plexItem['nowPlayingOriginalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '$' . $this->randString();
  690. $plexItem['originalImage'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '$' . $this->randString();
  691. $plexItem['openTab'] = $this->config['plexTabURL'] && $this->config['plexTabName'] ? true : false;
  692. $plexItem['tabName'] = $this->config['plexTabName'] ? $this->config['plexTabName'] : '';
  693. // Stream info
  694. $plexItem['userStream'] = array(
  695. 'platform' => (string)$item->Player['platform'],
  696. 'product' => (string)$item->Player['product'],
  697. 'device' => (string)$item->Player['device'],
  698. 'stream' => isset($item->Media) ? (string)$item->Media->Part['decision'] . ($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)' : '') : '',
  699. 'videoResolution' => (string)$item->Media['videoResolution'],
  700. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  701. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  702. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  703. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  704. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  705. 'videoDecision' => $this->streamType((string)$item->TranscodeSession['videoDecision']),
  706. 'audioDecision' => $this->streamType((string)$item->TranscodeSession['audioDecision']),
  707. 'container' => (string)$item->TranscodeSession['container'],
  708. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  709. );
  710. // Genre catch all
  711. if ($item->Genre) {
  712. $genres = array();
  713. foreach ($item->Genre as $key => $value) {
  714. $genres[] = (string)$value['tag'];
  715. }
  716. }
  717. // Actor catch all
  718. if ($item->Role) {
  719. $actors = array();
  720. foreach ($item->Role as $key => $value) {
  721. if ($value['thumb']) {
  722. $actors[] = array(
  723. 'name' => (string)$value['tag'],
  724. 'role' => (string)$value['role'],
  725. 'thumb' => (string)$value['thumb']
  726. );
  727. }
  728. }
  729. }
  730. // Metadata information
  731. $plexItem['metadata'] = array(
  732. 'guid' => (string)$item['guid'],
  733. 'summary' => (string)$item['summary'],
  734. 'rating' => (string)$item['rating'],
  735. 'duration' => (string)$item['duration'],
  736. 'originallyAvailableAt' => (string)$item['originallyAvailableAt'],
  737. 'year' => (string)$item['year'],
  738. 'studio' => (string)$item['studio'],
  739. 'tagline' => (string)$item['tagline'],
  740. 'genres' => ($item->Genre) ? $genres : '',
  741. 'actors' => ($item->Role) ? $actors : ''
  742. );
  743. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  744. $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb . $plexItem['nowPlayingKey'] . '.jpg';
  745. }
  746. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  747. $plexItem['imageURL'] = $cacheDirectoryWeb . $plexItem['key'] . '.jpg';
  748. }
  749. if (file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['nowPlayingKey'] . '.jpg')) {
  750. $plexItem['nowPlayingImageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $plexItem['nowPlayingKey'] . '';
  751. }
  752. if (file_exists($cacheDirectory . $plexItem['key'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $plexItem['key'] . '.jpg') || !file_exists($cacheDirectory . $plexItem['key'] . '.jpg')) {
  753. $plexItem['imageURL'] = 'api/v2/homepage/image?source=plex&img=' . $plexItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $plexItem['key'] . '';
  754. }
  755. if (!$plexItem['nowPlayingThumb']) {
  756. $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png";
  757. $plexItem['nowPlayingKey'] = "no-np";
  758. }
  759. if (!$plexItem['thumb'] || $plexItem['addedAt'] >= (time() - 300)) {
  760. $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/cache/no-list.png";
  761. $plexItem['key'] = "no-list";
  762. }
  763. if (isset($useImage)) {
  764. $plexItem['useImage'] = $useImage;
  765. }
  766. return $plexItem;
  767. }
  768. private function formatPlexUserName($item)
  769. {
  770. $name = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? (string)$item->User['title'] : "";
  771. try {
  772. if ($this->config['homepageUseCustomStreamNames']) {
  773. $customNames = json_decode($this->config['homepageCustomStreamNames'], true);
  774. if (array_key_exists($name, $customNames)) {
  775. $name = $customNames[$name];
  776. }
  777. }
  778. } catch (Exception $e) {
  779. // don't do anythig if it goes wrong, like if the JSON is badly formatted
  780. }
  781. return $name;
  782. }
  783. }