emby.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. <?php
  2. trait EmbyHomepageItem
  3. {
  4. public function embySettingsArray()
  5. {
  6. return array(
  7. 'name' => 'Emby',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/emby.png',
  10. 'category' => 'Media Server',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageEmbyEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageEmbyEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageEmbyAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageEmbyAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'embyURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['embyURL'],
  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' => 'embyToken',
  39. 'label' => 'Token',
  40. 'value' => $this->config['embyToken']
  41. )
  42. ),
  43. 'Active Streams' => array(
  44. array(
  45. 'type' => 'switch',
  46. 'name' => 'homepageEmbyStreams',
  47. 'label' => 'Enable',
  48. 'value' => $this->config['homepageEmbyStreams']
  49. ),
  50. array(
  51. 'type' => 'select',
  52. 'name' => 'homepageEmbyStreamsAuth',
  53. 'label' => 'Minimum Authorization',
  54. 'value' => $this->config['homepageEmbyStreamsAuth'],
  55. 'options' => $this->groupOptions
  56. ),
  57. array(
  58. 'type' => 'switch',
  59. 'name' => 'homepageShowStreamNames',
  60. 'label' => 'User Information',
  61. 'value' => $this->config['homepageShowStreamNames']
  62. ),
  63. array(
  64. 'type' => 'select',
  65. 'name' => 'homepageShowStreamNamesAuth',
  66. 'label' => 'Minimum Authorization',
  67. 'value' => $this->config['homepageShowStreamNamesAuth'],
  68. 'options' => $this->groupOptions
  69. ),
  70. array(
  71. 'type' => 'select',
  72. 'name' => 'homepageStreamRefresh',
  73. 'label' => 'Refresh Seconds',
  74. 'value' => $this->config['homepageStreamRefresh'],
  75. 'options' => $this->timeOptions()
  76. ),
  77. ),
  78. 'Recent Items' => array(
  79. array(
  80. 'type' => 'switch',
  81. 'name' => 'homepageEmbyRecent',
  82. 'label' => 'Enable',
  83. 'value' => $this->config['homepageEmbyRecent']
  84. ),
  85. array(
  86. 'type' => 'select',
  87. 'name' => 'homepageEmbyRecentAuth',
  88. 'label' => 'Minimum Authorization',
  89. 'value' => $this->config['homepageEmbyRecentAuth'],
  90. 'options' => $this->groupOptions
  91. ),
  92. array(
  93. 'type' => 'number',
  94. 'name' => 'homepageRecentLimit',
  95. 'label' => 'Item Limit',
  96. 'value' => $this->config['homepageRecentLimit'],
  97. ),
  98. array(
  99. 'type' => 'select',
  100. 'name' => 'homepageRecentRefresh',
  101. 'label' => 'Refresh Seconds',
  102. 'value' => $this->config['homepageRecentRefresh'],
  103. 'options' => $this->timeOptions()
  104. ),
  105. ),
  106. 'Misc Options' => array(
  107. array(
  108. 'type' => 'input',
  109. 'name' => 'embyTabName',
  110. 'label' => 'Emby Tab Name',
  111. 'value' => $this->config['embyTabName'],
  112. 'placeholder' => 'Only use if you have Emby in a reverse proxy'
  113. ),
  114. array(
  115. 'type' => 'input',
  116. 'name' => 'embyTabURL',
  117. 'label' => 'Emby Tab WAN URL',
  118. 'value' => $this->config['embyTabURL'],
  119. 'placeholder' => 'http(s)://hostname:port'
  120. ),
  121. array(
  122. 'type' => 'select',
  123. 'name' => 'cacheImageSize',
  124. 'label' => 'Image Cache Size',
  125. 'value' => $this->config['cacheImageSize'],
  126. 'options' => array(
  127. array(
  128. 'name' => 'Low',
  129. 'value' => '.5'
  130. ),
  131. array(
  132. 'name' => '1x',
  133. 'value' => '1'
  134. ),
  135. array(
  136. 'name' => '2x',
  137. 'value' => '2'
  138. ),
  139. array(
  140. 'name' => '3x',
  141. 'value' => '3'
  142. )
  143. )
  144. )
  145. ),
  146. 'Test Connection' => array(
  147. array(
  148. 'type' => 'blank',
  149. 'label' => 'Please Save before Testing'
  150. ),
  151. array(
  152. 'type' => 'button',
  153. 'label' => '',
  154. 'icon' => 'fa fa-flask',
  155. 'class' => 'pull-right',
  156. 'text' => 'Test Connection',
  157. 'attr' => 'onclick="testAPIConnection(\'emby\')"'
  158. ),
  159. )
  160. )
  161. );
  162. }
  163. public function testConnectionEmby()
  164. {
  165. if (empty($this->config['embyURL'])) {
  166. $this->setAPIResponse('error', 'Emby URL is not defined', 422);
  167. return false;
  168. }
  169. if (empty($this->config['embyToken'])) {
  170. $this->setAPIResponse('error', 'Emby Token is not defined', 422);
  171. return false;
  172. }
  173. $url = $this->qualifyURL($this->config['embyURL']);
  174. $url = $url . "/Users?api_key=" . $this->config['embyToken'];
  175. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  176. try {
  177. $response = Requests::get($url, array(), $options);
  178. if ($response->success) {
  179. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  180. return true;
  181. } else {
  182. $this->setAPIResponse('error', 'Emby Connection Error', 500);
  183. return true;
  184. }
  185. } catch (Requests_Exception $e) {
  186. $this->setAPIResponse('error', $e->getMessage(), 500);
  187. return false;
  188. }
  189. }
  190. public function embyHomepagePermissions($key = null)
  191. {
  192. $permissions = [
  193. 'streams' => [
  194. 'enabled' => [
  195. 'homepageEmbyEnabled',
  196. 'homepageEmbyStreams'
  197. ],
  198. 'auth' => [
  199. 'homepageEmbyAuth',
  200. 'homepageEmbyStreamsAuth'
  201. ],
  202. 'not_empty' => [
  203. 'embyURL',
  204. 'embyToken'
  205. ]
  206. ],
  207. 'recent' => [
  208. 'enabled' => [
  209. 'homepageEmbyEnabled',
  210. 'homepageEmbyRecent'
  211. ],
  212. 'auth' => [
  213. 'homepageEmbyAuth',
  214. 'homepageEmbyRecentAuth'
  215. ],
  216. 'not_empty' => [
  217. 'embyURL',
  218. 'embyToken'
  219. ]
  220. ],
  221. 'metadata' => [
  222. 'enabled' => [
  223. 'homepageEmbyEnabled'
  224. ],
  225. 'auth' => [
  226. 'homepageEmbyAuth'
  227. ],
  228. 'not_empty' => [
  229. 'embyURL',
  230. 'embyToken'
  231. ]
  232. ]
  233. ];
  234. if (array_key_exists($key, $permissions)) {
  235. return $permissions[$key];
  236. } elseif ($key == 'all') {
  237. return $permissions;
  238. } else {
  239. return [];
  240. }
  241. }
  242. public function homepageOrderembynowplaying()
  243. {
  244. if ($this->homepageItemPermissions($this->embyHomepagePermissions('streams'))) {
  245. return '
  246. <div id="' . __FUNCTION__ . '">
  247. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Now Playing...</h2></div>
  248. <script>
  249. // Emby Stream
  250. homepageStream("emby", "' . $this->config['homepageStreamRefresh'] . '");
  251. // End Emby Stream
  252. </script>
  253. </div>
  254. ';
  255. }
  256. }
  257. public function homepageOrderembyrecent()
  258. {
  259. if ($this->homepageItemPermissions($this->embyHomepagePermissions('recent'))) {
  260. return '
  261. <div id="' . __FUNCTION__ . '">
  262. <div class="white-box homepage-loading-box"><h2 class="text-center" lang="en">Loading Recent...</h2></div>
  263. <script>
  264. // Emby Recent
  265. homepageRecent("emby", "' . $this->config['homepageRecentRefresh'] . '");
  266. // End Emby Recent
  267. </script>
  268. </div>
  269. ';
  270. }
  271. }
  272. public function getEmbyHomepageStreams()
  273. {
  274. if (!$this->homepageItemPermissions($this->embyHomepagePermissions('streams'), true)) {
  275. return false;
  276. }
  277. $url = $this->qualifyURL($this->config['embyURL']);
  278. $url = $url . '/Sessions?api_key=' . $this->config['embyToken'] . '&Fields=Overview,People,Genres,CriticRating,Studios,Taglines';
  279. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  280. try {
  281. $response = Requests::get($url, array(), $options);
  282. if ($response->success) {
  283. $items = array();
  284. $emby = json_decode($response->body, true);
  285. foreach ($emby as $child) {
  286. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  287. $items[] = $this->resolveEmbyItem($child);
  288. }
  289. }
  290. $api['content'] = array_filter($items);
  291. $this->setAPIResponse('success', null, 200, $api);
  292. return $api;
  293. } else {
  294. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  295. return false;
  296. }
  297. } catch (Requests_Exception $e) {
  298. $this->writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  299. $this->setAPIResponse('error', $e->getMessage(), 500);
  300. return false;
  301. }
  302. }
  303. public function getEmbyHomepageRecent()
  304. {
  305. if (!$this->homepageItemPermissions($this->embyHomepagePermissions('recent'), true)) {
  306. return false;
  307. }
  308. $url = $this->qualifyURL($this->config['embyURL']);
  309. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  310. $username = false;
  311. $showPlayed = false;
  312. $userId = 0;
  313. try {
  314. if (isset($this->user['username'])) {
  315. $username = strtolower($this->user['username']);
  316. }
  317. // Get A User
  318. $userIds = $url . "/Users?api_key=" . $this->config['embyToken'];
  319. $response = Requests::get($userIds, array(), $options);
  320. if ($response->success) {
  321. $emby = json_decode($response->body, true);
  322. foreach ($emby as $value) { // Scan for admin user
  323. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  324. $userId = $value['Id'];
  325. }
  326. if ($username && strtolower($value['Name']) == $username) {
  327. $userId = $value['Id'];
  328. $showPlayed = false;
  329. break;
  330. }
  331. }
  332. $url = $url . '/Users/' . $userId . '/Items/Latest?EnableImages=true&Limit=' . $this->config['homepageRecentLimit'] . '&api_key=' . $this->config['embyToken'] . ($showPlayed ? '' : '&IsPlayed=false') . '&Fields=Overview,People,Genres,CriticRating,Studios,Taglines&IncludeItemTypes=Series,Episode,MusicAlbum,Audio,Movie,Video';
  333. } else {
  334. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  335. return false;
  336. }
  337. $response = Requests::get($url, array(), $options);
  338. if ($response->success) {
  339. $items = array();
  340. $emby = json_decode($response->body, true);
  341. foreach ($emby as $child) {
  342. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  343. $items[] = $this->resolveEmbyItem($child);
  344. }
  345. }
  346. $api['content'] = array_filter($items);
  347. $this->setAPIResponse('success', null, 200, $api);
  348. return $api;
  349. } else {
  350. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  351. return false;
  352. }
  353. } catch (Requests_Exception $e) {
  354. $this->writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  355. $this->setAPIResponse('error', $e->getMessage(), 500);
  356. return false;
  357. }
  358. }
  359. public function getEmbyHomepageMetadata($array)
  360. {
  361. if (!$this->homepageItemPermissions($this->embyHomepagePermissions('metadata'), true)) {
  362. return false;
  363. }
  364. $key = $array['key'] ?? null;
  365. if (!$key) {
  366. $this->setAPIResponse('error', 'Emby Metadata key is not defined', 422);
  367. return false;
  368. }
  369. $url = $this->qualifyURL($this->config['embyURL']);
  370. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  371. $username = false;
  372. $showPlayed = false;
  373. $userId = 0;
  374. try {
  375. if (isset($this->user['username'])) {
  376. $username = strtolower($this->user['username']);
  377. }
  378. // Get A User
  379. $userIds = $url . "/Users?api_key=" . $this->config['embyToken'];
  380. $response = Requests::get($userIds, array(), $options);
  381. if ($response->success) {
  382. $emby = json_decode($response->body, true);
  383. foreach ($emby as $value) { // Scan for admin user
  384. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  385. $userId = $value['Id'];
  386. }
  387. if ($username && strtolower($value['Name']) == $username) {
  388. $userId = $value['Id'];
  389. $showPlayed = false;
  390. break;
  391. }
  392. }
  393. $url = $url . '/Users/' . $userId . '/Items/' . $key . '?EnableImages=true&Limit=' . $this->config['homepageRecentLimit'] . '&api_key=' . $this->config['embyToken'] . ($showPlayed ? '' : '&IsPlayed=false') . '&Fields=Overview,People,Genres,CriticRating,Studios,Taglines';
  394. } else {
  395. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  396. return false;
  397. }
  398. $response = Requests::get($url, array(), $options);
  399. if ($response->success) {
  400. $items = array();
  401. $emby = json_decode($response->body, true);
  402. if (isset($emby['NowPlayingItem']) || isset($emby['Name'])) {
  403. $items[] = $this->resolveEmbyItem($emby);
  404. }
  405. $api['content'] = array_filter($items);
  406. $this->setAPIResponse('success', null, 200, $api);
  407. return $api;
  408. } else {
  409. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  410. return false;
  411. }
  412. } catch (Requests_Exception $e) {
  413. $this->writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  414. $this->setAPIResponse('error', $e->getMessage(), 500);
  415. return false;
  416. }
  417. }
  418. public function resolveEmbyItem($itemDetails)
  419. {
  420. $item = isset($itemDetails['NowPlayingItem']['Id']) ? $itemDetails['NowPlayingItem'] : $itemDetails;
  421. // Static Height & Width
  422. $height = $this->getCacheImageSize('h');
  423. $width = $this->getCacheImageSize('w');
  424. $nowPlayingHeight = $this->getCacheImageSize('nph');
  425. $nowPlayingWidth = $this->getCacheImageSize('npw');
  426. $actorHeight = 450;
  427. $actorWidth = 300;
  428. // Cache Directories
  429. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  430. $cacheDirectoryWeb = 'plugins/images/cache/';
  431. // Types
  432. //$embyItem['array-item'] = $item;
  433. //$embyItem['array-itemdetails'] = $itemDetails;
  434. switch (@$item['Type']) {
  435. case 'Series':
  436. $embyItem['type'] = 'tv';
  437. $embyItem['title'] = $item['Name'];
  438. $embyItem['secondaryTitle'] = '';
  439. $embyItem['summary'] = '';
  440. $embyItem['ratingKey'] = $item['Id'];
  441. $embyItem['thumb'] = $item['Id'];
  442. $embyItem['key'] = $item['Id'] . "-list";
  443. $embyItem['nowPlayingThumb'] = $item['Id'];
  444. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  445. $embyItem['metadataKey'] = $item['Id'];
  446. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['BackdropImageTags'][0]) ? 'Backdrop' : '');
  447. break;
  448. case 'Episode':
  449. $embyItem['type'] = 'tv';
  450. $embyItem['title'] = $item['SeriesName'];
  451. $embyItem['secondaryTitle'] = '';
  452. $embyItem['summary'] = '';
  453. $embyItem['ratingKey'] = $item['Id'];
  454. $embyItem['thumb'] = (isset($item['SeriesId']) ? $item['SeriesId'] : $item['Id']);
  455. $embyItem['key'] = (isset($item['SeriesId']) ? $item['SeriesId'] : $item['Id']) . "-list";
  456. $embyItem['nowPlayingThumb'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'] : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'] : false);
  457. $embyItem['nowPlayingKey'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'] . '-np' : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'] . '-np' : false);
  458. $embyItem['metadataKey'] = $item['Id'];
  459. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['ParentBackdropImageTags'][0]) ? 'Backdrop' : '');
  460. $embyItem['nowPlayingTitle'] = @$item['SeriesName'] . ' - ' . @$item['Name'];
  461. $embyItem['nowPlayingBottom'] = 'S' . @$item['ParentIndexNumber'] . ' · E' . @$item['IndexNumber'];
  462. break;
  463. case 'MusicAlbum':
  464. case 'Audio':
  465. $embyItem['type'] = 'music';
  466. $embyItem['title'] = $item['Name'];
  467. $embyItem['secondaryTitle'] = '';
  468. $embyItem['summary'] = '';
  469. $embyItem['ratingKey'] = $item['Id'];
  470. $embyItem['thumb'] = $item['Id'];
  471. $embyItem['key'] = $item['Id'] . "-list";
  472. $embyItem['nowPlayingThumb'] = (isset($item['AlbumId']) ? $item['AlbumId'] : @$item['ParentBackdropItemId']);
  473. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  474. $embyItem['metadataKey'] = isset($item['AlbumId']) ? $item['AlbumId'] : $item['Id'];
  475. $embyItem['nowPlayingImageType'] = (isset($item['ParentBackdropItemId']) ? "Primary" : "Backdrop");
  476. $embyItem['nowPlayingTitle'] = @$item['AlbumArtist'] . ' - ' . @$item['Name'];
  477. $embyItem['nowPlayingBottom'] = @$item['Album'];
  478. break;
  479. case 'Movie':
  480. $embyItem['type'] = 'movie';
  481. $embyItem['title'] = $item['Name'];
  482. $embyItem['secondaryTitle'] = '';
  483. $embyItem['summary'] = '';
  484. $embyItem['ratingKey'] = $item['Id'];
  485. $embyItem['thumb'] = $item['Id'];
  486. $embyItem['key'] = $item['Id'] . "-list";
  487. $embyItem['nowPlayingThumb'] = $item['Id'];
  488. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  489. $embyItem['metadataKey'] = $item['Id'];
  490. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  491. $embyItem['nowPlayingTitle'] = @$item['Name'];
  492. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  493. break;
  494. case 'Video':
  495. $embyItem['type'] = 'video';
  496. $embyItem['title'] = $item['Name'];
  497. $embyItem['secondaryTitle'] = '';
  498. $embyItem['summary'] = '';
  499. $embyItem['ratingKey'] = $item['Id'];
  500. $embyItem['thumb'] = $item['Id'];
  501. $embyItem['key'] = $item['Id'] . "-list";
  502. $embyItem['nowPlayingThumb'] = $item['Id'];
  503. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  504. $embyItem['metadataKey'] = $item['Id'];
  505. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  506. $embyItem['nowPlayingTitle'] = @$item['Name'];
  507. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  508. break;
  509. default:
  510. return false;
  511. }
  512. $embyItem['uid'] = $item['Id'];
  513. $embyItem['imageType'] = (isset($item['ImageTags']['Primary']) ? "Primary" : false);
  514. $embyItem['elapsed'] = isset($itemDetails['PlayState']['PositionTicks']) && $itemDetails['PlayState']['PositionTicks'] !== '0' ? (int)$itemDetails['PlayState']['PositionTicks'] : null;
  515. $embyItem['duration'] = isset($itemDetails['NowPlayingItem']['RunTimeTicks']) ? (int)$itemDetails['NowPlayingItem']['RunTimeTicks'] : (int)(isset($item['RunTimeTicks']) ? $item['RunTimeTicks'] : '');
  516. $embyItem['watched'] = ($embyItem['elapsed'] && $embyItem['duration'] ? floor(($embyItem['elapsed'] / $embyItem['duration']) * 100) : 0);
  517. $embyItem['transcoded'] = isset($itemDetails['TranscodingInfo']['CompletionPercentage']) ? floor((int)$itemDetails['TranscodingInfo']['CompletionPercentage']) : 100;
  518. $embyItem['stream'] = @$itemDetails['PlayState']['PlayMethod'];
  519. $embyItem['id'] = $item['ServerId'];
  520. $embyItem['session'] = @$itemDetails['DeviceId'];
  521. $embyItem['bandwidth'] = isset($itemDetails['TranscodingInfo']['Bitrate']) ? $itemDetails['TranscodingInfo']['Bitrate'] / 1000 : '';
  522. $embyItem['bandwidthType'] = 'wan';
  523. $embyItem['sessionType'] = (@$itemDetails['PlayState']['PlayMethod'] == 'Transcode') ? 'Transcoding' : 'Direct Playing';
  524. $embyItem['state'] = ((@(string)$itemDetails['PlayState']['IsPaused'] == '1') ? "pause" : "play");
  525. $embyItem['user'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? @(string)$itemDetails['UserName'] : "";
  526. $embyItem['userThumb'] = '';
  527. $embyItem['userAddress'] = (isset($itemDetails['RemoteEndPoint']) ? $itemDetails['RemoteEndPoint'] : "x.x.x.x");
  528. $embyURL = 'https://app.emby.media/#!/item/item.html?id=';
  529. $embyItem['address'] = $this->config['embyTabURL'] ? rtrim($this->config['embyTabURL'], '/') . "/web/#!/item/item.html?id=" . $embyItem['uid'] : $embyURL . $embyItem['uid'] . "&serverId=" . $embyItem['id'];
  530. $embyItem['nowPlayingOriginalImage'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['nowPlayingImageType'] . '&img=' . $embyItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $embyItem['nowPlayingKey'] . '$' . $this->randString();
  531. $embyItem['originalImage'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['imageType'] . '&img=' . $embyItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $embyItem['key'] . '$' . $this->randString();
  532. $embyItem['openTab'] = $this->config['embyTabURL'] && $this->config['embyTabName'] ? true : false;
  533. $embyItem['tabName'] = $this->config['embyTabName'] ? $this->config['embyTabName'] : '';
  534. // Stream info
  535. $embyItem['userStream'] = array(
  536. 'platform' => @(string)$itemDetails['Client'],
  537. 'product' => @(string)$itemDetails['Client'],
  538. 'device' => @(string)$itemDetails['DeviceName'],
  539. 'stream' => @$itemDetails['PlayState']['PlayMethod'],
  540. 'videoResolution' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]['Width']) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Width'] : '',
  541. 'throttled' => false,
  542. 'sourceVideoCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : '',
  543. 'videoCodec' => @$itemDetails['TranscodingInfo']['VideoCodec'],
  544. 'audioCodec' => @$itemDetails['TranscodingInfo']['AudioCodec'],
  545. 'sourceAudioCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][1]) ? $itemDetails['NowPlayingItem']['MediaStreams'][1]['Codec'] : (isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : ''),
  546. 'videoDecision' => $this->streamType(@$itemDetails['PlayState']['PlayMethod']),
  547. 'audioDecision' => $this->streamType(@$itemDetails['PlayState']['PlayMethod']),
  548. 'container' => isset($itemDetails['NowPlayingItem']['Container']) ? $itemDetails['NowPlayingItem']['Container'] : '',
  549. 'audioChannels' => @$itemDetails['TranscodingInfo']['AudioChannels']
  550. );
  551. // Genre catch all
  552. if (isset($item['Genres'])) {
  553. $genres = array();
  554. foreach ($item['Genres'] as $genre) {
  555. $genres[] = $genre;
  556. }
  557. }
  558. // Actor catch all
  559. if (isset($item['People'])) {
  560. $actors = array();
  561. foreach ($item['People'] as $key => $value) {
  562. if (@$value['PrimaryImageTag'] && @$value['Role']) {
  563. if (file_exists($cacheDirectory . (string)$value['Id'] . '-cast.jpg')) {
  564. $actorImage = $cacheDirectoryWeb . (string)$value['Id'] . '-cast.jpg';
  565. }
  566. if (file_exists($cacheDirectory . (string)$value['Id'] . '-cast.jpg') && (time() - 604800) > filemtime($cacheDirectory . (string)$value['Id'] . '-cast.jpg') || !file_exists($cacheDirectory . (string)$value['Id'] . '-cast.jpg')) {
  567. $actorImage = 'api/v2/homepage/image?source=emby&type=Primary&img=' . (string)$value['Id'] . '&height=' . $actorHeight . '&width=' . $actorWidth . '&key=' . (string)$value['Id'] . '-cast';
  568. }
  569. $actors[] = array(
  570. 'name' => (string)$value['Name'],
  571. 'role' => (string)$value['Role'],
  572. 'thumb' => $actorImage
  573. );
  574. }
  575. }
  576. }
  577. // Metadata information
  578. $embyItem['metadata'] = array(
  579. 'guid' => $item['Id'],
  580. 'summary' => @(string)$item['Overview'],
  581. 'rating' => @(string)$item['CommunityRating'],
  582. 'duration' => @(string)$item['RunTimeTicks'],
  583. 'originallyAvailableAt' => @(string)$item['PremiereDate'],
  584. 'year' => (string)isset($item['ProductionYear']) ? $item['ProductionYear'] : '',
  585. //'studio' => (string)$item['studio'],
  586. 'tagline' => @(string)$item['Taglines'][0],
  587. 'genres' => (isset($item['Genres'])) ? $genres : '',
  588. 'actors' => (isset($item['People'])) ? $actors : ''
  589. );
  590. if (file_exists($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg')) {
  591. $embyItem['nowPlayingImageURL'] = $cacheDirectoryWeb . $embyItem['nowPlayingKey'] . '.jpg';
  592. }
  593. if (file_exists($cacheDirectory . $embyItem['key'] . '.jpg')) {
  594. $embyItem['imageURL'] = $cacheDirectoryWeb . $embyItem['key'] . '.jpg';
  595. }
  596. if (file_exists($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg') || !file_exists($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg')) {
  597. $embyItem['nowPlayingImageURL'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['nowPlayingImageType'] . '&img=' . $embyItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $embyItem['nowPlayingKey'] . '';
  598. }
  599. if (file_exists($cacheDirectory . $embyItem['key'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $embyItem['key'] . '.jpg') || !file_exists($cacheDirectory . $embyItem['key'] . '.jpg')) {
  600. $embyItem['imageURL'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['imageType'] . '&img=' . $embyItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $embyItem['key'] . '';
  601. }
  602. if (!$embyItem['nowPlayingThumb']) {
  603. $embyItem['nowPlayingOriginalImage'] = $embyItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png";
  604. $embyItem['nowPlayingKey'] = "no-np";
  605. }
  606. if (!$embyItem['thumb']) {
  607. $embyItem['originalImage'] = $embyItem['imageURL'] = "plugins/images/cache/no-list.png";
  608. $embyItem['key'] = "no-list";
  609. }
  610. if (isset($useImage)) {
  611. $embyItem['useImage'] = $useImage;
  612. }
  613. return $embyItem;
  614. }
  615. }