homepage-connect-functions.php 34 KB

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