homepage-connect-functions.php 33 KB

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