homepage-connect-functions.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 resolvePlexItem($item) {
  30. // Static Height & Width
  31. $height = 300;
  32. $width = 200;
  33. $nowPlayingHeight = 675;
  34. $nowPlayingWidth = 1200;
  35. $widthOverride = 100;
  36. // Cache Directories
  37. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  38. $cacheDirectoryWeb = 'plugins/images/cache/';
  39. // Types
  40. switch ($item['type']) {
  41. case 'season':
  42. $plexItem['type'] = 'tv';
  43. $plexItem['title'] = (string)$item['parentTitle'];
  44. $plexItem['summary'] = (string)$item['parentSummary'];
  45. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  46. $plexItem['thumb'] = (string)$item['thumb'];
  47. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  48. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  49. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  50. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  51. break;
  52. case 'episode':
  53. $plexItem['type'] = 'tv';
  54. $plexItem['title'] = (string)$item['grandparentTitle'];
  55. $plexItem['summary'] = (string)$item['title'];
  56. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  57. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  58. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  59. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  60. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  61. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  62. $plexItem['nowPlayingBottom'] = 'S'.(string)$item['parentIndex'].' · E'.(string)$item['index'];
  63. $plexItem['metadataKey'] = (string)$item['grandparentRatingKey'];
  64. break;
  65. case 'clip':
  66. $useImage = (isset($item['live']) ? "plugins/images/cache/livetv.png" : null);
  67. $plexItem['type'] = 'clip';
  68. $plexItem['title'] = (string)$item['title'];
  69. $plexItem['summary'] = (string)$item['summary'];
  70. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  71. $plexItem['thumb'] = (string)$item['thumb'];
  72. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  73. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  74. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  75. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  76. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  77. break;
  78. case 'album':
  79. case 'track':
  80. $plexItem['type'] = 'music';
  81. $plexItem['title'] = (string)$item['parentTitle'];
  82. $plexItem['summary'] = (string)$item['title'];
  83. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  84. $plexItem['thumb'] = (string)$item['thumb'];
  85. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  86. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  87. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  88. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  89. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  90. $plexItem['metadataKey'] = isset($item['parentRatingKey']) ? (string)$item['parentRatingKey'] : (string)$item['ratingKey'];
  91. break;
  92. default:
  93. $plexItem['type'] = 'movie';
  94. $plexItem['title'] = (string)$item['title'];
  95. $plexItem['summary'] = (string)$item['summary'];
  96. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  97. $plexItem['thumb'] = (string)$item['thumb'];
  98. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  99. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  100. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  101. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  102. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  103. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  104. }
  105. $plexItem['uid'] = (string)$item['ratingKey'];
  106. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  107. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  108. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  109. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress']- $plexItem['watched']) : '';
  110. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision']: '';
  111. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  112. $plexItem['session'] = (string)$item->Session['id'];
  113. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  114. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  115. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  116. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  117. $plexItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['title'] : "";
  118. $plexItem['userThumb'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['thumb'] : "";
  119. $plexItem['userAddress'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->Player['address'] : "x.x.x.x";
  120. $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'];
  121. $plexItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'$'.randString();
  122. $plexItem['originalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'$'.randString();
  123. $plexItem['openTab'] = $GLOBALS['plexTabURL'] && $GLOBALS['plexTabName'] ? true : false;
  124. $plexItem['tabName'] = $GLOBALS['plexTabName'] ? $GLOBALS['plexTabName'] : '';
  125. // Stream info
  126. $plexItem['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. $plexItem['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.$plexItem['nowPlayingKey'].'.jpg')){ $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$plexItem['nowPlayingKey'].'.jpg'; }
  176. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg')){ $plexItem['imageURL'] = $cacheDirectoryWeb.$plexItem['key'].'.jpg'; }
  177. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')) {
  178. $plexItem['nowPlayingImageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'';
  179. }
  180. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['key'].'.jpg') || !file_exists($cacheDirectory.$plexItem['key'].'.jpg')) {
  181. $plexItem['imageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'';
  182. }
  183. if(!$plexItem['nowPlayingThumb'] ){ $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png"; $plexItem['nowPlayingKey'] = "no-np"; }
  184. if(!$plexItem['thumb'] ){ $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/cache/no-list.png"; $plexItem['key'] = "no-list"; }
  185. if(isset($useImage)){ $plexItem['useImage'] = $useImage; }
  186. return $plexItem;
  187. }
  188. function plexConnect($action,$key=null){
  189. if(!empty($GLOBALS['plexURL']) && !empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'] && qualifyRequest($GLOBALS['homepagePlexStreamsAuth']))){
  190. $url = qualifyURL($GLOBALS['plexURL']);
  191. switch ($action) {
  192. case 'streams':
  193. $url = $url."/status/sessions?X-Plex-Token=".$GLOBALS['plexToken'];
  194. break;
  195. case 'recent':
  196. $url = $url."/library/recentlyAdded?X-Plex-Token=".$GLOBALS['plexToken'];
  197. break;
  198. case 'metadata':
  199. $url = $url."/library/metadata/".$key."?X-Plex-Token=".$GLOBALS['plexToken'];
  200. break;
  201. default:
  202. # code...
  203. break;
  204. }
  205. try{
  206. $options = (localURL($url)) ? array('verify' => false ) : array();
  207. $response = Requests::get($url, array(), $options);
  208. libxml_use_internal_errors(true);
  209. if($response->success){
  210. $items = array();
  211. $plex = simplexml_load_string($response->body);
  212. foreach($plex AS $child) {
  213. $items[] = resolvePlexItem($child);
  214. }
  215. $api['content'] = $items;
  216. $api['plexID'] = $GLOBALS['plexID'];
  217. $api['showNames'] = true;
  218. $api['group'] = '1';
  219. return $api;
  220. }
  221. }catch( Requests_Exception $e ) {
  222. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  223. };
  224. }
  225. return false;
  226. }