homepage-connect-functions.php 30 KB

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