homepage-connect-functions.php 29 KB

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