homepage-connect-functions.php 25 KB

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