homepage-connect-functions.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. function homepageConnect($array){
  3. switch ($array['data']['action']) {
  4. case 'getPlexStreams':
  5. return plexConnect('streams');
  6. break;
  7. case 'getPlexRecent':
  8. return plexConnect('recent');
  9. break;
  10. case 'getPlexMetadata':
  11. return plexConnect('metadata',$array['data']['key']);
  12. break;
  13. case 'getEmbyStreams':
  14. return embyConnect('streams');
  15. break;
  16. case 'getEmbyRecent':
  17. return embyConnect('recent');
  18. break;
  19. case 'getEmbyMetadata':
  20. return embyConnect('metadata',$array['data']['key'],true);
  21. break;
  22. default:
  23. # code...
  24. break;
  25. }
  26. }
  27. function streamType($value){
  28. if($value == "transcode" || $value == "Transcode"){
  29. return "Transcode";
  30. }elseif($value == "copy" || $value == "DirectStream"){
  31. return "Direct Stream";
  32. }elseif($value == "directplay" || $value == "DirectPlay"){
  33. return "Direct Play";
  34. }else{
  35. return "Direct Play";
  36. }
  37. }
  38. function resolveEmbyItem($itemDetails) {
  39. // Grab Each item info from Emby (extra call)
  40. $id = isset($itemDetails['NowPlayingItem']['Id']) ? $itemDetails['NowPlayingItem']['Id'] : $itemDetails['Id'];
  41. $url = qualifyURL($GLOBALS['embyURL']);
  42. $url = $url.'/Items?Ids='.$id.'&api_key='.$GLOBALS['embyToken'].'&Fields=Overview,People,Genres,CriticRating,Studios,Taglines';
  43. try{
  44. $options = (localURL($url)) ? array('verify' => false ) : array();
  45. $response = Requests::get($url, array(), $options);
  46. if($response->success){
  47. $item = json_decode($response->body,true)['Items'][0];
  48. }
  49. }catch( Requests_Exception $e ) {
  50. return false;
  51. };
  52. // Static Height & Width
  53. $height = 300;
  54. $width = 200;
  55. $nowPlayingHeight = 675;
  56. $nowPlayingWidth = 1200;
  57. $actorHeight = 450;
  58. $actorWidth = 300;
  59. $widthOverride = 100;
  60. // Cache Directories
  61. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  62. $cacheDirectoryWeb = 'plugins/images/cache/';
  63. // Types
  64. $embyItem['array-item'] = $item;
  65. $embyItem['array-itemdetails'] = $itemDetails;
  66. switch (@$item['Type']) {
  67. case 'Series':
  68. $embyItem['type'] = 'tv';
  69. $embyItem['title'] = $item['Name'];
  70. $embyItem['summary'] = '';
  71. $embyItem['ratingKey'] = $item['Id'];
  72. $embyItem['thumb'] = $item['Id'];
  73. $embyItem['key'] = $item['Id'] . "-list";
  74. $embyItem['nowPlayingThumb'] = $item['Id'];
  75. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  76. $embyItem['metadataKey'] = $item['Id'];
  77. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['BackdropImageTags'][0]) ? 'Backdrop' : '');
  78. break;
  79. case 'Episode':
  80. $embyItem['type'] = 'tv';
  81. $embyItem['title'] = $item['SeriesName'];
  82. $embyItem['summary'] = '';
  83. $embyItem['ratingKey'] = $item['Id'];
  84. $embyItem['thumb'] = (isset($item['SeriesId'])?$item['SeriesId']:$item['Id']);
  85. $embyItem['key'] = (isset($item['SeriesId'])?$item['SeriesId']:$item['Id']) . "-list";
  86. $embyItem['nowPlayingThumb'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'] : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'] : false);
  87. $embyItem['nowPlayingKey'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'].'-np' : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'].'-np' : false);
  88. $embyItem['metadataKey'] = $item['Id'];
  89. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['ParentBackdropImageTags'][0]) ? 'Backdrop' : '');
  90. $embyItem['nowPlayingTitle'] = @$item['SeriesName'].' - '.@$item['Name'];
  91. $embyItem['nowPlayingBottom'] = 'S'.@$item['ParentIndexNumber'].' · E'.@$item['IndexNumber'];
  92. break;
  93. case 'MusicAlbum':
  94. case 'Audio':
  95. $embyItem['type'] = 'music';
  96. $embyItem['title'] = $item['Name'];
  97. $embyItem['summary'] = '';
  98. $embyItem['ratingKey'] = $item['Id'];
  99. $embyItem['thumb'] = $item['Id'];
  100. $embyItem['key'] = $item['Id'] . "-list";
  101. $embyItem['nowPlayingThumb'] = (isset($item['AlbumId']) ? $item['AlbumId'] : @$item['ParentBackdropItemId']);
  102. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  103. $embyItem['metadataKey'] = isset($item['AlbumId']) ? $item['AlbumId'] : $item['Id'];
  104. $embyItem['nowPlayingImageType'] = (isset($item['ParentBackdropItemId']) ? "Primary" : "Backdrop");
  105. $embyItem['nowPlayingTitle'] = @$item['AlbumArtist'].' - '.@$item['Name'];
  106. $embyItem['nowPlayingBottom'] = @$item['Album'];
  107. break;
  108. case 'Movie':
  109. $embyItem['type'] = 'movie';
  110. $embyItem['title'] = $item['Name'];
  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)$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'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? @(string)$itemDetails['UserName'] : "";
  139. $embyItem['userThumb'] = '';
  140. $embyItem['userAddress'] = "x.x.x.x";
  141. $embyItem['address'] = $GLOBALS['embyTabURL'] ? '' : '';
  142. $embyItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=emby&type='.$embyItem['nowPlayingImageType'].'&img='.$embyItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$embyItem['nowPlayingKey'].'$'.randString();
  143. $embyItem['originalImage'] = 'api/?v1/image&source=emby&type='.$embyItem['imageType'].'&img='.$embyItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$embyItem['key'].'$'.randString();
  144. $embyItem['openTab'] = $GLOBALS['embyTabURL'] && $GLOBALS['embyTabName'] ? true : false;
  145. $embyItem['tabName'] = $GLOBALS['embyTabName'] ? $GLOBALS['embyTabName'] : '';
  146. // Stream info
  147. $embyItem['userStream'] = array(
  148. 'platform' => @(string)$itemDetails['Client'],
  149. 'product' => @(string)$itemDetails['Client'],
  150. 'device' => @(string)$itemDetails['DeviceName'],
  151. 'stream' => @$itemDetails['PlayState']['PlayMethod'],
  152. 'videoResolution' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]['Width']) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Width'] : '',
  153. 'throttled' => false,
  154. 'sourceVideoCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : '',
  155. 'videoCodec' => @$itemDetails['TranscodingInfo']['VideoCodec'],
  156. 'audioCodec' => @$itemDetails['TranscodingInfo']['AudioCodec'],
  157. 'sourceAudioCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][1]) ? $itemDetails['NowPlayingItem']['MediaStreams'][1]['Codec'] : (isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : ''),
  158. 'videoDecision' => streamType(@$itemDetails['PlayState']['PlayMethod']),
  159. 'audioDecision' => streamType(@$itemDetails['PlayState']['PlayMethod']),
  160. 'container' => isset($itemDetails['NowPlayingItem']['Container']) ? $itemDetails['NowPlayingItem']['Container'] : '',
  161. 'audioChannels' => @$itemDetails['TranscodingInfo']['AudioChannels']
  162. );
  163. // Genre catch all
  164. if($item['Genres']){
  165. $genres = array();
  166. foreach ($item['Genres'] as $genre) {
  167. $genres[] = $genre;
  168. }
  169. }
  170. // Actor catch all
  171. if($item['People'] ){
  172. $actors = array();
  173. foreach ($item['People'] as $key => $value) {
  174. if(@$value['PrimaryImageTag'] && @$value['Role']){
  175. if (file_exists($cacheDirectory.(string)$value['Id'].'-cast.jpg')){ $actorImage = $cacheDirectoryWeb.(string)$value['Id'].'-cast.jpg'; }
  176. 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')) {
  177. $actorImage = 'api/?v1/image&source=emby&type=Primary&img='.(string)$value['Id'].'&height='.$actorHeight.'&width='.$actorWidth.'&key='.(string)$value['Id'].'-cast';
  178. }
  179. $actors[] = array(
  180. 'name' => (string)$value['Name'],
  181. 'role' => (string)$value['Role'],
  182. 'thumb' => $actorImage
  183. );
  184. }
  185. }
  186. }
  187. // Metadata information
  188. $embyItem['metadata'] = array(
  189. 'guid' => $item['Id'],
  190. 'summary' => @(string)$item['Overview'],
  191. 'rating' => @(string)$item['CommunityRating'],
  192. 'duration' => @(string)$item['RunTimeTicks'],
  193. 'originallyAvailableAt' => @(string)$item['PremiereDate'],
  194. 'year' => (string)$item['ProductionYear'],
  195. //'studio' => (string)$item['studio'],
  196. 'tagline' => @(string)$item['Taglines'][0],
  197. 'genres' => ($item['Genres']) ? $genres : '',
  198. 'actors' => ($item['People']) ? $actors : ''
  199. );
  200. if (file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg')){ $embyItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$embyItem['nowPlayingKey'].'.jpg'; }
  201. if (file_exists($cacheDirectory.$embyItem['key'].'.jpg')){ $embyItem['imageURL'] = $cacheDirectoryWeb.$embyItem['key'].'.jpg'; }
  202. if (file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg')) {
  203. $embyItem['nowPlayingImageURL'] = 'api/?v1/image&source=emby&type='.$embyItem['nowPlayingImageType'].'&img='.$embyItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$embyItem['nowPlayingKey'].'';
  204. }
  205. if (file_exists($cacheDirectory.$embyItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$embyItem['key'].'.jpg') || !file_exists($cacheDirectory.$embyItem['key'].'.jpg')) {
  206. $embyItem['imageURL'] = 'api/?v1/image&source=emby&type='.$embyItem['imageType'].'&img='.$embyItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$embyItem['key'].'';
  207. }
  208. if(!$embyItem['nowPlayingThumb'] ){ $embyItem['nowPlayingOriginalImage'] = $embyItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png"; $embyItem['nowPlayingKey'] = "no-np"; }
  209. if(!$embyItem['thumb'] ){ $embyItem['originalImage'] = $embyItem['imageURL'] = "plugins/images/cache/no-list.png"; $embyItem['key'] = "no-list"; }
  210. if(isset($useImage)){ $embyItem['useImage'] = $useImage; }
  211. return $embyItem;
  212. }
  213. function resolvePlexItem($item) {
  214. // Static Height & Width
  215. $height = 300;
  216. $width = 200;
  217. $nowPlayingHeight = 675;
  218. $nowPlayingWidth = 1200;
  219. $widthOverride = 100;
  220. // Cache Directories
  221. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  222. $cacheDirectoryWeb = 'plugins/images/cache/';
  223. // Types
  224. switch ($item['type']) {
  225. case 'season':
  226. $plexItem['type'] = 'tv';
  227. $plexItem['title'] = (string)$item['parentTitle'];
  228. $plexItem['summary'] = (string)$item['parentSummary'];
  229. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  230. $plexItem['thumb'] = (string)$item['thumb'];
  231. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  232. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  233. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  234. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  235. break;
  236. case 'episode':
  237. $plexItem['type'] = 'tv';
  238. $plexItem['title'] = (string)$item['grandparentTitle'];
  239. $plexItem['summary'] = (string)$item['title'];
  240. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  241. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  242. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  243. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  244. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  245. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  246. $plexItem['nowPlayingBottom'] = 'S'.(string)$item['parentIndex'].' · E'.(string)$item['index'];
  247. $plexItem['metadataKey'] = (string)$item['grandparentRatingKey'];
  248. break;
  249. case 'clip':
  250. $useImage = (isset($item['live']) ? "plugins/images/cache/livetv.png" : null);
  251. $plexItem['type'] = 'clip';
  252. $plexItem['title'] = (string)$item['title'];
  253. $plexItem['summary'] = (string)$item['summary'];
  254. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  255. $plexItem['thumb'] = (string)$item['thumb'];
  256. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  257. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  258. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  259. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  260. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  261. break;
  262. case 'album':
  263. case 'track':
  264. $plexItem['type'] = 'music';
  265. $plexItem['title'] = (string)$item['parentTitle'];
  266. $plexItem['summary'] = (string)$item['title'];
  267. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  268. $plexItem['thumb'] = (string)$item['thumb'];
  269. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  270. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  271. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  272. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  273. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  274. $plexItem['metadataKey'] = isset($item['grandparentRatingKey']) ? (string)$item['grandparentRatingKey'] : (string)$item['parentRatingKey'];
  275. break;
  276. default:
  277. $plexItem['type'] = 'movie';
  278. $plexItem['title'] = (string)$item['title'];
  279. $plexItem['summary'] = (string)$item['summary'];
  280. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  281. $plexItem['thumb'] = (string)$item['thumb'];
  282. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  283. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  284. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  285. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  286. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  287. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  288. }
  289. $plexItem['uid'] = (string)$item['ratingKey'];
  290. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  291. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  292. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  293. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress']- $plexItem['watched']) : '';
  294. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision']: '';
  295. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  296. $plexItem['session'] = (string)$item->Session['id'];
  297. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  298. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  299. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  300. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  301. $plexItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['title'] : "";
  302. $plexItem['userThumb'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['thumb'] : "";
  303. $plexItem['userAddress'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->Player['address'] : "x.x.x.x";
  304. $plexItem['address'] = $GLOBALS['plexTabURL'] ? $GLOBALS['plexTabURL']."/web/index.html#!/server/".$GLOBALS['plexID']."/details?key=/library/metadata/".$item['ratingKey'] : "https://app.plex.tv/web/app#!/server/".$GLOBALS['plexID']."/details?key=/library/metadata/".$item['ratingKey'];
  305. $plexItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'$'.randString();
  306. $plexItem['originalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'$'.randString();
  307. $plexItem['openTab'] = $GLOBALS['plexTabURL'] && $GLOBALS['plexTabName'] ? true : false;
  308. $plexItem['tabName'] = $GLOBALS['plexTabName'] ? $GLOBALS['plexTabName'] : '';
  309. // Stream info
  310. $plexItem['userStream'] = array(
  311. 'platform' => (string)$item->Player['platform'],
  312. 'product' => (string)$item->Player['product'],
  313. 'device' => (string)$item->Player['device'],
  314. 'stream' => (string)$item->Media->Part['decision'].($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)': ''),
  315. 'videoResolution' => (string)$item->Media['videoResolution'],
  316. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  317. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  318. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  319. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  320. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  321. 'videoDecision' => streamType((string)$item->TranscodeSession['videoDecision']),
  322. 'audioDecision' => streamType((string)$item->TranscodeSession['audioDecision']),
  323. 'container' => (string)$item->TranscodeSession['container'],
  324. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  325. );
  326. // Genre catch all
  327. if($item->Genre){
  328. $genres = array();
  329. foreach ($item->Genre as $key => $value) {
  330. $genres[] = (string)$value['tag'];
  331. }
  332. }
  333. // Actor catch all
  334. if($item->Role ){
  335. $actors = array();
  336. foreach ($item->Role as $key => $value) {
  337. if($value['thumb']){
  338. $actors[] = array(
  339. 'name' => (string)$value['tag'],
  340. 'role' => (string)$value['role'],
  341. 'thumb' => (string)$value['thumb']
  342. );
  343. }
  344. }
  345. }
  346. // Metadata information
  347. $plexItem['metadata'] = array(
  348. 'guid' => (string)$item['guid'],
  349. 'summary' => (string)$item['summary'],
  350. 'rating' => (string)$item['rating'],
  351. 'duration' => (string)$item['duration'],
  352. 'originallyAvailableAt' => (string)$item['originallyAvailableAt'],
  353. 'year' => (string)$item['year'],
  354. 'studio' => (string)$item['studio'],
  355. 'tagline' => (string)$item['tagline'],
  356. 'genres' => ($item->Genre) ? $genres : '',
  357. 'actors' => ($item->Role) ? $actors : ''
  358. );
  359. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')){ $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$plexItem['nowPlayingKey'].'.jpg'; }
  360. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg')){ $plexItem['imageURL'] = $cacheDirectoryWeb.$plexItem['key'].'.jpg'; }
  361. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')) {
  362. $plexItem['nowPlayingImageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'';
  363. }
  364. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['key'].'.jpg') || !file_exists($cacheDirectory.$plexItem['key'].'.jpg')) {
  365. $plexItem['imageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'';
  366. }
  367. if(!$plexItem['nowPlayingThumb'] ){ $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png"; $plexItem['nowPlayingKey'] = "no-np"; }
  368. if(!$plexItem['thumb'] ){ $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/cache/no-list.png"; $plexItem['key'] = "no-list"; }
  369. if(isset($useImage)){ $plexItem['useImage'] = $useImage; }
  370. return $plexItem;
  371. }
  372. function plexConnect($action,$key=null){
  373. if($GLOBALS['homepagePlexEnabled'] && !empty($GLOBALS['plexURL']) && !empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'] && qualifyRequest($GLOBALS['homepagePlexAuth']))){
  374. $url = qualifyURL($GLOBALS['plexURL']);
  375. switch ($action) {
  376. case 'streams':
  377. $url = $url."/status/sessions?X-Plex-Token=".$GLOBALS['plexToken'];
  378. break;
  379. case 'recent':
  380. $url = $url."/library/recentlyAdded?X-Plex-Token=".$GLOBALS['plexToken'];
  381. break;
  382. case 'metadata':
  383. $url = $url."/library/metadata/".$key."?X-Plex-Token=".$GLOBALS['plexToken'];
  384. break;
  385. default:
  386. # code...
  387. break;
  388. }
  389. try{
  390. $options = (localURL($url)) ? array('verify' => false ) : array();
  391. $response = Requests::get($url, array(), $options);
  392. libxml_use_internal_errors(true);
  393. if($response->success){
  394. $items = array();
  395. $plex = simplexml_load_string($response->body);
  396. foreach($plex AS $child) {
  397. $items[] = resolvePlexItem($child);
  398. }
  399. $api['content'] = $items;
  400. $api['plexID'] = $GLOBALS['plexID'];
  401. $api['showNames'] = true;
  402. $api['group'] = '1';
  403. return $api;
  404. }
  405. }catch( Requests_Exception $e ) {
  406. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  407. };
  408. }
  409. return false;
  410. }
  411. function embyConnect($action,$key=null,$skip=false){
  412. if($GLOBALS['homepageEmbyEnabled'] && !empty($GLOBALS['embyURL']) && !empty($GLOBALS['embyToken']) && qualifyRequest($GLOBALS['homepageEmbyAuth'])){
  413. $url = qualifyURL($GLOBALS['embyURL']);
  414. switch ($action) {
  415. case 'streams':
  416. $url = $url.'/Sessions?api_key='.$GLOBALS['embyToken'];
  417. break;
  418. case 'recent':
  419. $username = false;
  420. if (isset($GLOBALS['organizrUser']['username'])) {
  421. $username = strtolower($GLOBALS['organizrUser']['username']);
  422. }
  423. // Get A User
  424. $userIds = $url."/Users?api_key=".$GLOBALS['embyToken'];
  425. $showPlayed = true;
  426. try{
  427. $options = (localURL($userIds)) ? array('verify' => false ) : array();
  428. $response = Requests::get($userIds, array(), $options);
  429. if($response->success){
  430. $emby = json_decode($response->body, true);
  431. foreach ($emby as $value) { // Scan for admin user
  432. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  433. $userId = $value['Id'];
  434. }
  435. if ($username && strtolower($value['Name']) == $username) {
  436. $userId = $value['Id'];
  437. $showPlayed = false;
  438. break;
  439. }
  440. }
  441. }
  442. }catch( Requests_Exception $e ) {
  443. writeLog('error', 'Emby Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  444. };
  445. $url = $url.'/Users/'.$userId.'/Items/Latest?EnableImages=false&Limit=100&api_key='.$GLOBALS['embyToken'].($showPlayed?'':'&IsPlayed=false');
  446. break;
  447. case 'metadata':
  448. $skip = true;
  449. break;
  450. default:
  451. # code...
  452. break;
  453. }
  454. if($skip && $key){
  455. $items[] = resolveEmbyItem(array('Id'=>$key));
  456. $api['content'] = $items;
  457. return $api;
  458. }
  459. try{
  460. $options = (localURL($url)) ? array('verify' => false ) : array();
  461. $response = Requests::get($url, array(), $options);
  462. if($response->success){
  463. $items = array();
  464. $emby = json_decode($response->body, true);
  465. foreach($emby AS $child) {
  466. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  467. $items[] = resolveEmbyItem($child);
  468. }
  469. }
  470. $api['content'] = $items;
  471. return $api;
  472. }
  473. }catch( Requests_Exception $e ) {
  474. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  475. };
  476. }
  477. return false;
  478. }