homepage-connect-functions.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. function homepageConnect($array){
  3. switch ($array['data']['action']) {
  4. case 'getPlexStreams':
  5. return getPlexStreams();
  6. break;
  7. case 'getPlexRecent':
  8. return getPlexRecent();
  9. break;
  10. default:
  11. # code...
  12. break;
  13. }
  14. }
  15. function streamType($value){
  16. if($value == "transcode" || $value == "Transcode"){
  17. return "Transcode";
  18. }elseif($value == "copy" || $value == "DirectStream"){
  19. return "Direct Stream";
  20. }elseif($value == "directplay" || $value == "DirectPlay"){
  21. return "Direct Play";
  22. }else{
  23. return "Direct Play";
  24. }
  25. }
  26. function resolvePlexItem($item) {
  27. // Static Height
  28. $height = 300;
  29. $width = 200;
  30. $nowPlayingHeight = 338;
  31. $nowPlayingWidth = 600;
  32. $widthOverride = 100;
  33. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  34. $cacheDirectoryWeb = 'plugins/images/cache/';
  35. switch ($item['type']) {
  36. case 'season':
  37. $plexItem['type'] = 'tv';
  38. $plexItem['title'] = (string)$item['parentTitle'];
  39. $plexItem['summary'] = (string)$item['parentSummary'];
  40. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  41. $plexItem['thumb'] = (string)$item['thumb'];
  42. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  43. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  44. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  45. break;
  46. case 'episode':
  47. $plexItem['type'] = 'tv';
  48. $plexItem['title'] = (string)$item['grandparentTitle'];
  49. $plexItem['summary'] = (string)$item['title'];
  50. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  51. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  52. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  53. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  54. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  55. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  56. $plexItem['nowPlayingBottom'] = 'S'.(string)$item['parentIndex'].' · E'.(string)$item['index'];
  57. break;
  58. case 'clip':
  59. $useImage = (isset($item['live']) ? "images/livetv.png" : null);
  60. $plexItem['type'] = 'clip';
  61. $plexItem['title'] = (string)$item['title'];
  62. $plexItem['summary'] = (string)$item['summary'];
  63. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  64. $plexItem['thumb'] = (string)$item['thumb'];
  65. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  66. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  67. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  68. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  69. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  70. break;
  71. case 'album':
  72. case 'track':
  73. $plexItem['type'] = 'music';
  74. $plexItem['title'] = (string)$item['parentTitle'];
  75. $plexItem['summary'] = (string)$item['title'];
  76. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  77. $plexItem['thumb'] = (string)$item['thumb'];
  78. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  79. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  80. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  81. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  82. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  83. break;
  84. default:
  85. $plexItem['type'] = 'movie';
  86. $plexItem['title'] = (string)$item['title'];
  87. $plexItem['summary'] = (string)$item['summary'];
  88. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  89. $plexItem['thumb'] = (string)$item['thumb'];
  90. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  91. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  92. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  93. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  94. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  95. }
  96. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  97. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  98. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  99. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress']- $plexItem['watched']) : '';
  100. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision']: '';
  101. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  102. $plexItem['session'] = (string)$item->Session['id'];
  103. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  104. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  105. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  106. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  107. $plexItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['title'] : "";
  108. $plexItem['userThumb'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['thumb'] : "";
  109. $plexItem['userAddress'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->Player['address'] : "x.x.x.x";
  110. $plexItem['userStream'] = array(
  111. 'platform' => (string)$item->Player['platform'],
  112. 'device' => (string)$item->Player['device'],
  113. 'stream' => (string)$item->Media->Part['decision'].($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)': ''),
  114. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  115. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  116. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  117. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  118. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  119. 'videoDecision' => streamType((string)$item->TranscodeSession['videoDecision']),
  120. 'audioDecision' => streamType((string)$item->TranscodeSession['audioDecision']),
  121. 'container' => (string)$item->TranscodeSession['container'],
  122. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  123. );
  124. $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'];
  125. $plexItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'$'.randString();
  126. $plexItem['originalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'$'.randString();
  127. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')){ $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$plexItem['nowPlayingKey'].'.jpg'; }
  128. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg')){ $plexItem['imageURL'] = $cacheDirectoryWeb.$plexItem['key'].'.jpg'; }
  129. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')) {
  130. $plexItem['nowPlayingImageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'';
  131. }
  132. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['key'].'.jpg') || !file_exists($cacheDirectory.$plexItem['key'].'.jpg')) {
  133. $plexItem['imageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'';
  134. }
  135. if(!$plexItem['nowPlayingThumb'] ){ $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "images/no-np.png"; $plexItem['nowPlayingKey'] = "no-np"; }
  136. if(!$plexItem['thumb'] ){ $plexItem['originalImage'] = $plexItem['imageURL'] = "images/no-list.png"; $plexItem['key'] = "no-list"; }
  137. if(isset($useImage)){ $plexItem['useImage'] = $useImage; }
  138. return $plexItem;
  139. }
  140. function getPlexStreams(){
  141. if(!empty($GLOBALS['plexURL']) && !empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'] && qualifyRequest($GLOBALS['homepagePlexStreamsAuth']))){
  142. try{
  143. $url = qualifyURL($GLOBALS['plexURL']);
  144. $url = $url."/status/sessions?X-Plex-Token=".$GLOBALS['plexToken'];
  145. $options = (localURL($url)) ? array('verify' => false ) : array();
  146. $response = Requests::get($url, array(), $options);
  147. libxml_use_internal_errors(true);
  148. if($response->success){
  149. $items = array();
  150. $plex = simplexml_load_string($response->body);
  151. foreach($plex AS $child) {
  152. $items[] = resolvePlexItem($child);
  153. }
  154. $api['content'] = $items;
  155. $api['plexID'] = $GLOBALS['plexID'];
  156. $api['showNames'] = true;
  157. $api['group'] = '1';
  158. return $api;
  159. }
  160. }catch( Requests_Exception $e ) {
  161. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  162. };
  163. }
  164. return false;
  165. }
  166. function getPlexRecent(){
  167. if(!empty($GLOBALS['plexURL']) && !empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'] && qualifyRequest($GLOBALS['homepagePlexRecentAuth']))){
  168. try{
  169. $url = qualifyURL($GLOBALS['plexURL']);
  170. $url = $url."/library/recentlyAdded?X-Plex-Token=".$GLOBALS['plexToken'];
  171. $options = (localURL($url)) ? array('verify' => false ) : array();
  172. $response = Requests::get($url, array(), $options);
  173. libxml_use_internal_errors(true);
  174. if($response->success){
  175. $items = array();
  176. $plex = simplexml_load_string($response->body);
  177. foreach($plex AS $child) {
  178. $items[] = resolvePlexItem($child);
  179. }
  180. $api['content'] = $items;
  181. $api['plexID'] = $GLOBALS['plexID'];
  182. $api['showNames'] = true;
  183. $api['group'] = '1';
  184. return $api;
  185. }
  186. }catch( Requests_Exception $e ) {
  187. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  188. };
  189. }
  190. return false;
  191. }