emby.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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 resolveEmbyItem($itemDetails)
  191. {
  192. $item = isset($itemDetails['NowPlayingItem']['Id']) ? $itemDetails['NowPlayingItem'] : $itemDetails;
  193. // Static Height & Width
  194. $height = $this->getCacheImageSize('h');
  195. $width = $this->getCacheImageSize('w');
  196. $nowPlayingHeight = $this->getCacheImageSize('nph');
  197. $nowPlayingWidth = $this->getCacheImageSize('npw');
  198. $actorHeight = 450;
  199. $actorWidth = 300;
  200. // Cache Directories
  201. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  202. $cacheDirectoryWeb = 'plugins/images/cache/';
  203. // Types
  204. //$embyItem['array-item'] = $item;
  205. //$embyItem['array-itemdetails'] = $itemDetails;
  206. switch (@$item['Type']) {
  207. case 'Series':
  208. $embyItem['type'] = 'tv';
  209. $embyItem['title'] = $item['Name'];
  210. $embyItem['secondaryTitle'] = '';
  211. $embyItem['summary'] = '';
  212. $embyItem['ratingKey'] = $item['Id'];
  213. $embyItem['thumb'] = $item['Id'];
  214. $embyItem['key'] = $item['Id'] . "-list";
  215. $embyItem['nowPlayingThumb'] = $item['Id'];
  216. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  217. $embyItem['metadataKey'] = $item['Id'];
  218. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['BackdropImageTags'][0]) ? 'Backdrop' : '');
  219. break;
  220. case 'Episode':
  221. $embyItem['type'] = 'tv';
  222. $embyItem['title'] = $item['SeriesName'];
  223. $embyItem['secondaryTitle'] = '';
  224. $embyItem['summary'] = '';
  225. $embyItem['ratingKey'] = $item['Id'];
  226. $embyItem['thumb'] = (isset($item['SeriesId']) ? $item['SeriesId'] : $item['Id']);
  227. $embyItem['key'] = (isset($item['SeriesId']) ? $item['SeriesId'] : $item['Id']) . "-list";
  228. $embyItem['nowPlayingThumb'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'] : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'] : false);
  229. $embyItem['nowPlayingKey'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'] . '-np' : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'] . '-np' : false);
  230. $embyItem['metadataKey'] = $item['Id'];
  231. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['ParentBackdropImageTags'][0]) ? 'Backdrop' : '');
  232. $embyItem['nowPlayingTitle'] = @$item['SeriesName'] . ' - ' . @$item['Name'];
  233. $embyItem['nowPlayingBottom'] = 'S' . @$item['ParentIndexNumber'] . ' · E' . @$item['IndexNumber'];
  234. break;
  235. case 'MusicAlbum':
  236. case 'Audio':
  237. $embyItem['type'] = 'music';
  238. $embyItem['title'] = $item['Name'];
  239. $embyItem['secondaryTitle'] = '';
  240. $embyItem['summary'] = '';
  241. $embyItem['ratingKey'] = $item['Id'];
  242. $embyItem['thumb'] = $item['Id'];
  243. $embyItem['key'] = $item['Id'] . "-list";
  244. $embyItem['nowPlayingThumb'] = (isset($item['AlbumId']) ? $item['AlbumId'] : @$item['ParentBackdropItemId']);
  245. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  246. $embyItem['metadataKey'] = isset($item['AlbumId']) ? $item['AlbumId'] : $item['Id'];
  247. $embyItem['nowPlayingImageType'] = (isset($item['ParentBackdropItemId']) ? "Primary" : "Backdrop");
  248. $embyItem['nowPlayingTitle'] = @$item['AlbumArtist'] . ' - ' . @$item['Name'];
  249. $embyItem['nowPlayingBottom'] = @$item['Album'];
  250. break;
  251. case 'Movie':
  252. $embyItem['type'] = 'movie';
  253. $embyItem['title'] = $item['Name'];
  254. $embyItem['secondaryTitle'] = '';
  255. $embyItem['summary'] = '';
  256. $embyItem['ratingKey'] = $item['Id'];
  257. $embyItem['thumb'] = $item['Id'];
  258. $embyItem['key'] = $item['Id'] . "-list";
  259. $embyItem['nowPlayingThumb'] = $item['Id'];
  260. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  261. $embyItem['metadataKey'] = $item['Id'];
  262. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  263. $embyItem['nowPlayingTitle'] = @$item['Name'];
  264. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  265. break;
  266. case 'Video':
  267. $embyItem['type'] = 'video';
  268. $embyItem['title'] = $item['Name'];
  269. $embyItem['secondaryTitle'] = '';
  270. $embyItem['summary'] = '';
  271. $embyItem['ratingKey'] = $item['Id'];
  272. $embyItem['thumb'] = $item['Id'];
  273. $embyItem['key'] = $item['Id'] . "-list";
  274. $embyItem['nowPlayingThumb'] = $item['Id'];
  275. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  276. $embyItem['metadataKey'] = $item['Id'];
  277. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  278. $embyItem['nowPlayingTitle'] = @$item['Name'];
  279. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  280. break;
  281. default:
  282. return false;
  283. }
  284. $embyItem['uid'] = $item['Id'];
  285. $embyItem['imageType'] = (isset($item['ImageTags']['Primary']) ? "Primary" : false);
  286. $embyItem['elapsed'] = isset($itemDetails['PlayState']['PositionTicks']) && $itemDetails['PlayState']['PositionTicks'] !== '0' ? (int)$itemDetails['PlayState']['PositionTicks'] : null;
  287. $embyItem['duration'] = isset($itemDetails['NowPlayingItem']['RunTimeTicks']) ? (int)$itemDetails['NowPlayingItem']['RunTimeTicks'] : (int)(isset($item['RunTimeTicks']) ? $item['RunTimeTicks'] : '');
  288. $embyItem['watched'] = ($embyItem['elapsed'] && $embyItem['duration'] ? floor(($embyItem['elapsed'] / $embyItem['duration']) * 100) : 0);
  289. $embyItem['transcoded'] = isset($itemDetails['TranscodingInfo']['CompletionPercentage']) ? floor((int)$itemDetails['TranscodingInfo']['CompletionPercentage']) : 100;
  290. $embyItem['stream'] = @$itemDetails['PlayState']['PlayMethod'];
  291. $embyItem['id'] = $item['ServerId'];
  292. $embyItem['session'] = @$itemDetails['DeviceId'];
  293. $embyItem['bandwidth'] = isset($itemDetails['TranscodingInfo']['Bitrate']) ? $itemDetails['TranscodingInfo']['Bitrate'] / 1000 : '';
  294. $embyItem['bandwidthType'] = 'wan';
  295. $embyItem['sessionType'] = (@$itemDetails['PlayState']['PlayMethod'] == 'Transcode') ? 'Transcoding' : 'Direct Playing';
  296. $embyItem['state'] = ((@(string)$itemDetails['PlayState']['IsPaused'] == '1') ? "pause" : "play");
  297. $embyItem['user'] = ($this->config['homepageShowStreamNames'] && $this->qualifyRequest($this->config['homepageShowStreamNamesAuth'])) ? @(string)$itemDetails['UserName'] : "";
  298. $embyItem['userThumb'] = '';
  299. $embyItem['userAddress'] = (isset($itemDetails['RemoteEndPoint']) ? $itemDetails['RemoteEndPoint'] : "x.x.x.x");
  300. $embyURL = 'https://app.emby.media/#!/item/item.html?id=';
  301. $embyItem['address'] = $this->config['embyTabURL'] ? rtrim($this->config['embyTabURL'], '/') . "/web/#!/item/item.html?id=" . $embyItem['uid'] : $embyURL . $embyItem['uid'] . "&serverId=" . $embyItem['id'];
  302. $embyItem['nowPlayingOriginalImage'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['nowPlayingImageType'] . '&img=' . $embyItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $embyItem['nowPlayingKey'] . '$' . $this->randString();
  303. $embyItem['originalImage'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['imageType'] . '&img=' . $embyItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $embyItem['key'] . '$' . $this->randString();
  304. $embyItem['openTab'] = $this->config['embyTabURL'] && $this->config['embyTabName'] ? true : false;
  305. $embyItem['tabName'] = $this->config['embyTabName'] ? $this->config['embyTabName'] : '';
  306. // Stream info
  307. $embyItem['userStream'] = array(
  308. 'platform' => @(string)$itemDetails['Client'],
  309. 'product' => @(string)$itemDetails['Client'],
  310. 'device' => @(string)$itemDetails['DeviceName'],
  311. 'stream' => @$itemDetails['PlayState']['PlayMethod'],
  312. 'videoResolution' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]['Width']) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Width'] : '',
  313. 'throttled' => false,
  314. 'sourceVideoCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : '',
  315. 'videoCodec' => @$itemDetails['TranscodingInfo']['VideoCodec'],
  316. 'audioCodec' => @$itemDetails['TranscodingInfo']['AudioCodec'],
  317. 'sourceAudioCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][1]) ? $itemDetails['NowPlayingItem']['MediaStreams'][1]['Codec'] : (isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : ''),
  318. 'videoDecision' => $this->streamType(@$itemDetails['PlayState']['PlayMethod']),
  319. 'audioDecision' => $this->streamType(@$itemDetails['PlayState']['PlayMethod']),
  320. 'container' => isset($itemDetails['NowPlayingItem']['Container']) ? $itemDetails['NowPlayingItem']['Container'] : '',
  321. 'audioChannels' => @$itemDetails['TranscodingInfo']['AudioChannels']
  322. );
  323. // Genre catch all
  324. if (isset($item['Genres'])) {
  325. $genres = array();
  326. foreach ($item['Genres'] as $genre) {
  327. $genres[] = $genre;
  328. }
  329. }
  330. // Actor catch all
  331. if (isset($item['People'])) {
  332. $actors = array();
  333. foreach ($item['People'] as $key => $value) {
  334. if (@$value['PrimaryImageTag'] && @$value['Role']) {
  335. if (file_exists($cacheDirectory . (string)$value['Id'] . '-cast.jpg')) {
  336. $actorImage = $cacheDirectoryWeb . (string)$value['Id'] . '-cast.jpg';
  337. }
  338. 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')) {
  339. $actorImage = 'api/v2/homepage/image?source=emby&type=Primary&img=' . (string)$value['Id'] . '&height=' . $actorHeight . '&width=' . $actorWidth . '&key=' . (string)$value['Id'] . '-cast';
  340. }
  341. $actors[] = array(
  342. 'name' => (string)$value['Name'],
  343. 'role' => (string)$value['Role'],
  344. 'thumb' => $actorImage
  345. );
  346. }
  347. }
  348. }
  349. // Metadata information
  350. $embyItem['metadata'] = array(
  351. 'guid' => $item['Id'],
  352. 'summary' => @(string)$item['Overview'],
  353. 'rating' => @(string)$item['CommunityRating'],
  354. 'duration' => @(string)$item['RunTimeTicks'],
  355. 'originallyAvailableAt' => @(string)$item['PremiereDate'],
  356. 'year' => (string)isset($item['ProductionYear']) ? $item['ProductionYear'] : '',
  357. //'studio' => (string)$item['studio'],
  358. 'tagline' => @(string)$item['Taglines'][0],
  359. 'genres' => (isset($item['Genres'])) ? $genres : '',
  360. 'actors' => (isset($item['People'])) ? $actors : ''
  361. );
  362. if (file_exists($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg')) {
  363. $embyItem['nowPlayingImageURL'] = $cacheDirectoryWeb . $embyItem['nowPlayingKey'] . '.jpg';
  364. }
  365. if (file_exists($cacheDirectory . $embyItem['key'] . '.jpg')) {
  366. $embyItem['imageURL'] = $cacheDirectoryWeb . $embyItem['key'] . '.jpg';
  367. }
  368. if (file_exists($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg') || !file_exists($cacheDirectory . $embyItem['nowPlayingKey'] . '.jpg')) {
  369. $embyItem['nowPlayingImageURL'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['nowPlayingImageType'] . '&img=' . $embyItem['nowPlayingThumb'] . '&height=' . $nowPlayingHeight . '&width=' . $nowPlayingWidth . '&key=' . $embyItem['nowPlayingKey'] . '';
  370. }
  371. if (file_exists($cacheDirectory . $embyItem['key'] . '.jpg') && (time() - 604800) > filemtime($cacheDirectory . $embyItem['key'] . '.jpg') || !file_exists($cacheDirectory . $embyItem['key'] . '.jpg')) {
  372. $embyItem['imageURL'] = 'api/v2/homepage/image?source=emby&type=' . $embyItem['imageType'] . '&img=' . $embyItem['thumb'] . '&height=' . $height . '&width=' . $width . '&key=' . $embyItem['key'] . '';
  373. }
  374. if (!$embyItem['nowPlayingThumb']) {
  375. $embyItem['nowPlayingOriginalImage'] = $embyItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png";
  376. $embyItem['nowPlayingKey'] = "no-np";
  377. }
  378. if (!$embyItem['thumb']) {
  379. $embyItem['originalImage'] = $embyItem['imageURL'] = "plugins/images/cache/no-list.png";
  380. $embyItem['key'] = "no-list";
  381. }
  382. if (isset($useImage)) {
  383. $embyItem['useImage'] = $useImage;
  384. }
  385. return $embyItem;
  386. }
  387. public function getEmbyHomepageStreams()
  388. {
  389. if (!$this->config['homepageEmbyEnabled']) {
  390. $this->setAPIResponse('error', 'Emby homepage item is not enabled', 409);
  391. return false;
  392. }
  393. if (!$this->config['homepageEmbyStreams']) {
  394. $this->setAPIResponse('error', 'Emby homepage module is not enabled', 409);
  395. return false;
  396. }
  397. if (!$this->qualifyRequest($this->config['homepageEmbyAuth'])) {
  398. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  399. return false;
  400. }
  401. if (!$this->qualifyRequest($this->config['homepageEmbyStreamsAuth'])) {
  402. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  403. return false;
  404. }
  405. if (empty($this->config['embyURL'])) {
  406. $this->setAPIResponse('error', 'Emby URL is not defined', 422);
  407. return false;
  408. }
  409. if (empty($this->config['embyToken'])) {
  410. $this->setAPIResponse('error', 'Emby Token is not defined', 422);
  411. return false;
  412. }
  413. $url = $this->qualifyURL($this->config['embyURL']);
  414. $url = $url . '/Sessions?api_key=' . $this->config['embyToken'] . '&Fields=Overview,People,Genres,CriticRating,Studios,Taglines';
  415. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  416. try {
  417. $response = Requests::get($url, array(), $options);
  418. if ($response->success) {
  419. $items = array();
  420. $emby = json_decode($response->body, true);
  421. foreach ($emby as $child) {
  422. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  423. $items[] = $this->resolveEmbyItem($child);
  424. }
  425. }
  426. $api['content'] = array_filter($items);
  427. $this->setAPIResponse('success', null, 200, $api);
  428. return $api;
  429. } else {
  430. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  431. return false;
  432. }
  433. } catch (Requests_Exception $e) {
  434. $this->writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  435. $this->setAPIResponse('error', $e->getMessage(), 500);
  436. return false;
  437. }
  438. }
  439. public function getEmbyHomepageRecent()
  440. {
  441. if (!$this->config['homepageEmbyEnabled']) {
  442. $this->setAPIResponse('error', 'Emby homepage item is not enabled', 409);
  443. return false;
  444. }
  445. if (!$this->config['homepageEmbyRecent']) {
  446. $this->setAPIResponse('error', 'Emby homepage module is not enabled', 409);
  447. return false;
  448. }
  449. if (!$this->qualifyRequest($this->config['homepageEmbyAuth'])) {
  450. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  451. return false;
  452. }
  453. if (!$this->qualifyRequest($this->config['homepageEmbyRecentAuth'])) {
  454. $this->setAPIResponse('error', 'User not approved to view this homepage module', 401);
  455. return false;
  456. }
  457. if (empty($this->config['embyURL'])) {
  458. $this->setAPIResponse('error', 'Emby URL is not defined', 422);
  459. return false;
  460. }
  461. if (empty($this->config['embyToken'])) {
  462. $this->setAPIResponse('error', 'Emby Token is not defined', 422);
  463. return false;
  464. }
  465. $url = $this->qualifyURL($this->config['embyURL']);
  466. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  467. $username = false;
  468. $showPlayed = false;
  469. $userId = 0;
  470. try {
  471. if (isset($this->user['username'])) {
  472. $username = strtolower($this->user['username']);
  473. }
  474. // Get A User
  475. $userIds = $url . "/Users?api_key=" . $this->config['embyToken'];
  476. $response = Requests::get($userIds, array(), $options);
  477. if ($response->success) {
  478. $emby = json_decode($response->body, true);
  479. foreach ($emby as $value) { // Scan for admin user
  480. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  481. $userId = $value['Id'];
  482. }
  483. if ($username && strtolower($value['Name']) == $username) {
  484. $userId = $value['Id'];
  485. $showPlayed = false;
  486. break;
  487. }
  488. }
  489. $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';
  490. } else {
  491. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  492. return false;
  493. }
  494. $response = Requests::get($url, array(), $options);
  495. if ($response->success) {
  496. $items = array();
  497. $emby = json_decode($response->body, true);
  498. foreach ($emby as $child) {
  499. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  500. $items[] = $this->resolveEmbyItem($child);
  501. }
  502. }
  503. $api['content'] = array_filter($items);
  504. $this->setAPIResponse('success', null, 200, $api);
  505. return $api;
  506. } else {
  507. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  508. return false;
  509. }
  510. } catch (Requests_Exception $e) {
  511. $this->writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  512. $this->setAPIResponse('error', $e->getMessage(), 500);
  513. return false;
  514. }
  515. }
  516. public function getEmbyHomepageMetadata($array)
  517. {
  518. if (!$this->config['homepageEmbyEnabled']) {
  519. $this->setAPIResponse('error', 'Emby homepage item is not enabled', 409);
  520. return false;
  521. }
  522. if (!$this->qualifyRequest($this->config['homepageEmbyAuth'])) {
  523. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  524. return false;
  525. }
  526. if (empty($this->config['embyURL'])) {
  527. $this->setAPIResponse('error', 'Emby URL is not defined', 422);
  528. return false;
  529. }
  530. if (empty($this->config['embyToken'])) {
  531. $this->setAPIResponse('error', 'Emby Token is not defined', 422);
  532. return false;
  533. }
  534. $key = $array['key'] ?? null;
  535. if (!$key) {
  536. $this->setAPIResponse('error', 'Emby Metadata key is not defined', 422);
  537. return false;
  538. }
  539. $url = $this->qualifyURL($this->config['embyURL']);
  540. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  541. $username = false;
  542. $showPlayed = false;
  543. $userId = 0;
  544. try {
  545. if (isset($this->user['username'])) {
  546. $username = strtolower($this->user['username']);
  547. }
  548. // Get A User
  549. $userIds = $url . "/Users?api_key=" . $this->config['embyToken'];
  550. $response = Requests::get($userIds, array(), $options);
  551. if ($response->success) {
  552. $emby = json_decode($response->body, true);
  553. foreach ($emby as $value) { // Scan for admin user
  554. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  555. $userId = $value['Id'];
  556. }
  557. if ($username && strtolower($value['Name']) == $username) {
  558. $userId = $value['Id'];
  559. $showPlayed = false;
  560. break;
  561. }
  562. }
  563. $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';
  564. } else {
  565. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  566. return false;
  567. }
  568. $response = Requests::get($url, array(), $options);
  569. if ($response->success) {
  570. $items = array();
  571. $emby = json_decode($response->body, true);
  572. if (isset($emby['NowPlayingItem']) || isset($emby['Name'])) {
  573. $items[] = $this->resolveEmbyItem($emby);
  574. }
  575. $api['content'] = array_filter($items);
  576. $this->setAPIResponse('success', null, 200, $api);
  577. return $api;
  578. } else {
  579. $this->setAPIResponse('error', 'Emby Error Occurred', 500);
  580. return false;
  581. }
  582. } catch (Requests_Exception $e) {
  583. $this->writeLog('error', 'Emby Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  584. $this->setAPIResponse('error', $e->getMessage(), 500);
  585. return false;
  586. }
  587. }
  588. }