4
0

homepage-connect-functions.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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'] = $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. //Stream
  124. switch ($item['NowPlayingItem']['Type']) {
  125. case 'Episode':
  126. # code...
  127. break;
  128. default:
  129. # code...
  130. break;
  131. }
  132. }
  133. $embyItem['uid'] = $item['Id'];
  134. $embyItem['imageType'] = (isset($item['ImageTags']['Primary']) ? "Primary" : false);
  135. $embyItem['elapsed'] = isset($itemDetails['PlayState']['PositionTicks']) && $itemDetails['PlayState']['PositionTicks'] !== '0' ? (int)$itemDetails['PlayState']['PositionTicks'] : null;
  136. $embyItem['duration'] = isset($itemDetails['NowPlayingItem']['RunTimeTicks']) ? (int)$itemDetails['NowPlayingItem']['RunTimeTicks'] : (int)$item['RunTimeTicks'];
  137. $embyItem['watched'] = ($embyItem['elapsed'] && $embyItem['duration'] ? floor(($embyItem['elapsed'] / $embyItem['duration']) * 100) : 0);
  138. $embyItem['transcoded'] = isset($itemDetails['TranscodingInfo']['CompletionPercentage']) ? floor((int)$itemDetails['TranscodingInfo']['CompletionPercentage']) : 100;
  139. $embyItem['stream'] = @$itemDetails['PlayState']['PlayMethod'];
  140. $embyItem['id'] = $item['ServerId'];
  141. $embyItem['session'] = @$itemDetails['DeviceId'];
  142. $embyItem['bandwidth'] = isset($itemDetails['TranscodingInfo']['Bitrate']) ? $itemDetails['TranscodingInfo']['Bitrate'] / 1000 : '';
  143. $embyItem['bandwidthType'] = 'wan';
  144. $embyItem['sessionType'] = (@$itemDetails['PlayState']['PlayMethod'] == 'Transcode') ? 'Transcoding' : 'Direct Playing';
  145. $embyItem['state'] = ((@(string)$itemDetails['PlayState']['IsPaused'] == '1') ? "pause" : "play");
  146. $embyItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? @(string)$itemDetails['UserName'] : "";
  147. $embyItem['userThumb'] = '';
  148. $embyItem['userAddress'] = "x.x.x.x";
  149. $embyItem['address'] = $GLOBALS['embyTabURL'] ? '' : '';
  150. $embyItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=emby&type='.$embyItem['nowPlayingImageType'].'&img='.$embyItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$embyItem['nowPlayingKey'].'$'.randString();
  151. $embyItem['originalImage'] = 'api/?v1/image&source=emby&type='.$embyItem['imageType'].'&img='.$embyItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$embyItem['key'].'$'.randString();
  152. $embyItem['openTab'] = $GLOBALS['embyTabURL'] && $GLOBALS['embyTabName'] ? true : false;
  153. $embyItem['tabName'] = $GLOBALS['embyTabName'] ? $GLOBALS['embyTabName'] : '';
  154. // Stream info
  155. $embyItem['userStream'] = array(
  156. 'platform' => @(string)$itemDetails['Client'],
  157. 'product' => @(string)$itemDetails['Client'],
  158. 'device' => @(string)$itemDetails['DeviceName'],
  159. 'stream' => @$itemDetails['PlayState']['PlayMethod'],
  160. 'videoResolution' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]['Width']) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Width'] : '',
  161. 'throttled' => false,
  162. 'sourceVideoCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : '',
  163. 'videoCodec' => @$itemDetails['TranscodingInfo']['VideoCodec'],
  164. 'audioCodec' => @$itemDetails['TranscodingInfo']['AudioCodec'],
  165. 'sourceAudioCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][1]) ? $itemDetails['NowPlayingItem']['MediaStreams'][1]['Codec'] : (isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : ''),
  166. 'videoDecision' => streamType(@$itemDetails['PlayState']['PlayMethod']),
  167. 'audioDecision' => streamType(@$itemDetails['PlayState']['PlayMethod']),
  168. 'container' => isset($itemDetails['NowPlayingItem']['Container']) ? $itemDetails['NowPlayingItem']['Container'] : '',
  169. 'audioChannels' => @$itemDetails['TranscodingInfo']['AudioChannels']
  170. );
  171. // Genre catch all
  172. if($item['Genres']){
  173. $genres = array();
  174. foreach ($item['Genres'] as $genre) {
  175. $genres[] = $genre;
  176. }
  177. }
  178. // Actor catch all
  179. if($item['People'] ){
  180. $actors = array();
  181. foreach ($item['People'] as $key => $value) {
  182. if(@$value['PrimaryImageTag'] && @$value['Role']){
  183. if (file_exists($cacheDirectory.(string)$value['Id'].'-cast.jpg')){ $actorImage = $cacheDirectoryWeb.(string)$value['Id'].'-cast.jpg'; }
  184. 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')) {
  185. $actorImage = 'api/?v1/image&source=emby&type=Primary&img='.(string)$value['Id'].'&height='.$actorHeight.'&width='.$actorWidth.'&key='.(string)$value['Id'].'-cast';
  186. }
  187. $actors[] = array(
  188. 'name' => (string)$value['Name'],
  189. 'role' => (string)$value['Role'],
  190. 'thumb' => $actorImage
  191. );
  192. }
  193. }
  194. }
  195. // Metadata information
  196. $embyItem['metadata'] = array(
  197. 'guid' => $item['Id'],
  198. 'summary' => @(string)$item['Overview'],
  199. 'rating' => @(string)$item['CommunityRating'],
  200. 'duration' => @(string)$item['RunTimeTicks'],
  201. 'originallyAvailableAt' => @(string)$item['PremiereDate'],
  202. 'year' => (string)$item['ProductionYear'],
  203. //'studio' => (string)$item['studio'],
  204. 'tagline' => @(string)$item['Taglines'][0],
  205. 'genres' => ($item['Genres']) ? $genres : '',
  206. 'actors' => ($item['People']) ? $actors : ''
  207. );
  208. if (file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg')){ $embyItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$embyItem['nowPlayingKey'].'.jpg'; }
  209. if (file_exists($cacheDirectory.$embyItem['key'].'.jpg')){ $embyItem['imageURL'] = $cacheDirectoryWeb.$embyItem['key'].'.jpg'; }
  210. if (file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg')) {
  211. $embyItem['nowPlayingImageURL'] = 'api/?v1/image&source=emby&type='.$embyItem['nowPlayingImageType'].'&img='.$embyItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$embyItem['nowPlayingKey'].'';
  212. }
  213. if (file_exists($cacheDirectory.$embyItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$embyItem['key'].'.jpg') || !file_exists($cacheDirectory.$embyItem['key'].'.jpg')) {
  214. $embyItem['imageURL'] = 'api/?v1/image&source=emby&type='.$embyItem['imageType'].'&img='.$embyItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$embyItem['key'].'';
  215. }
  216. if(!$embyItem['nowPlayingThumb'] ){ $embyItem['nowPlayingOriginalImage'] = $embyItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png"; $embyItem['nowPlayingKey'] = "no-np"; }
  217. if(!$embyItem['thumb'] ){ $embyItem['originalImage'] = $embyItem['imageURL'] = "plugins/images/cache/no-list.png"; $embyItem['key'] = "no-list"; }
  218. if(isset($useImage)){ $embyItem['useImage'] = $useImage; }
  219. return $embyItem;
  220. }
  221. function resolvePlexItem($item) {
  222. // Static Height & Width
  223. $height = 300;
  224. $width = 200;
  225. $nowPlayingHeight = 675;
  226. $nowPlayingWidth = 1200;
  227. $widthOverride = 100;
  228. // Cache Directories
  229. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  230. $cacheDirectoryWeb = 'plugins/images/cache/';
  231. // Types
  232. switch ($item['type']) {
  233. case 'season':
  234. $plexItem['type'] = 'tv';
  235. $plexItem['title'] = (string)$item['parentTitle'];
  236. $plexItem['summary'] = (string)$item['parentSummary'];
  237. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  238. $plexItem['thumb'] = (string)$item['thumb'];
  239. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  240. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  241. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  242. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  243. break;
  244. case 'episode':
  245. $plexItem['type'] = 'tv';
  246. $plexItem['title'] = (string)$item['grandparentTitle'];
  247. $plexItem['summary'] = (string)$item['title'];
  248. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  249. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  250. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  251. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  252. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  253. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  254. $plexItem['nowPlayingBottom'] = 'S'.(string)$item['parentIndex'].' · E'.(string)$item['index'];
  255. $plexItem['metadataKey'] = (string)$item['grandparentRatingKey'];
  256. break;
  257. case 'clip':
  258. $useImage = (isset($item['live']) ? "plugins/images/cache/livetv.png" : null);
  259. $plexItem['type'] = 'clip';
  260. $plexItem['title'] = (string)$item['title'];
  261. $plexItem['summary'] = (string)$item['summary'];
  262. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  263. $plexItem['thumb'] = (string)$item['thumb'];
  264. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  265. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  266. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  267. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  268. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  269. break;
  270. case 'album':
  271. case 'track':
  272. $plexItem['type'] = 'music';
  273. $plexItem['title'] = (string)$item['parentTitle'];
  274. $plexItem['summary'] = (string)$item['title'];
  275. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  276. $plexItem['thumb'] = (string)$item['thumb'];
  277. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  278. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  279. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  280. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  281. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  282. $plexItem['metadataKey'] = isset($item['grandparentRatingKey']) ? (string)$item['grandparentRatingKey'] : (string)$item['parentRatingKey'];
  283. break;
  284. default:
  285. $plexItem['type'] = 'movie';
  286. $plexItem['title'] = (string)$item['title'];
  287. $plexItem['summary'] = (string)$item['summary'];
  288. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  289. $plexItem['thumb'] = (string)$item['thumb'];
  290. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  291. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  292. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  293. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  294. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  295. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  296. }
  297. $plexItem['uid'] = (string)$item['ratingKey'];
  298. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  299. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  300. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  301. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress']- $plexItem['watched']) : '';
  302. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision']: '';
  303. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  304. $plexItem['session'] = (string)$item->Session['id'];
  305. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  306. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  307. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  308. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  309. $plexItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['title'] : "";
  310. $plexItem['userThumb'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['thumb'] : "";
  311. $plexItem['userAddress'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->Player['address'] : "x.x.x.x";
  312. $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'];
  313. $plexItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'$'.randString();
  314. $plexItem['originalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'$'.randString();
  315. $plexItem['openTab'] = $GLOBALS['plexTabURL'] && $GLOBALS['plexTabName'] ? true : false;
  316. $plexItem['tabName'] = $GLOBALS['plexTabName'] ? $GLOBALS['plexTabName'] : '';
  317. // Stream info
  318. $plexItem['userStream'] = array(
  319. 'platform' => (string)$item->Player['platform'],
  320. 'product' => (string)$item->Player['product'],
  321. 'device' => (string)$item->Player['device'],
  322. 'stream' => (string)$item->Media->Part['decision'].($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)': ''),
  323. 'videoResolution' => (string)$item->Media['videoResolution'],
  324. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  325. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  326. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  327. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  328. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  329. 'videoDecision' => streamType((string)$item->TranscodeSession['videoDecision']),
  330. 'audioDecision' => streamType((string)$item->TranscodeSession['audioDecision']),
  331. 'container' => (string)$item->TranscodeSession['container'],
  332. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  333. );
  334. // Genre catch all
  335. if($item->Genre){
  336. $genres = array();
  337. foreach ($item->Genre as $key => $value) {
  338. $genres[] = (string)$value['tag'];
  339. }
  340. }
  341. // Actor catch all
  342. if($item->Role ){
  343. $actors = array();
  344. foreach ($item->Role as $key => $value) {
  345. if($value['thumb']){
  346. $actors[] = array(
  347. 'name' => (string)$value['tag'],
  348. 'role' => (string)$value['role'],
  349. 'thumb' => (string)$value['thumb']
  350. );
  351. }
  352. }
  353. }
  354. // Metadata information
  355. $plexItem['metadata'] = array(
  356. 'guid' => (string)$item['guid'],
  357. 'summary' => (string)$item['summary'],
  358. 'rating' => (string)$item['rating'],
  359. 'duration' => (string)$item['duration'],
  360. 'originallyAvailableAt' => (string)$item['originallyAvailableAt'],
  361. 'year' => (string)$item['year'],
  362. 'studio' => (string)$item['studio'],
  363. 'tagline' => (string)$item['tagline'],
  364. 'genres' => ($item->Genre) ? $genres : '',
  365. 'actors' => ($item->Role) ? $actors : ''
  366. );
  367. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')){ $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$plexItem['nowPlayingKey'].'.jpg'; }
  368. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg')){ $plexItem['imageURL'] = $cacheDirectoryWeb.$plexItem['key'].'.jpg'; }
  369. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')) {
  370. $plexItem['nowPlayingImageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'';
  371. }
  372. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['key'].'.jpg') || !file_exists($cacheDirectory.$plexItem['key'].'.jpg')) {
  373. $plexItem['imageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'';
  374. }
  375. if(!$plexItem['nowPlayingThumb'] ){ $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png"; $plexItem['nowPlayingKey'] = "no-np"; }
  376. if(!$plexItem['thumb'] ){ $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/cache/no-list.png"; $plexItem['key'] = "no-list"; }
  377. if(isset($useImage)){ $plexItem['useImage'] = $useImage; }
  378. return $plexItem;
  379. }
  380. function plexConnect($action,$key=null){
  381. if(!empty($GLOBALS['plexURL']) && !empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'] && qualifyRequest($GLOBALS['homepagePlexAuth']))){
  382. $url = qualifyURL($GLOBALS['plexURL']);
  383. switch ($action) {
  384. case 'streams':
  385. $url = $url."/status/sessions?X-Plex-Token=".$GLOBALS['plexToken'];
  386. break;
  387. case 'recent':
  388. $url = $url."/library/recentlyAdded?X-Plex-Token=".$GLOBALS['plexToken'];
  389. break;
  390. case 'metadata':
  391. $url = $url."/library/metadata/".$key."?X-Plex-Token=".$GLOBALS['plexToken'];
  392. break;
  393. default:
  394. # code...
  395. break;
  396. }
  397. try{
  398. $options = (localURL($url)) ? array('verify' => false ) : array();
  399. $response = Requests::get($url, array(), $options);
  400. libxml_use_internal_errors(true);
  401. if($response->success){
  402. $items = array();
  403. $plex = simplexml_load_string($response->body);
  404. foreach($plex AS $child) {
  405. $items[] = resolvePlexItem($child);
  406. }
  407. $api['content'] = $items;
  408. $api['plexID'] = $GLOBALS['plexID'];
  409. $api['showNames'] = true;
  410. $api['group'] = '1';
  411. return $api;
  412. }
  413. }catch( Requests_Exception $e ) {
  414. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  415. };
  416. }
  417. return false;
  418. }
  419. function embyConnect($action,$key=null,$skip=false){
  420. if(!empty($GLOBALS['embyURL']) && !empty($GLOBALS['embyToken']) && qualifyRequest($GLOBALS['homepageEmbyAuth'])){
  421. $url = qualifyURL($GLOBALS['embyURL']);
  422. switch ($action) {
  423. case 'streams':
  424. $url = $url.'/Sessions?api_key='.$GLOBALS['embyToken'];
  425. break;
  426. case 'recent':
  427. $username = false;
  428. if (isset($GLOBALS['organizrUser']['username'])) {
  429. $username = strtolower($GLOBALS['organizrUser']['username']);
  430. }
  431. // Get A User
  432. $userIds = $url."/Users?api_key=".$GLOBALS['embyToken'];
  433. $showPlayed = true;
  434. try{
  435. $options = (localURL($userIds)) ? array('verify' => false ) : array();
  436. $response = Requests::get($userIds, array(), $options);
  437. if($response->success){
  438. $emby = json_decode($response->body, true);
  439. foreach ($emby as $value) { // Scan for admin user
  440. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  441. $userId = $value['Id'];
  442. }
  443. if ($username && strtolower($value['Name']) == $username) {
  444. $userId = $value['Id'];
  445. $showPlayed = false;
  446. break;
  447. }
  448. }
  449. }
  450. }catch( Requests_Exception $e ) {
  451. writeLog('error', 'Emby Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  452. };
  453. $url = $url.'/Users/'.$userId.'/Items/Latest?EnableImages=false&Limit=100&api_key='.$GLOBALS['embyToken'].($showPlayed?'':'&IsPlayed=false');
  454. break;
  455. case 'metadata':
  456. $skip = true;
  457. break;
  458. default:
  459. # code...
  460. break;
  461. }
  462. if($skip && $key){
  463. $items[] = resolveEmbyItem(array('Id'=>$key));
  464. $api['content'] = $items;
  465. return $api;
  466. }
  467. try{
  468. $options = (localURL($url)) ? array('verify' => false ) : array();
  469. $response = Requests::get($url, array(), $options);
  470. if($response->success){
  471. $items = array();
  472. $emby = json_decode($response->body, true);
  473. foreach($emby AS $child) {
  474. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  475. $items[] = resolveEmbyItem($child);
  476. }
  477. }
  478. $api['content'] = $items;
  479. return $api;
  480. }
  481. }catch( Requests_Exception $e ) {
  482. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  483. };
  484. }
  485. return false;
  486. }