plex.php 30 KB

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