emby.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. <?php
  2. trait EmbyHomepageItem
  3. {
  4. public function testConnectionEmby()
  5. {
  6. if (empty($this->config['embyURL'])) {
  7. $this->setAPIResponse('error', 'Emby URL is not defined', 422);
  8. return false;
  9. }
  10. if (empty($this->config['embyToken'])) {
  11. $this->setAPIResponse('error', 'Emby Token is not defined', 422);
  12. return false;
  13. }
  14. $url = $this->qualifyURL($this->config['embyURL']);
  15. $url = $url . "/Users?api_key=" . $this->config['embyToken'];
  16. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  17. try {
  18. $response = Requests::get($url, array(), $options);
  19. if ($response->success) {
  20. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  21. return true;
  22. } else {
  23. $this->setAPIResponse('error', 'Emby Connection Error', 500);
  24. return true;
  25. }
  26. } catch (Requests_Exception $e) {
  27. $this->setAPIResponse('error', $e->getMessage(), 500);
  28. return false;
  29. }
  30. }
  31. public function resolveEmbyItem($itemDetails)
  32. {
  33. $item = isset($itemDetails['NowPlayingItem']['Id']) ? $itemDetails['NowPlayingItem'] : $itemDetails;
  34. // Static Height & Width
  35. $height = $this->getCacheImageSize('h');
  36. $width = $this->getCacheImageSize('w');
  37. $nowPlayingHeight = $this->getCacheImageSize('nph');
  38. $nowPlayingWidth = $this->getCacheImageSize('npw');
  39. $actorHeight = 450;
  40. $actorWidth = 300;
  41. // Cache Directories
  42. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  43. $cacheDirectoryWeb = 'plugins/images/cache/';
  44. // Types
  45. //$embyItem['array-item'] = $item;
  46. //$embyItem['array-itemdetails'] = $itemDetails;
  47. switch (@$item['Type']) {
  48. case 'Series':
  49. $embyItem['type'] = 'tv';
  50. $embyItem['title'] = $item['Name'];
  51. $embyItem['secondaryTitle'] = '';
  52. $embyItem['summary'] = '';
  53. $embyItem['ratingKey'] = $item['Id'];
  54. $embyItem['thumb'] = $item['Id'];
  55. $embyItem['key'] = $item['Id'] . "-list";
  56. $embyItem['nowPlayingThumb'] = $item['Id'];
  57. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  58. $embyItem['metadataKey'] = $item['Id'];
  59. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['BackdropImageTags'][0]) ? 'Backdrop' : '');
  60. break;
  61. case 'Episode':
  62. $embyItem['type'] = 'tv';
  63. $embyItem['title'] = $item['SeriesName'];
  64. $embyItem['secondaryTitle'] = '';
  65. $embyItem['summary'] = '';
  66. $embyItem['ratingKey'] = $item['Id'];
  67. $embyItem['thumb'] = (isset($item['SeriesId']) ? $item['SeriesId'] : $item['Id']);
  68. $embyItem['key'] = (isset($item['SeriesId']) ? $item['SeriesId'] : $item['Id']) . "-list";
  69. $embyItem['nowPlayingThumb'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'] : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'] : false);
  70. $embyItem['nowPlayingKey'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'] . '-np' : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'] . '-np' : false);
  71. $embyItem['metadataKey'] = $item['Id'];
  72. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['ParentBackdropImageTags'][0]) ? 'Backdrop' : '');
  73. $embyItem['nowPlayingTitle'] = @$item['SeriesName'] . ' - ' . @$item['Name'];
  74. $embyItem['nowPlayingBottom'] = 'S' . @$item['ParentIndexNumber'] . ' · E' . @$item['IndexNumber'];
  75. break;
  76. case 'MusicAlbum':
  77. case 'Audio':
  78. $embyItem['type'] = 'music';
  79. $embyItem['title'] = $item['Name'];
  80. $embyItem['secondaryTitle'] = '';
  81. $embyItem['summary'] = '';
  82. $embyItem['ratingKey'] = $item['Id'];
  83. $embyItem['thumb'] = $item['Id'];
  84. $embyItem['key'] = $item['Id'] . "-list";
  85. $embyItem['nowPlayingThumb'] = (isset($item['AlbumId']) ? $item['AlbumId'] : @$item['ParentBackdropItemId']);
  86. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  87. $embyItem['metadataKey'] = isset($item['AlbumId']) ? $item['AlbumId'] : $item['Id'];
  88. $embyItem['nowPlayingImageType'] = (isset($item['ParentBackdropItemId']) ? "Primary" : "Backdrop");
  89. $embyItem['nowPlayingTitle'] = @$item['AlbumArtist'] . ' - ' . @$item['Name'];
  90. $embyItem['nowPlayingBottom'] = @$item['Album'];
  91. break;
  92. case 'Movie':
  93. $embyItem['type'] = 'movie';
  94. $embyItem['title'] = $item['Name'];
  95. $embyItem['secondaryTitle'] = '';
  96. $embyItem['summary'] = '';
  97. $embyItem['ratingKey'] = $item['Id'];
  98. $embyItem['thumb'] = $item['Id'];
  99. $embyItem['key'] = $item['Id'] . "-list";
  100. $embyItem['nowPlayingThumb'] = $item['Id'];
  101. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  102. $embyItem['metadataKey'] = $item['Id'];
  103. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  104. $embyItem['nowPlayingTitle'] = @$item['Name'];
  105. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  106. break;
  107. case 'Video':
  108. $embyItem['type'] = 'video';
  109. $embyItem['title'] = $item['Name'];
  110. $embyItem['secondaryTitle'] = '';
  111. $embyItem['summary'] = '';
  112. $embyItem['ratingKey'] = $item['Id'];
  113. $embyItem['thumb'] = $item['Id'];
  114. $embyItem['key'] = $item['Id'] . "-list";
  115. $embyItem['nowPlayingThumb'] = $item['Id'];
  116. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  117. $embyItem['metadataKey'] = $item['Id'];
  118. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  119. $embyItem['nowPlayingTitle'] = @$item['Name'];
  120. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  121. break;
  122. default:
  123. return false;
  124. }
  125. $embyItem['uid'] = $item['Id'];
  126. $embyItem['imageType'] = (isset($item['ImageTags']['Primary']) ? "Primary" : false);
  127. $embyItem['elapsed'] = isset($itemDetails['PlayState']['PositionTicks']) && $itemDetails['PlayState']['PositionTicks'] !== '0' ? (int)$itemDetails['PlayState']['PositionTicks'] : null;
  128. $embyItem['duration'] = isset($itemDetails['NowPlayingItem']['RunTimeTicks']) ? (int)$itemDetails['NowPlayingItem']['RunTimeTicks'] : (int)(isset($item['RunTimeTicks']) ? $item['RunTimeTicks'] : '');
  129. $embyItem['watched'] = ($embyItem['elapsed'] && $embyItem['duration'] ? floor(($embyItem['elapsed'] / $embyItem['duration']) * 100) : 0);
  130. $embyItem['transcoded'] = isset($itemDetails['TranscodingInfo']['CompletionPercentage']) ? floor((int)$itemDetails['TranscodingInfo']['CompletionPercentage']) : 100;
  131. $embyItem['stream'] = @$itemDetails['PlayState']['PlayMethod'];
  132. $embyItem['id'] = $item['ServerId'];
  133. $embyItem['session'] = @$itemDetails['DeviceId'];
  134. $embyItem['bandwidth'] = isset($itemDetails['TranscodingInfo']['Bitrate']) ? $itemDetails['TranscodingInfo']['Bitrate'] / 1000 : '';
  135. $embyItem['bandwidthType'] = 'wan';
  136. $embyItem['sessionType'] = (@$itemDetails['PlayState']['PlayMethod'] == 'Transcode') ? 'Transcoding' : 'Direct Playing';
  137. $embyItem['state'] = ((@(string)$itemDetails['PlayState']['IsPaused'] == '1') ? "pause" : "play");
  138. $embyItem['user'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? @(string)$itemDetails['UserName'] : "";
  139. $embyItem['userThumb'] = '';
  140. $embyItem['userAddress'] = (isset($itemDetails['RemoteEndPoint']) ? $itemDetails['RemoteEndPoint'] : "x.x.x.x");
  141. $embyURL = ($this->config['homepageJellyfinInstead']) ? $this->config['embyURL'] . '/web/index.html#!/itemdetails.html?id=' : 'https://app.emby.media/#!/item/item.html?id=';
  142. $embyItem['address'] = $this->config['embyTabURL'] ? rtrim($this->config['embyTabURL'], '/') . "/web/#!/item/item.html?id=" . $embyItem['uid'] : $embyURL . $embyItem['uid'] . "&serverId=" . $embyItem['id'];
  143. $embyItem['nowPlayingOriginalImage'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['nowPlayingImageType'] . '&img=' . $embyItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $embyItem['nowPlayingKey'] . '$' . $this->randString();
  144. $embyItem['originalImage'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['imageType'] . '&img=' . $embyItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $embyItem['key'] . '$' . $this->randString();
  145. $embyItem['openTab'] = $this->config['embyTabURL'] && $this->config['embyTabName'] ? true : false;
  146. $embyItem['tabName'] = $this->config['embyTabName'] ? $this->config['embyTabName'] : '';
  147. // Stream info
  148. $embyItem['userStream'] = array(
  149. 'platform' => @(string)$itemDetails['Client'],
  150. 'product' => @(string)$itemDetails['Client'],
  151. 'device' => @(string)$itemDetails['DeviceName'],
  152. 'stream' => @$itemDetails['PlayState']['PlayMethod'],
  153. 'videoResolution' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]['Width']) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Width'] : '',
  154. 'throttled' => false,
  155. 'sourceVideoCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : '',
  156. 'videoCodec' => @$itemDetails['TranscodingInfo']['VideoCodec'],
  157. 'audioCodec' => @$itemDetails['TranscodingInfo']['AudioCodec'],
  158. 'sourceAudioCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][1]) ? $itemDetails['NowPlayingItem']['MediaStreams'][1]['Codec'] : (isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : ''),
  159. 'videoDecision' => $this->streamType(@$itemDetails['PlayState']['PlayMethod']),
  160. 'audioDecision' => $this->streamType(@$itemDetails['PlayState']['PlayMethod']),
  161. 'container' => isset($itemDetails['NowPlayingItem']['Container']) ? $itemDetails['NowPlayingItem']['Container'] : '',
  162. 'audioChannels' => @$itemDetails['TranscodingInfo']['AudioChannels']
  163. );
  164. // Genre catch all
  165. if (isset($item['Genres'])) {
  166. $genres = array();
  167. foreach ($item['Genres'] as $genre) {
  168. $genres[] = $genre;
  169. }
  170. }
  171. // Actor catch all
  172. if (isset($item['People'])) {
  173. $actors = array();
  174. foreach ($item['People'] as $key => $value) {
  175. if (@$value['PrimaryImageTag'] && @$value['Role']) {
  176. if (file_exists($cacheDirectory . (string)$value['Id'] . '-cast.jpg')) {
  177. $actorImage = $cacheDirectoryWeb . (string)$value['Id'] . '-cast.jpg';
  178. }
  179. 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')) {
  180. $actorImage = 'api/v2/homepage/image?source=emby&type=Primary&img=' . (string)$value['Id'] . '&height=' . $actorHeight . '&width=' . $actorWidth . '&key=' . (string)$value['Id'] . '-cast';
  181. }
  182. $actors[] = array(
  183. 'name' => (string)$value['Name'],
  184. 'role' => (string)$value['Role'],
  185. 'thumb' => $actorImage
  186. );
  187. }
  188. }
  189. }
  190. // Metadata information
  191. $embyItem['metadata'] = array(
  192. 'guid' => $item['Id'],
  193. 'summary' => @(string)$item['Overview'],
  194. 'rating' => @(string)$item['CommunityRating'],
  195. 'duration' => @(string)$item['RunTimeTicks'],
  196. 'originallyAvailableAt' => @(string)$item['PremiereDate'],
  197. 'year' => (string)isset($item['ProductionYear']) ? $item['ProductionYear'] : '',
  198. //'studio' => (string)$item['studio'],
  199. 'tagline' => @(string)$item['Taglines'][0],
  200. 'genres' => (isset($item['Genres'])) ? $genres : '',
  201. 'actors' => (isset($item['People'])) ? $actors : ''
  202. );
  203. if (file_exists($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg')) {
  204. $embyItem['nowPlayingImageURL'] = $cacheDirectoryWeb . $embyItem['nowPlayingKey'] . '.jpg';
  205. }
  206. if (file_exists($cacheDirectory . $embyItem['key'] . '.jpg')) {
  207. $embyItem['imageURL'] = $cacheDirectoryWeb . $embyItem['key'] . '.jpg';
  208. }
  209. if (file_exists($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg') || !file_exists($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg')) {
  210. $embyItem['nowPlayingImageURL'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['nowPlayingImageType'] . '&img=' . $embyItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $embyItem['nowPlayingKey'] . '';
  211. }
  212. if (file_exists($cacheDirectory . $embyItem['key'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $embyItem['key'] . '.jpg') || !file_exists($cacheDirectory . $embyItem['key'] . '.jpg')) {
  213. $embyItem['imageURL'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['imageType'] . '&img=' . $embyItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $embyItem['key'] . '';
  214. }
  215. if (!$embyItem['nowPlayingThumb']) {
  216. $embyItem['nowPlayingOriginalImage'] = $embyItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png";
  217. $embyItem['nowPlayingKey'] = "no-np";
  218. }
  219. if (!$embyItem['thumb']) {
  220. $embyItem['originalImage'] = $embyItem['imageURL'] = "plugins/images/cache/no-list.png";
  221. $embyItem['key'] = "no-list";
  222. }
  223. if (isset($useImage)) {
  224. $embyItem['useImage'] = $useImage;
  225. }
  226. return $embyItem;
  227. }
  228. public function getEmbyHomepageStreams()
  229. {
  230. if (!$this->config['homepageEmbyEnabled']) {
  231. $this->setAPIResponse('error', 'Emby homepage item is not enabled', 409);
  232. return false;
  233. }
  234. if (!$this->config['homepageEmbyStreams']) {
  235. $this->setAPIResponse('error', 'Emby homepage module is not enabled', 409);
  236. return false;
  237. }
  238. if (!$this->qualifyRequest($this->config['homepageEmbyAuth'])) {
  239. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  240. return false;
  241. }
  242. if (!$this->qualifyRequest($this->config['homepageEmbyStreamsAuth'])) {
  243. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  244. return false;
  245. }
  246. if (empty($this->config['embyURL'])) {
  247. $this->setAPIResponse('error', 'Emby URL is not defined', 422);
  248. return false;
  249. }
  250. if (empty($this->config['embyToken'])) {
  251. $this->setAPIResponse('error', 'Emby Token is not defined', 422);
  252. return false;
  253. }
  254. $url = $this->qualifyURL($this->config['embyURL']);
  255. $url = $url . '/Sessions?api_key=' . $this->config['embyToken'] . '&Fields=Overview,People,Genres,CriticRating,Studios,Taglines';
  256. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  257. try {
  258. $response = Requests::get($url, array(), $options);
  259. if ($response->success) {
  260. $items = array();
  261. $emby = json_decode($response->body, true);
  262. foreach ($emby as $child) {
  263. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  264. $items[] = $this->resolveEmbyItem($child);
  265. }
  266. }
  267. $api['content'] = array_filter($items);
  268. $this->setAPIResponse('success', null, 200, $api);
  269. return $api;
  270. } else {
  271. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  272. return false;
  273. }
  274. } catch (Requests_Exception $e) {
  275. $this->writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  276. $this->setAPIResponse('error', $e->getMessage(), 500);
  277. return false;
  278. }
  279. }
  280. public function getEmbyHomepageRecent()
  281. {
  282. if (!$this->config['homepageEmbyEnabled']) {
  283. $this->setAPIResponse('error', 'Emby homepage item is not enabled', 409);
  284. return false;
  285. }
  286. if (!$this->config['homepageEmbyRecent']) {
  287. $this->setAPIResponse('error', 'Emby homepage module is not enabled', 409);
  288. return false;
  289. }
  290. if (!$this->qualifyRequest($this->config['homepageEmbyAuth'])) {
  291. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  292. return false;
  293. }
  294. if (!$this->qualifyRequest($this->config['homepageEmbyRecentAuth'])) {
  295. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  296. return false;
  297. }
  298. if (empty($this->config['embyURL'])) {
  299. $this->setAPIResponse('error', 'Emby URL is not defined', 422);
  300. return false;
  301. }
  302. if (empty($this->config['embyToken'])) {
  303. $this->setAPIResponse('error', 'Emby Token is not defined', 422);
  304. return false;
  305. }
  306. $url = $this->qualifyURL($this->config['embyURL']);
  307. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  308. $username = false;
  309. $showPlayed = false;
  310. $userId = 0;
  311. try {
  312. if (isset($this->user['username'])) {
  313. $username = strtolower($this->user['username']);
  314. }
  315. // Get A User
  316. $userIds = $url . "/Users?api_key=" . $this->config['embyToken'];
  317. $response = Requests::get($userIds, array(), $options);
  318. if ($response->success) {
  319. $emby = json_decode($response->body, true);
  320. foreach ($emby as $value) { // Scan for admin user
  321. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  322. $userId = $value['Id'];
  323. }
  324. if ($username && strtolower($value['Name']) == $username) {
  325. $userId = $value['Id'];
  326. $showPlayed = false;
  327. break;
  328. }
  329. }
  330. $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';
  331. } else {
  332. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  333. return false;
  334. }
  335. $response = Requests::get($url, array(), $options);
  336. if ($response->success) {
  337. $items = array();
  338. $emby = json_decode($response->body, true);
  339. foreach ($emby as $child) {
  340. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  341. $items[] = $this->resolveEmbyItem($child);
  342. }
  343. }
  344. $api['content'] = array_filter($items);
  345. $this->setAPIResponse('success', null, 200, $api);
  346. return $api;
  347. } else {
  348. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  349. return false;
  350. }
  351. } catch (Requests_Exception $e) {
  352. $this->writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  353. $this->setAPIResponse('error', $e->getMessage(), 500);
  354. return false;
  355. }
  356. }
  357. public function getEmbyHomepageMetadata($array)
  358. {
  359. if (!$this->config['homepageEmbyEnabled']) {
  360. $this->setAPIResponse('error', 'Emby homepage item is not enabled', 409);
  361. return false;
  362. }
  363. if (!$this->qualifyRequest($this->config['homepageEmbyAuth'])) {
  364. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  365. return false;
  366. }
  367. if (empty($this->config['embyURL'])) {
  368. $this->setAPIResponse('error', 'Emby URL is not defined', 422);
  369. return false;
  370. }
  371. if (empty($this->config['embyToken'])) {
  372. $this->setAPIResponse('error', 'Emby Token is not defined', 422);
  373. return false;
  374. }
  375. $key = $array['key'] ?? null;
  376. if (!$key) {
  377. $this->setAPIResponse('error', 'Plex Metadata key is not defined', 422);
  378. return false;
  379. }
  380. $url = $this->qualifyURL($this->config['embyURL']);
  381. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  382. $username = false;
  383. $showPlayed = false;
  384. $userId = 0;
  385. try {
  386. if (isset($this->user['username'])) {
  387. $username = strtolower($this->user['username']);
  388. }
  389. // Get A User
  390. $userIds = $url . "/Users?api_key=" . $this->config['embyToken'];
  391. $response = Requests::get($userIds, array(), $options);
  392. if ($response->success) {
  393. $emby = json_decode($response->body, true);
  394. foreach ($emby as $value) { // Scan for admin user
  395. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  396. $userId = $value['Id'];
  397. }
  398. if ($username && strtolower($value['Name']) == $username) {
  399. $userId = $value['Id'];
  400. $showPlayed = false;
  401. break;
  402. }
  403. }
  404. $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';
  405. } else {
  406. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  407. return false;
  408. }
  409. $response = Requests::get($url, array(), $options);
  410. if ($response->success) {
  411. $items = array();
  412. $emby = json_decode($response->body, true);
  413. if (isset($emby['NowPlayingItem']) || isset($emby['Name'])) {
  414. $items[] = $this->resolveEmbyItem($emby);
  415. }
  416. $api['content'] = array_filter($items);
  417. $this->setAPIResponse('success', null, 200, $api);
  418. return $api;
  419. } else {
  420. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  421. return false;
  422. }
  423. } catch (Requests_Exception $e) {
  424. $this->writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  425. $this->setAPIResponse('error', $e->getMessage(), 500);
  426. return false;
  427. }
  428. }
  429. }