homepage-connect-functions.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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'] = (string)$item['viewOffset'];
  97. $plexItem['duration'] = ($item['duration']) ? (string)$item['duration'] : (string)$item->Media['duration'];
  98. $plexItem['watched'] = (!empty($plexItem['elapsed']) ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  99. $plexItem['transcoded'] = floor($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['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  103. $plexItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest(1) ) ? (string)$item->User['title'] : "";
  104. $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'];
  105. $plexItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'$'.randString();
  106. $plexItem['originalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'$'.randString();
  107. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')){ $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$plexItem['nowPlayingKey'].'.jpg'; }
  108. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg')){ $plexItem['imageURL'] = $cacheDirectoryWeb.$plexItem['key'].'.jpg'; }
  109. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')) {
  110. $plexItem['nowPlayingImageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'';
  111. }
  112. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['key'].'.jpg') || !file_exists($cacheDirectory.$plexItem['key'].'.jpg')) {
  113. $plexItem['imageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'';
  114. }
  115. if(!$plexItem['nowPlayingThumb'] ){ $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "images/no-np.png"; $plexItem['nowPlayingKey'] = "no-np"; }
  116. if(!$plexItem['thumb'] ){ $plexItem['originalImage'] = $plexItem['imageURL'] = "images/no-list.png"; $plexItem['key'] = "no-list"; }
  117. if(isset($useImage)){ $plexItem['useImage'] = $useImage; }
  118. return $plexItem;
  119. }
  120. function getPlexStreams(){
  121. if(!empty($GLOBALS['plexURL']) && !empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'] && qualifyRequest($GLOBALS['homepagePlexStreamsAuth']))){
  122. try{
  123. $url = qualifyURL($GLOBALS['plexURL']);
  124. $url = $url."/status/sessions?X-Plex-Token=".$GLOBALS['plexToken'];
  125. $options = (localURL($url)) ? array('verify' => false ) : array();
  126. $response = Requests::get($url, array(), $options);
  127. libxml_use_internal_errors(true);
  128. if($response->success){
  129. $items = array();
  130. $plex = simplexml_load_string($response->body);
  131. foreach($plex AS $child) {
  132. $items[] = resolvePlexItem($child);
  133. }
  134. $api['content'] = $items;
  135. $api['plexID'] = $GLOBALS['plexID'];
  136. $api['showNames'] = true;
  137. $api['group'] = '1';
  138. return $api;
  139. }
  140. }catch( Requests_Exception $e ) {
  141. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  142. };
  143. }
  144. return false;
  145. }
  146. function getPlexRecent(){
  147. if(!empty($GLOBALS['plexURL']) && !empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'] && qualifyRequest($GLOBALS['homepagePlexRecentAuth']))){
  148. try{
  149. $url = qualifyURL($GLOBALS['plexURL']);
  150. $url = $url."/library/recentlyAdded?X-Plex-Token=".$GLOBALS['plexToken'];
  151. $options = (localURL($url)) ? array('verify' => false ) : array();
  152. $response = Requests::get($url, array(), $options);
  153. libxml_use_internal_errors(true);
  154. if($response->success){
  155. $items = array();
  156. $plex = simplexml_load_string($response->body);
  157. foreach($plex AS $child) {
  158. $items[] = resolvePlexItem($child);
  159. }
  160. $api['content'] = $items;
  161. $api['plexID'] = $GLOBALS['plexID'];
  162. $api['showNames'] = true;
  163. $api['group'] = '1';
  164. return $api;
  165. }
  166. }catch( Requests_Exception $e ) {
  167. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  168. };
  169. }
  170. return false;
  171. }