homepage-connect-functions.php 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. case 'getqBittorrent':
  32. return qBittorrentConnect();
  33. break;
  34. case 'getCalendar':
  35. return getCalendar();
  36. break;
  37. default:
  38. # code...
  39. break;
  40. }
  41. }
  42. function streamType($value){
  43. if($value == "transcode" || $value == "Transcode"){
  44. return "Transcode";
  45. }elseif($value == "copy" || $value == "DirectStream"){
  46. return "Direct Stream";
  47. }elseif($value == "directplay" || $value == "DirectPlay"){
  48. return "Direct Play";
  49. }else{
  50. return "Direct Play";
  51. }
  52. }
  53. function resolveEmbyItem($itemDetails) {
  54. // Grab Each item info from Emby (extra call)
  55. $id = isset($itemDetails['NowPlayingItem']['Id']) ? $itemDetails['NowPlayingItem']['Id'] : $itemDetails['Id'];
  56. $url = qualifyURL($GLOBALS['embyURL']);
  57. $url = $url.'/Items?Ids='.$id.'&api_key='.$GLOBALS['embyToken'].'&Fields=Overview,People,Genres,CriticRating,Studios,Taglines';
  58. try{
  59. $options = (localURL($url)) ? array('verify' => false ) : array();
  60. $response = Requests::get($url, array(), $options);
  61. if($response->success){
  62. $item = json_decode($response->body,true)['Items'][0];
  63. }
  64. }catch( Requests_Exception $e ) {
  65. return false;
  66. };
  67. // Static Height & Width
  68. $height = 300;
  69. $width = 200;
  70. $nowPlayingHeight = 675;
  71. $nowPlayingWidth = 1200;
  72. $actorHeight = 450;
  73. $actorWidth = 300;
  74. $widthOverride = 100;
  75. // Cache Directories
  76. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  77. $cacheDirectoryWeb = 'plugins/images/cache/';
  78. // Types
  79. $embyItem['array-item'] = $item;
  80. $embyItem['array-itemdetails'] = $itemDetails;
  81. switch (@$item['Type']) {
  82. case 'Series':
  83. $embyItem['type'] = 'tv';
  84. $embyItem['title'] = $item['Name'];
  85. $embyItem['summary'] = '';
  86. $embyItem['ratingKey'] = $item['Id'];
  87. $embyItem['thumb'] = $item['Id'];
  88. $embyItem['key'] = $item['Id'] . "-list";
  89. $embyItem['nowPlayingThumb'] = $item['Id'];
  90. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  91. $embyItem['metadataKey'] = $item['Id'];
  92. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['BackdropImageTags'][0]) ? 'Backdrop' : '');
  93. break;
  94. case 'Episode':
  95. $embyItem['type'] = 'tv';
  96. $embyItem['title'] = $item['SeriesName'];
  97. $embyItem['summary'] = '';
  98. $embyItem['ratingKey'] = $item['Id'];
  99. $embyItem['thumb'] = (isset($item['SeriesId'])?$item['SeriesId']:$item['Id']);
  100. $embyItem['key'] = (isset($item['SeriesId'])?$item['SeriesId']:$item['Id']) . "-list";
  101. $embyItem['nowPlayingThumb'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'] : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'] : false);
  102. $embyItem['nowPlayingKey'] = isset($item['ParentThumbItemId']) ? $item['ParentThumbItemId'].'-np' : (isset($item['ParentBackdropItemId']) ? $item['ParentBackdropItemId'].'-np' : false);
  103. $embyItem['metadataKey'] = $item['Id'];
  104. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? 'Thumb' : (isset($item['ParentBackdropImageTags'][0]) ? 'Backdrop' : '');
  105. $embyItem['nowPlayingTitle'] = @$item['SeriesName'].' - '.@$item['Name'];
  106. $embyItem['nowPlayingBottom'] = 'S'.@$item['ParentIndexNumber'].' · E'.@$item['IndexNumber'];
  107. break;
  108. case 'MusicAlbum':
  109. case 'Audio':
  110. $embyItem['type'] = 'music';
  111. $embyItem['title'] = $item['Name'];
  112. $embyItem['summary'] = '';
  113. $embyItem['ratingKey'] = $item['Id'];
  114. $embyItem['thumb'] = $item['Id'];
  115. $embyItem['key'] = $item['Id'] . "-list";
  116. $embyItem['nowPlayingThumb'] = (isset($item['AlbumId']) ? $item['AlbumId'] : @$item['ParentBackdropItemId']);
  117. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  118. $embyItem['metadataKey'] = isset($item['AlbumId']) ? $item['AlbumId'] : $item['Id'];
  119. $embyItem['nowPlayingImageType'] = (isset($item['ParentBackdropItemId']) ? "Primary" : "Backdrop");
  120. $embyItem['nowPlayingTitle'] = @$item['AlbumArtist'].' - '.@$item['Name'];
  121. $embyItem['nowPlayingBottom'] = @$item['Album'];
  122. break;
  123. case 'Movie':
  124. $embyItem['type'] = 'movie';
  125. $embyItem['title'] = $item['Name'];
  126. $embyItem['summary'] = '';
  127. $embyItem['ratingKey'] = $item['Id'];
  128. $embyItem['thumb'] = $item['Id'];
  129. $embyItem['key'] = $item['Id'] . "-list";
  130. $embyItem['nowPlayingThumb'] = $item['Id'];
  131. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  132. $embyItem['metadataKey'] = $item['Id'];
  133. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  134. $embyItem['nowPlayingTitle'] = @$item['Name'];
  135. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  136. break;
  137. case 'Video':
  138. $embyItem['type'] = 'video';
  139. $embyItem['title'] = $item['Name'];
  140. $embyItem['summary'] = '';
  141. $embyItem['ratingKey'] = $item['Id'];
  142. $embyItem['thumb'] = $item['Id'];
  143. $embyItem['key'] = $item['Id'] . "-list";
  144. $embyItem['nowPlayingThumb'] = $item['Id'];
  145. $embyItem['nowPlayingKey'] = $item['Id'] . "-np";
  146. $embyItem['metadataKey'] = $item['Id'];
  147. $embyItem['nowPlayingImageType'] = isset($item['ImageTags']['Thumb']) ? "Thumb" : (isset($item['BackdropImageTags']) ? "Backdrop" : false);
  148. $embyItem['nowPlayingTitle'] = @$item['Name'];
  149. $embyItem['nowPlayingBottom'] = @$item['ProductionYear'];
  150. break;
  151. default:
  152. return false;
  153. }
  154. $embyItem['uid'] = $item['Id'];
  155. $embyItem['imageType'] = (isset($item['ImageTags']['Primary']) ? "Primary" : false);
  156. $embyItem['elapsed'] = isset($itemDetails['PlayState']['PositionTicks']) && $itemDetails['PlayState']['PositionTicks'] !== '0' ? (int)$itemDetails['PlayState']['PositionTicks'] : null;
  157. $embyItem['duration'] = isset($itemDetails['NowPlayingItem']['RunTimeTicks']) ? (int)$itemDetails['NowPlayingItem']['RunTimeTicks'] : (int)$item['RunTimeTicks'];
  158. $embyItem['watched'] = ($embyItem['elapsed'] && $embyItem['duration'] ? floor(($embyItem['elapsed'] / $embyItem['duration']) * 100) : 0);
  159. $embyItem['transcoded'] = isset($itemDetails['TranscodingInfo']['CompletionPercentage']) ? floor((int)$itemDetails['TranscodingInfo']['CompletionPercentage']) : 100;
  160. $embyItem['stream'] = @$itemDetails['PlayState']['PlayMethod'];
  161. $embyItem['id'] = $item['ServerId'];
  162. $embyItem['session'] = @$itemDetails['DeviceId'];
  163. $embyItem['bandwidth'] = isset($itemDetails['TranscodingInfo']['Bitrate']) ? $itemDetails['TranscodingInfo']['Bitrate'] / 1000 : '';
  164. $embyItem['bandwidthType'] = 'wan';
  165. $embyItem['sessionType'] = (@$itemDetails['PlayState']['PlayMethod'] == 'Transcode') ? 'Transcoding' : 'Direct Playing';
  166. $embyItem['state'] = ((@(string)$itemDetails['PlayState']['IsPaused'] == '1') ? "pause" : "play");
  167. $embyItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? @(string)$itemDetails['UserName'] : "";
  168. $embyItem['userThumb'] = '';
  169. $embyItem['userAddress'] = "x.x.x.x";
  170. $embyItem['address'] = $GLOBALS['embyTabURL'] ? '' : '';
  171. $embyItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=emby&type='.$embyItem['nowPlayingImageType'].'&img='.$embyItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$embyItem['nowPlayingKey'].'$'.randString();
  172. $embyItem['originalImage'] = 'api/?v1/image&source=emby&type='.$embyItem['imageType'].'&img='.$embyItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$embyItem['key'].'$'.randString();
  173. $embyItem['openTab'] = $GLOBALS['embyTabURL'] && $GLOBALS['embyTabName'] ? true : false;
  174. $embyItem['tabName'] = $GLOBALS['embyTabName'] ? $GLOBALS['embyTabName'] : '';
  175. // Stream info
  176. $embyItem['userStream'] = array(
  177. 'platform' => @(string)$itemDetails['Client'],
  178. 'product' => @(string)$itemDetails['Client'],
  179. 'device' => @(string)$itemDetails['DeviceName'],
  180. 'stream' => @$itemDetails['PlayState']['PlayMethod'],
  181. 'videoResolution' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]['Width']) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Width'] : '',
  182. 'throttled' => false,
  183. 'sourceVideoCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : '',
  184. 'videoCodec' => @$itemDetails['TranscodingInfo']['VideoCodec'],
  185. 'audioCodec' => @$itemDetails['TranscodingInfo']['AudioCodec'],
  186. 'sourceAudioCodec' => isset($itemDetails['NowPlayingItem']['MediaStreams'][1]) ? $itemDetails['NowPlayingItem']['MediaStreams'][1]['Codec'] : (isset($itemDetails['NowPlayingItem']['MediaStreams'][0]) ? $itemDetails['NowPlayingItem']['MediaStreams'][0]['Codec'] : ''),
  187. 'videoDecision' => streamType(@$itemDetails['PlayState']['PlayMethod']),
  188. 'audioDecision' => streamType(@$itemDetails['PlayState']['PlayMethod']),
  189. 'container' => isset($itemDetails['NowPlayingItem']['Container']) ? $itemDetails['NowPlayingItem']['Container'] : '',
  190. 'audioChannels' => @$itemDetails['TranscodingInfo']['AudioChannels']
  191. );
  192. // Genre catch all
  193. if($item['Genres']){
  194. $genres = array();
  195. foreach ($item['Genres'] as $genre) {
  196. $genres[] = $genre;
  197. }
  198. }
  199. // Actor catch all
  200. if($item['People'] ){
  201. $actors = array();
  202. foreach ($item['People'] as $key => $value) {
  203. if(@$value['PrimaryImageTag'] && @$value['Role']){
  204. if (file_exists($cacheDirectory.(string)$value['Id'].'-cast.jpg')){ $actorImage = $cacheDirectoryWeb.(string)$value['Id'].'-cast.jpg'; }
  205. 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')) {
  206. $actorImage = 'api/?v1/image&source=emby&type=Primary&img='.(string)$value['Id'].'&height='.$actorHeight.'&width='.$actorWidth.'&key='.(string)$value['Id'].'-cast';
  207. }
  208. $actors[] = array(
  209. 'name' => (string)$value['Name'],
  210. 'role' => (string)$value['Role'],
  211. 'thumb' => $actorImage
  212. );
  213. }
  214. }
  215. }
  216. // Metadata information
  217. $embyItem['metadata'] = array(
  218. 'guid' => $item['Id'],
  219. 'summary' => @(string)$item['Overview'],
  220. 'rating' => @(string)$item['CommunityRating'],
  221. 'duration' => @(string)$item['RunTimeTicks'],
  222. 'originallyAvailableAt' => @(string)$item['PremiereDate'],
  223. 'year' => (string)$item['ProductionYear'],
  224. //'studio' => (string)$item['studio'],
  225. 'tagline' => @(string)$item['Taglines'][0],
  226. 'genres' => ($item['Genres']) ? $genres : '',
  227. 'actors' => ($item['People']) ? $actors : ''
  228. );
  229. if (file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg')){ $embyItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$embyItem['nowPlayingKey'].'.jpg'; }
  230. if (file_exists($cacheDirectory.$embyItem['key'].'.jpg')){ $embyItem['imageURL'] = $cacheDirectoryWeb.$embyItem['key'].'.jpg'; }
  231. if (file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$embyItem['nowPlayingKey'].'.jpg')) {
  232. $embyItem['nowPlayingImageURL'] = 'api/?v1/image&source=emby&type='.$embyItem['nowPlayingImageType'].'&img='.$embyItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$embyItem['nowPlayingKey'].'';
  233. }
  234. if (file_exists($cacheDirectory.$embyItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$embyItem['key'].'.jpg') || !file_exists($cacheDirectory.$embyItem['key'].'.jpg')) {
  235. $embyItem['imageURL'] = 'api/?v1/image&source=emby&type='.$embyItem['imageType'].'&img='.$embyItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$embyItem['key'].'';
  236. }
  237. if(!$embyItem['nowPlayingThumb'] ){ $embyItem['nowPlayingOriginalImage'] = $embyItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png"; $embyItem['nowPlayingKey'] = "no-np"; }
  238. if(!$embyItem['thumb'] ){ $embyItem['originalImage'] = $embyItem['imageURL'] = "plugins/images/cache/no-list.png"; $embyItem['key'] = "no-list"; }
  239. if(isset($useImage)){ $embyItem['useImage'] = $useImage; }
  240. return $embyItem;
  241. }
  242. function resolvePlexItem($item) {
  243. // Static Height & Width
  244. $height = 300;
  245. $width = 200;
  246. $nowPlayingHeight = 675;
  247. $nowPlayingWidth = 1200;
  248. $widthOverride = 100;
  249. // Cache Directories
  250. $cacheDirectory = dirname(__DIR__,2).DIRECTORY_SEPARATOR.'plugins'.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR;
  251. $cacheDirectoryWeb = 'plugins/images/cache/';
  252. // Types
  253. switch ($item['type']) {
  254. case 'season':
  255. $plexItem['type'] = 'tv';
  256. $plexItem['title'] = (string)$item['parentTitle'];
  257. $plexItem['summary'] = (string)$item['parentSummary'];
  258. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  259. $plexItem['thumb'] = (string)$item['thumb'];
  260. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  261. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  262. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  263. $plexItem['metadataKey'] = (string)$item['parentRatingKey'];
  264. break;
  265. case 'episode':
  266. $plexItem['type'] = 'tv';
  267. $plexItem['title'] = (string)$item['grandparentTitle'];
  268. $plexItem['summary'] = (string)$item['title'];
  269. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  270. $plexItem['thumb'] = ($item['parentThumb'] ? (string)$item['parentThumb'] : (string)$item['grandparentThumb']);
  271. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  272. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  273. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  274. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  275. $plexItem['nowPlayingBottom'] = 'S'.(string)$item['parentIndex'].' · E'.(string)$item['index'];
  276. $plexItem['metadataKey'] = (string)$item['grandparentRatingKey'];
  277. break;
  278. case 'clip':
  279. $useImage = (isset($item['live']) ? "plugins/images/cache/livetv.png" : null);
  280. $plexItem['type'] = 'clip';
  281. $plexItem['title'] = (string)$item['title'];
  282. $plexItem['summary'] = (string)$item['summary'];
  283. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  284. $plexItem['thumb'] = (string)$item['thumb'];
  285. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  286. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  287. $plexItem['nowPlayingKey'] = isset($item['ratingKey']) ? (string)$item['ratingKey'] . "-np" : (isset($item['live']) ? "livetv.png" : ":)");
  288. $plexItem['nowPlayingTitle'] = $plexItem['title'];
  289. $plexItem['nowPlayingBottom'] = isset($item['extraType']) ? "Trailer" : (isset($item['live']) ? "Live TV" : ":)");
  290. break;
  291. case 'album':
  292. case 'track':
  293. $plexItem['type'] = 'music';
  294. $plexItem['title'] = (string)$item['parentTitle'];
  295. $plexItem['summary'] = (string)$item['title'];
  296. $plexItem['ratingKey'] = (string)$item['parentRatingKey'];
  297. $plexItem['thumb'] = (string)$item['thumb'];
  298. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  299. $plexItem['nowPlayingThumb'] = ($item['parentThumb']) ? (string)$item['parentThumb'] : (string)$item['art'];
  300. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  301. $plexItem['nowPlayingTitle'] = (string)$item['grandparentTitle'].' - '.(string)$item['title'];
  302. $plexItem['nowPlayingBottom'] = (string)$item['parentTitle'];
  303. $plexItem['metadataKey'] = isset($item['grandparentRatingKey']) ? (string)$item['grandparentRatingKey'] : (string)$item['parentRatingKey'];
  304. break;
  305. default:
  306. $plexItem['type'] = 'movie';
  307. $plexItem['title'] = (string)$item['title'];
  308. $plexItem['summary'] = (string)$item['summary'];
  309. $plexItem['ratingKey'] = (string)$item['ratingKey'];
  310. $plexItem['thumb'] = (string)$item['thumb'];
  311. $plexItem['key'] = (string)$item['ratingKey'] . "-list";
  312. $plexItem['nowPlayingThumb'] = (string)$item['art'];
  313. $plexItem['nowPlayingKey'] = (string)$item['ratingKey'] . "-np";
  314. $plexItem['nowPlayingTitle'] = (string)$item['title'];
  315. $plexItem['nowPlayingBottom'] = (string)$item['year'];
  316. $plexItem['metadataKey'] = (string)$item['ratingKey'];
  317. }
  318. $plexItem['uid'] = (string)$item['ratingKey'];
  319. $plexItem['elapsed'] = isset($item['viewOffset']) && $item['viewOffset'] !== '0' ? (int)$item['viewOffset'] : null;
  320. $plexItem['duration'] = isset($item['duration']) ? (int)$item['duration'] : (int)$item->Media['duration'];
  321. $plexItem['watched'] = ($plexItem['elapsed'] && $plexItem['duration'] ? floor(($plexItem['elapsed'] / $plexItem['duration']) * 100) : 0);
  322. $plexItem['transcoded'] = isset($item->TranscodeSession['progress']) ? floor((int)$item->TranscodeSession['progress']- $plexItem['watched']) : '';
  323. $plexItem['stream'] = isset($item->Media->Part->Stream['decision']) ? (string)$item->Media->Part->Stream['decision']: '';
  324. $plexItem['id'] = str_replace('"', '', (string)$item->Player['machineIdentifier']);
  325. $plexItem['session'] = (string)$item->Session['id'];
  326. $plexItem['bandwidth'] = (string)$item->Session['bandwidth'];
  327. $plexItem['bandwidthType'] = (string)$item->Session['location'];
  328. $plexItem['sessionType'] = isset($item->TranscodeSession['progress']) ? 'Transcoding' : 'Direct Playing';
  329. $plexItem['state'] = (((string)$item->Player['state'] == "paused") ? "pause" : "play");
  330. $plexItem['user'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['title'] : "";
  331. $plexItem['userThumb'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->User['thumb'] : "";
  332. $plexItem['userAddress'] = ($GLOBALS['homepageShowStreamNames'] && qualifyRequest($GLOBALS['homepageShowStreamNamesAuth']) ) ? (string)$item->Player['address'] : "x.x.x.x";
  333. $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'];
  334. $plexItem['nowPlayingOriginalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'$'.randString();
  335. $plexItem['originalImage'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'$'.randString();
  336. $plexItem['openTab'] = $GLOBALS['plexTabURL'] && $GLOBALS['plexTabName'] ? true : false;
  337. $plexItem['tabName'] = $GLOBALS['plexTabName'] ? $GLOBALS['plexTabName'] : '';
  338. // Stream info
  339. $plexItem['userStream'] = array(
  340. 'platform' => (string)$item->Player['platform'],
  341. 'product' => (string)$item->Player['product'],
  342. 'device' => (string)$item->Player['device'],
  343. 'stream' => (string)$item->Media->Part['decision'].($item->TranscodeSession['throttled'] == '1' ? ' (Throttled)': ''),
  344. 'videoResolution' => (string)$item->Media['videoResolution'],
  345. 'throttled' => ($item->TranscodeSession['throttled'] == 1) ? true : false,
  346. 'sourceVideoCodec' => (string)$item->TranscodeSession['sourceVideoCodec'],
  347. 'videoCodec' => (string)$item->TranscodeSession['videoCodec'],
  348. 'audioCodec' => (string)$item->TranscodeSession['audioCodec'],
  349. 'sourceAudioCodec' => (string)$item->TranscodeSession['sourceAudioCodec'],
  350. 'videoDecision' => streamType((string)$item->TranscodeSession['videoDecision']),
  351. 'audioDecision' => streamType((string)$item->TranscodeSession['audioDecision']),
  352. 'container' => (string)$item->TranscodeSession['container'],
  353. 'audioChannels' => (string)$item->TranscodeSession['audioChannels']
  354. );
  355. // Genre catch all
  356. if($item->Genre){
  357. $genres = array();
  358. foreach ($item->Genre as $key => $value) {
  359. $genres[] = (string)$value['tag'];
  360. }
  361. }
  362. // Actor catch all
  363. if($item->Role ){
  364. $actors = array();
  365. foreach ($item->Role as $key => $value) {
  366. if($value['thumb']){
  367. $actors[] = array(
  368. 'name' => (string)$value['tag'],
  369. 'role' => (string)$value['role'],
  370. 'thumb' => (string)$value['thumb']
  371. );
  372. }
  373. }
  374. }
  375. // Metadata information
  376. $plexItem['metadata'] = array(
  377. 'guid' => (string)$item['guid'],
  378. 'summary' => (string)$item['summary'],
  379. 'rating' => (string)$item['rating'],
  380. 'duration' => (string)$item['duration'],
  381. 'originallyAvailableAt' => (string)$item['originallyAvailableAt'],
  382. 'year' => (string)$item['year'],
  383. 'studio' => (string)$item['studio'],
  384. 'tagline' => (string)$item['tagline'],
  385. 'genres' => ($item->Genre) ? $genres : '',
  386. 'actors' => ($item->Role) ? $actors : ''
  387. );
  388. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')){ $plexItem['nowPlayingImageURL'] = $cacheDirectoryWeb.$plexItem['nowPlayingKey'].'.jpg'; }
  389. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg')){ $plexItem['imageURL'] = $cacheDirectoryWeb.$plexItem['key'].'.jpg'; }
  390. if (file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg') || !file_exists($cacheDirectory.$plexItem['nowPlayingKey'].'.jpg')) {
  391. $plexItem['nowPlayingImageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['nowPlayingThumb'].'&height='.$nowPlayingHeight.'&width='.$nowPlayingWidth.'&key='.$plexItem['nowPlayingKey'].'';
  392. }
  393. if (file_exists($cacheDirectory.$plexItem['key'].'.jpg') && (time() - 604800) > filemtime($cacheDirectory.$plexItem['key'].'.jpg') || !file_exists($cacheDirectory.$plexItem['key'].'.jpg')) {
  394. $plexItem['imageURL'] = 'api/?v1/image&source=plex&img='.$plexItem['thumb'].'&height='.$height.'&width='.$width.'&key='.$plexItem['key'].'';
  395. }
  396. if(!$plexItem['nowPlayingThumb'] ){ $plexItem['nowPlayingOriginalImage'] = $plexItem['nowPlayingImageURL'] = "plugins/images/cache/no-np.png"; $plexItem['nowPlayingKey'] = "no-np"; }
  397. if(!$plexItem['thumb'] ){ $plexItem['originalImage'] = $plexItem['imageURL'] = "plugins/images/cache/no-list.png"; $plexItem['key'] = "no-list"; }
  398. if(isset($useImage)){ $plexItem['useImage'] = $useImage; }
  399. return $plexItem;
  400. }
  401. function plexConnect($action,$key=null){
  402. if($GLOBALS['homepagePlexEnabled'] && !empty($GLOBALS['plexURL']) && !empty($GLOBALS['plexToken']) && !empty($GLOBALS['plexID'] && qualifyRequest($GLOBALS['homepagePlexAuth']))){
  403. $url = qualifyURL($GLOBALS['plexURL']);
  404. switch ($action) {
  405. case 'streams':
  406. $url = $url."/status/sessions?X-Plex-Token=".$GLOBALS['plexToken'];
  407. break;
  408. case 'recent':
  409. $url = $url."/library/recentlyAdded?X-Plex-Token=".$GLOBALS['plexToken'];
  410. break;
  411. case 'metadata':
  412. $url = $url."/library/metadata/".$key."?X-Plex-Token=".$GLOBALS['plexToken'];
  413. break;
  414. default:
  415. # code...
  416. break;
  417. }
  418. try{
  419. $options = (localURL($url)) ? array('verify' => false ) : array();
  420. $response = Requests::get($url, array(), $options);
  421. libxml_use_internal_errors(true);
  422. if($response->success){
  423. $items = array();
  424. $plex = simplexml_load_string($response->body);
  425. foreach($plex AS $child) {
  426. $items[] = resolvePlexItem($child);
  427. }
  428. $api['content'] = $items;
  429. $api['plexID'] = $GLOBALS['plexID'];
  430. $api['showNames'] = true;
  431. $api['group'] = '1';
  432. return $api;
  433. }
  434. }catch( Requests_Exception $e ) {
  435. writeLog('error', 'Plex Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  436. };
  437. }
  438. return false;
  439. }
  440. function embyConnect($action,$key=null,$skip=false){
  441. if($GLOBALS['homepageEmbyEnabled'] && !empty($GLOBALS['embyURL']) && !empty($GLOBALS['embyToken']) && qualifyRequest($GLOBALS['homepageEmbyAuth'])){
  442. $url = qualifyURL($GLOBALS['embyURL']);
  443. switch ($action) {
  444. case 'streams':
  445. $url = $url.'/Sessions?api_key='.$GLOBALS['embyToken'];
  446. break;
  447. case 'recent':
  448. $username = false;
  449. if (isset($GLOBALS['organizrUser']['username'])) {
  450. $username = strtolower($GLOBALS['organizrUser']['username']);
  451. }
  452. // Get A User
  453. $userIds = $url."/Users?api_key=".$GLOBALS['embyToken'];
  454. $showPlayed = true;
  455. try{
  456. $options = (localURL($userIds)) ? array('verify' => false ) : array();
  457. $response = Requests::get($userIds, array(), $options);
  458. if($response->success){
  459. $emby = json_decode($response->body, true);
  460. foreach ($emby as $value) { // Scan for admin user
  461. if (isset($value['Policy']) && isset($value['Policy']['IsAdministrator']) && $value['Policy']['IsAdministrator']) {
  462. $userId = $value['Id'];
  463. }
  464. if ($username && strtolower($value['Name']) == $username) {
  465. $userId = $value['Id'];
  466. $showPlayed = false;
  467. break;
  468. }
  469. }
  470. }
  471. }catch( Requests_Exception $e ) {
  472. writeLog('error', 'Emby Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  473. };
  474. $url = $url.'/Users/'.$userId.'/Items/Latest?EnableImages=false&Limit=100&api_key='.$GLOBALS['embyToken'].($showPlayed?'':'&IsPlayed=false');
  475. break;
  476. case 'metadata':
  477. $skip = true;
  478. break;
  479. default:
  480. # code...
  481. break;
  482. }
  483. if($skip && $key){
  484. $items[] = resolveEmbyItem(array('Id'=>$key));
  485. $api['content'] = $items;
  486. return $api;
  487. }
  488. try{
  489. $options = (localURL($url)) ? array('verify' => false ) : array();
  490. $response = Requests::get($url, array(), $options);
  491. if($response->success){
  492. $items = array();
  493. $emby = json_decode($response->body, true);
  494. foreach($emby AS $child) {
  495. if (isset($child['NowPlayingItem']) || isset($child['Name'])) {
  496. $items[] = resolveEmbyItem($child);
  497. }
  498. }
  499. $api['content'] = array_filter($items);
  500. return $api;
  501. }
  502. }catch( Requests_Exception $e ) {
  503. writeLog('error', 'Emby Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  504. };
  505. }
  506. return false;
  507. }
  508. function sabnzbdConnect() {
  509. if($GLOBALS['homepageSabnzbdEnabled'] && !empty($GLOBALS['sabnzbdURL']) && !empty($GLOBALS['sabnzbdToken']) && qualifyRequest($GLOBALS['homepageSabnzbdAuth'])){
  510. $url = qualifyURL($GLOBALS['sabnzbdURL']);
  511. $url = $url.'/api?mode=queue&output=json&apikey='.$GLOBALS['sabnzbdToken'];
  512. try{
  513. $options = (localURL($url)) ? array('verify' => false ) : array();
  514. $response = Requests::get($url, array(), $options);
  515. if($response->success){
  516. $api['content']['queueItems'] = json_decode($response->body, true);
  517. }
  518. }catch( Requests_Exception $e ) {
  519. writeLog('error', 'SabNZBd Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  520. };
  521. $url = qualifyURL($GLOBALS['sabnzbdURL']);
  522. $url = $url.'/api?mode=history&output=json&apikey='.$GLOBALS['sabnzbdToken'];
  523. try{
  524. $options = (localURL($url)) ? array('verify' => false ) : array();
  525. $response = Requests::get($url, array(), $options);
  526. if($response->success){
  527. $api['content']['historyItems']= json_decode($response->body, true);
  528. }
  529. }catch( Requests_Exception $e ) {
  530. writeLog('error', 'SabNZBd Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  531. };
  532. $api['content'] = isset($api['content']) ? $api['content'] : false;
  533. return $api;
  534. }
  535. }
  536. function nzbgetConnect() {
  537. if($GLOBALS['homepageNzbgetEnabled'] && !empty($GLOBALS['nzbgetURL']) && qualifyRequest($GLOBALS['homepageNzbgetAuth'])){
  538. $url = qualifyURL($GLOBALS['nzbgetURL']);
  539. $url = $url.'/'.$GLOBALS['nzbgetUsername'].':'.decrypt($GLOBALS['nzbgetPassword']).'/jsonrpc/listgroups';
  540. try{
  541. $options = (localURL($url)) ? array('verify' => false ) : array();
  542. $response = Requests::get($url, array(), $options);
  543. if($response->success){
  544. $api['content']['queueItems'] = json_decode($response->body, true);
  545. }
  546. }catch( Requests_Exception $e ) {
  547. writeLog('error', 'NZBGet Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  548. };
  549. $url = qualifyURL($GLOBALS['nzbgetURL']);
  550. $url = $url.'/'.$GLOBALS['nzbgetUsername'].':'.decrypt($GLOBALS['nzbgetPassword']).'/jsonrpc/history';
  551. try{
  552. $options = (localURL($url)) ? array('verify' => false ) : array();
  553. $response = Requests::get($url, array(), $options);
  554. if($response->success){
  555. $api['content']['historyItems']= json_decode($response->body, true);
  556. }
  557. }catch( Requests_Exception $e ) {
  558. writeLog('error', 'NZBGet Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  559. };
  560. $api['content'] = isset($api['content']) ? $api['content'] : false;
  561. return $api;
  562. }
  563. }
  564. function transmissionConnect() {
  565. if($GLOBALS['homepageTransmissionEnabled'] && !empty($GLOBALS['transmissionURL']) && qualifyRequest($GLOBALS['homepageTransmissionAuth'])){
  566. $digest = qualifyURL($GLOBALS['transmissionURL'], true);
  567. $passwordInclude = ($GLOBALS['transmissionUsername'] != '' && $GLOBALS['transmissionPassword'] != '') ? $GLOBALS['transmissionUsername'].':'.decrypt($GLOBALS['transmissionPassword'])."@" : '';
  568. $url = $digest['scheme'].'://'.$passwordInclude.$digest['host'].$digest['port'].$digest['path'].'/rpc';
  569. try{
  570. $options = (localURL($GLOBALS['transmissionURL'])) ? array('verify' => false ) : array();
  571. $response = Requests::get($url, array(), $options);
  572. if($response->headers['x-transmission-session-id']){
  573. $headers = array(
  574. 'X-Transmission-Session-Id' => $response->headers['x-transmission-session-id'],
  575. 'Content-Type' => 'application/json'
  576. );
  577. $data = array(
  578. 'method' => 'torrent-get',
  579. 'arguments' => array(
  580. 'fields' => array(
  581. "id", "name", "totalSize", "eta", "isFinished", "isStalled", "percentDone", "rateDownload", "status", "downloadDir","errorString"
  582. ),
  583. ),
  584. 'tags' => ''
  585. );
  586. $response = Requests::post($url, $headers, json_encode($data), $options);
  587. if($response->success){
  588. $torrentList = json_decode($response->body, true)['arguments']['torrents'];
  589. if($GLOBALS['transmissionHideSeeding'] || $GLOBALS['transmissionHideCompleted']){
  590. $filter = array();
  591. $torrents['arguments']['torrents'] = array();
  592. if($GLOBALS['transmissionHideSeeding']){ array_push($filter, 6, 5); }
  593. if($GLOBALS['transmissionHideCompleted']){ array_push($filter, 0); }
  594. foreach ($torrentList as $key => $value) {
  595. if(!in_array($value['status'], $filter)){
  596. $torrents['arguments']['torrents'][] = $value;
  597. }
  598. }
  599. }else{
  600. $torrents = json_decode($response->body, true);
  601. }
  602. $api['content']['queueItems'] = $torrents;
  603. $api['content']['historyItems'] = false;
  604. }
  605. }else{
  606. writeLog('error', 'Transmission Connect Function - Error: Could not get session ID', 'SYSTEM');
  607. }
  608. }catch( Requests_Exception $e ) {
  609. writeLog('error', 'Transmission Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  610. };
  611. $api['content'] = isset($api['content']) ? $api['content'] : false;
  612. return $api;
  613. }
  614. }
  615. function qBittorrentConnect() {
  616. if($GLOBALS['homepageqBittorrentEnabled'] && !empty($GLOBALS['qBittorrentURL']) && qualifyRequest($GLOBALS['homepageqBittorrentAuth'])){
  617. $digest = qualifyURL($GLOBALS['qBittorrentURL'], true);
  618. $passwordInclude = ($GLOBALS['qBittorrentUsername'] != '' && $GLOBALS['qBittorrentPassword'] != '') ? 'username='.$GLOBALS['qBittorrentUsername'].'&password='.decrypt($GLOBALS['qBittorrentPassword'])."@" : '';
  619. $data = array('username'=>$GLOBALS['qBittorrentUsername'], 'password'=> decrypt($GLOBALS['qBittorrentPassword']));
  620. $url = $digest['scheme'].'://'.$digest['host'].$digest['port'].$digest['path'].'/login';
  621. try{
  622. $options = (localURL($GLOBALS['qBittorrentURL'])) ? array('verify' => false ) : array();
  623. $response = Requests::post($url, array(), $data, $options);
  624. $reflection = new ReflectionClass($response->cookies);
  625. $cookie = $reflection->getProperty("cookies");
  626. $cookie->setAccessible(true);
  627. $cookie = $cookie->getValue($response->cookies);
  628. if($cookie){
  629. $headers = array(
  630. 'Cookie' => 'SID=' . $cookie['SID']->value
  631. );
  632. $reverse = $GLOBALS['qBittorrentReverseSorting'] ? 'true' : 'false';
  633. $url = $digest['scheme'].'://'.$digest['host'].$digest['port'].$digest['path'].'/query/torrents?sort=' . $GLOBALS['qBittorrentSortOrder'] . '&reverse=' . $reverse;
  634. $response = Requests::get($url, $headers, $options);
  635. if($response){
  636. $torrentList = json_decode($response->body, true);
  637. if($GLOBALS['qBittorrentHideSeeding'] || $GLOBALS['qBittorrentHideCompleted']){
  638. $filter = array();
  639. $torrents['arguments']['torrents'] = array();
  640. if($GLOBALS['qBittorrentHideSeeding']){ array_push($filter, 'uploading', 'stalledUP', 'queuedUP'); }
  641. if($GLOBALS['qBittorrentHideCompleted']){ array_push($filter, 'pausedUP'); }
  642. foreach ($torrentList as $key => $value) {
  643. if(!in_array($value['state'], $filter)){
  644. $torrents['arguments']['torrents'][] = $value;
  645. }
  646. }
  647. }else{
  648. $torrents['arguments']['torrents'] = json_decode($response->body, true);
  649. }
  650. $api['content']['queueItems'] = $torrents;
  651. $api['content']['historyItems'] = false;
  652. }
  653. }else{
  654. writeLog('error', 'qBittorrent Connect Function - Error: Could not get session ID', 'SYSTEM');
  655. }
  656. }catch( Requests_Exception $e ) {
  657. writeLog('error', 'qBittorrent Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  658. };
  659. $api['content'] = isset($api['content']) ? $api['content'] : false;
  660. return $api;
  661. }
  662. }
  663. function getCalendar(){
  664. $startDate = date('Y-m-d',strtotime("-".$GLOBALS['calendarStart']." days"));
  665. $endDate = date('Y-m-d',strtotime("+".$GLOBALS['calendarEnd']." days"));
  666. $calendarItems = array();
  667. // SONARR CONNECT
  668. if($GLOBALS['homepageSonarrEnabled'] && qualifyRequest($GLOBALS['homepageSonarrAuth']) && !empty($GLOBALS['sonarrURL']) && !empty($GLOBALS['sonarrToken'])){
  669. $sonarrs = array();
  670. $sonarrURLList = explode(',', $GLOBALS['sonarrURL']);
  671. $sonarrTokenList = explode(',', $GLOBALS['sonarrToken']);
  672. if(count($sonarrURLList) == count($sonarrTokenList)){
  673. foreach ($sonarrURLList as $key => $value) {
  674. $sonarrs[$key] = array(
  675. 'url' => $value,
  676. 'token' => $sonarrTokenList[$key]
  677. );
  678. }
  679. foreach ($sonarrs as $key => $value) {
  680. try {
  681. $sonarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  682. $sonarrCalendar = getSonarrCalendar($sonarr->getCalendar($startDate, $endDate),$key);
  683. } catch (Exception $e) {
  684. writeLog('error', 'Sonarr Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  685. }
  686. if(!empty($sonarrCalendar)) { $calendarItems = array_merge($calendarItems, $sonarrCalendar); }
  687. }
  688. }
  689. }
  690. // RADARR CONNECT
  691. if($GLOBALS['homepageRadarrEnabled'] && qualifyRequest($GLOBALS['homepageRadarrAuth']) && !empty($GLOBALS['radarrURL']) && !empty($GLOBALS['radarrToken'])){
  692. $radarrs = array();
  693. $radarrURLList = explode(',', $GLOBALS['radarrURL']);
  694. $radarrTokenList = explode(',', $GLOBALS['radarrToken']);
  695. if(count($radarrURLList) == count($radarrTokenList)){
  696. foreach ($radarrURLList as $key => $value) {
  697. $radarrs[$key] = array(
  698. 'url' => $value,
  699. 'token' => $radarrTokenList[$key]
  700. );
  701. }
  702. foreach ($radarrs as $key => $value) {
  703. try {
  704. $radarr = new Kryptonit3\Sonarr\Sonarr($value['url'], $value['token']);
  705. $radarrCalendar = getRadarrCalendar($radarr->getCalendar($startDate, $endDate),$key);
  706. } catch (Exception $e) {
  707. writeLog('error', 'Radarr Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  708. }
  709. if(!empty($radarrCalendar)) { $calendarItems = array_merge($calendarItems, $radarrCalendar); }
  710. }
  711. }
  712. }
  713. // SICKRAGE/BEARD/MEDUSA CONNECT
  714. if($GLOBALS['homepageSickrageEnabled'] && qualifyRequest($GLOBALS['homepageSickrageAuth']) && !empty($GLOBALS['sickrageURL']) && !empty($GLOBALS['sickrageToken'])){
  715. $sicks = array();
  716. $sickURLList = explode(',', $GLOBALS['sickrageURL']);
  717. $sickTokenList = explode(',', $GLOBALS['sickrageToken']);
  718. if(count($sickURLList) == count($sickTokenList)){
  719. foreach ($sickURLList as $key => $value) {
  720. $sicks[$key] = array(
  721. 'url' => $value,
  722. 'token' => $sickTokenList[$key]
  723. );
  724. }
  725. foreach ($sicks as $key => $value) {
  726. try {
  727. $sickrage = new Kryptonit3\SickRage\SickRage($value['url'], $value['token']);
  728. $sickrageFuture = getSickrageCalendarWanted($sickrage->future(),$key);
  729. $sickrageHistory = getSickrageCalendarHistory($sickrage->history("100","downloaded"),$key);
  730. if(!empty($sickrageFuture)) { $calendarItems = array_merge($calendarItems, $sickrageFuture); }
  731. if(!empty($sickrageHistory)) { $calendarItems = array_merge($calendarItems, $sickrageHistory); }
  732. } catch (Exception $e) {
  733. writeLog('error', 'Sickrage Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  734. }
  735. }
  736. }
  737. }
  738. // COUCHPOTATO CONNECT
  739. if($GLOBALS['homepageCouchpotatoEnabled'] && qualifyRequest($GLOBALS['homepageCouchpotatoAuth']) && !empty($GLOBALS['couchpotatoURL']) && !empty($GLOBALS['couchpotatoToken'])){
  740. $couchs = array();
  741. $couchpotatoURLList = explode(',', $GLOBALS['couchpotatoURL']);
  742. $couchpotatoTokenList = explode(',', $GLOBALS['couchpotatoToken']);
  743. if(count($couchpotatoURLList) == count($couchpotatoTokenList)){
  744. foreach ($couchpotatoURLList as $key => $value) {
  745. $couchs[$key] = array(
  746. 'url' => $value,
  747. 'token' => $couchpotatoTokenList[$key]
  748. );
  749. }
  750. foreach ($couchs as $key => $value) {
  751. try {
  752. $couchpotato = new Kryptonit3\CouchPotato\CouchPotato($value['url'], $value['token']);
  753. $couchCalendar = getCouchCalendar($couchpotato->getMediaList(),$key);
  754. if(!empty($couchCalendar)) { $calendarItems = array_merge($calendarItems, $couchCalendar); }
  755. } catch (Exception $e) {
  756. writeLog('error', 'Sickrage Connect Function - Error: '.$e->getMessage(), 'SYSTEM');
  757. }
  758. }
  759. }
  760. }
  761. return ($calendarItems) ? $calendarItems : false;
  762. }
  763. function getSonarrCalendar($array,$number){
  764. $array = json_decode($array, true);
  765. $gotCalendar = array();
  766. $i = 0;
  767. foreach($array AS $child) {
  768. $i++;
  769. $seriesName = $child['series']['title'];
  770. $episodeID = $child['series']['tvdbId'];
  771. if(!isset($episodeID)){ $episodeID = ""; }
  772. $episodeName = htmlentities($child['title'], ENT_QUOTES);
  773. if($child['episodeNumber'] == "1"){ $episodePremier = "true"; }else{ $episodePremier = "false"; }
  774. $episodeAirDate = $child['airDateUtc'];
  775. $episodeAirDate = strtotime($episodeAirDate);
  776. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  777. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  778. $downloaded = $child['hasFile'];
  779. if($downloaded == "0" && isset($unaired) && $episodePremier == "true"){ $downloaded = "text-primary"; }elseif($downloaded == "0" && isset($unaired)){ $downloaded = "text-info"; }elseif($downloaded == "1"){ $downloaded = "text-success"; }else{ $downloaded = "text-danger"; }
  780. array_push($gotCalendar, array(
  781. "id" => "Sonarr-".$number."-".$i,
  782. "title" => $seriesName,
  783. "start" => $child['airDateUtc'],
  784. "className" => "bg-calendar tvID--".$episodeID,
  785. "imagetype" => "tv ".$downloaded,
  786. ));
  787. }
  788. if ($i != 0){ return $gotCalendar; }
  789. }
  790. function getRadarrCalendar($array,$number){
  791. $array = json_decode($array, true);
  792. $gotCalendar = array();
  793. $i = 0;
  794. foreach($array AS $child) {
  795. if(isset($child['physicalRelease'])){
  796. $i++;
  797. $movieName = $child['title'];
  798. $movieID = $child['tmdbId'];
  799. if(!isset($movieID)){ $movieID = ""; }
  800. $physicalRelease = $child['physicalRelease'];
  801. $physicalRelease = strtotime($physicalRelease);
  802. $physicalRelease = date("Y-m-d", $physicalRelease);
  803. if (new DateTime() < new DateTime($physicalRelease)) { $notReleased = "true"; }else{ $notReleased = "false"; }
  804. $downloaded = $child['hasFile'];
  805. if($downloaded == "0" && $notReleased == "true"){ $downloaded = "text-info"; }elseif($downloaded == "1"){ $downloaded = "text-success"; }else{ $downloaded = "text-danger"; }
  806. array_push($gotCalendar, array(
  807. "id" => "Radarr-".$number."-".$i,
  808. "title" => $movieName,
  809. "start" => $child['physicalRelease'],
  810. "className" => "bg-calendar movieID--".$movieID,
  811. "imagetype" => "film ".$downloaded,
  812. ));
  813. }
  814. }
  815. if ($i != 0){ return $gotCalendar; }
  816. }
  817. function getCouchCalendar($array,$number){
  818. $api = json_decode($array, true);
  819. $gotCalendar = array();
  820. $i = 0;
  821. foreach($api['movies'] AS $child) {
  822. if($child['status'] == "active" || $child['status'] == "done" ){
  823. $i++;
  824. $movieName = $child['info']['original_title'];
  825. $movieID = $child['info']['tmdb_id'];
  826. if(!isset($movieID)){ $movieID = ""; }
  827. $physicalRelease = (isset($child['info']['released']) ? $child['info']['released'] : null);
  828. $backupRelease = (isset($child['info']['release_date']['theater']) ? $child['info']['release_date']['theater'] : null);
  829. $physicalRelease = (isset($physicalRelease) ? $physicalRelease : $backupRelease);
  830. $physicalRelease = strtotime($physicalRelease);
  831. $physicalRelease = date("Y-m-d", $physicalRelease);
  832. if (new DateTime() < new DateTime($physicalRelease)) { $notReleased = "true"; }else{ $notReleased = "false"; }
  833. $downloaded = ($child['status'] == "active") ? "0" : "1";
  834. if($downloaded == "0" && $notReleased == "true"){ $downloaded = "text-info"; }elseif($downloaded == "1"){ $downloaded = "text-success"; }else{ $downloaded = "text-danger"; }
  835. array_push($gotCalendar, array(
  836. "id" => "CouchPotato-".$number."-".$i,
  837. "title" => $movieName,
  838. "start" => $physicalRelease,
  839. "className" => "bg-calendar movieID--".$movieID,
  840. "imagetype" => "film ".$downloaded,
  841. ));
  842. }
  843. }
  844. if ($i != 0){ return $gotCalendar; }
  845. }
  846. function getSickrageCalendarWanted($array,$number){
  847. $array = json_decode($array, true);
  848. $gotCalendar = array();
  849. $i = 0;
  850. foreach($array['data']['missed'] AS $child) {
  851. $i++;
  852. $seriesName = $child['show_name'];
  853. $episodeID = $child['tvdbid'];
  854. $episodeAirDate = $child['airdate'];
  855. $episodeAirDateTime = explode(" ",$child['airs']);
  856. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2]));
  857. $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime);
  858. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  859. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  860. $downloaded = "0";
  861. if($downloaded == "0" && isset($unaired)){ $downloaded = "text-info"; }elseif($downloaded == "1"){ $downloaded = "text-success";}else{ $downloaded = "text-danger"; }
  862. array_push($gotCalendar, array(
  863. "id" => "Sick-".$number."-Miss-".$i,
  864. "title" => $seriesName,
  865. "start" => $episodeAirDate,
  866. "className" => "bg-calendar tvID--".$episodeID,
  867. "imagetype" => "tv ".$downloaded,
  868. ));
  869. }
  870. foreach($array['data']['today'] AS $child) {
  871. $i++;
  872. $seriesName = $child['show_name'];
  873. $episodeID = $child['tvdbid'];
  874. $episodeAirDate = $child['airdate'];
  875. $episodeAirDateTime = explode(" ",$child['airs']);
  876. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2]));
  877. $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime);
  878. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  879. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  880. $downloaded = "0";
  881. if($downloaded == "0" && isset($unaired)){ $downloaded = "text-info"; }elseif($downloaded == "1"){ $downloaded = "text-success";}else{ $downloaded = "text-danger"; }
  882. array_push($gotCalendar, array(
  883. "id" => "Sick-".$number."-Today-".$i,
  884. "title" => $seriesName,
  885. "start" => $episodeAirDate,
  886. "className" => "bg-calendar tvID--".$episodeID,
  887. "imagetype" => "tv ".$downloaded,
  888. ));
  889. }
  890. foreach($array['data']['soon'] AS $child) {
  891. $i++;
  892. $seriesName = $child['show_name'];
  893. $episodeID = $child['tvdbid'];
  894. $episodeAirDate = $child['airdate'];
  895. $episodeAirDateTime = explode(" ",$child['airs']);
  896. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2]));
  897. $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime);
  898. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  899. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  900. $downloaded = "0";
  901. if($downloaded == "0" && isset($unaired)){ $downloaded = "text-info"; }elseif($downloaded == "1"){ $downloaded = "text-success";}else{ $downloaded = "text-danger"; }
  902. array_push($gotCalendar, array(
  903. "id" => "Sick-".$number."-Soon-".$i,
  904. "title" => $seriesName,
  905. "start" => $episodeAirDate,
  906. "className" => "bg-calendar tvID--".$episodeID,
  907. "imagetype" => "tv ".$downloaded,
  908. ));
  909. }
  910. foreach($array['data']['later'] AS $child) {
  911. $i++;
  912. $seriesName = $child['show_name'];
  913. $episodeID = $child['tvdbid'];
  914. $episodeAirDate = $child['airdate'];
  915. $episodeAirDateTime = explode(" ",$child['airs']);
  916. $episodeAirDateTime = date("H:i:s", strtotime($episodeAirDateTime[1].$episodeAirDateTime[2]));
  917. $episodeAirDate = strtotime($episodeAirDate.$episodeAirDateTime);
  918. $episodeAirDate = date("Y-m-d H:i:s", $episodeAirDate);
  919. if (new DateTime() < new DateTime($episodeAirDate)) { $unaired = true; }
  920. $downloaded = "0";
  921. if($downloaded == "0" && isset($unaired)){ $downloaded = "text-info"; }elseif($downloaded == "1"){ $downloaded = "text-success";}else{ $downloaded = "text-danger"; }
  922. array_push($gotCalendar, array(
  923. "id" => "Sick-".$number."-Later-".$i,
  924. "title" => $seriesName,
  925. "start" => $episodeAirDate,
  926. "className" => "bg-calendar tvID--".$episodeID,
  927. "imagetype" => "tv ".$downloaded,
  928. ));
  929. }
  930. if ($i != 0){ return $gotCalendar; }
  931. }
  932. function getSickrageCalendarHistory($array,$number){
  933. $array = json_decode($array, true);
  934. $gotCalendar = array();
  935. $i = 0;
  936. foreach($array['data'] AS $child) {
  937. $i++;
  938. $seriesName = $child['show_name'];
  939. $episodeID = $child['tvdbid'];
  940. $episodeAirDate = $child['date'];
  941. $downloaded = "text-success";
  942. array_push($gotCalendar, array(
  943. "id" => "Sick-".$number."-History-".$i,
  944. "title" => $seriesName,
  945. "start" => $episodeAirDate,
  946. "className" => "bg-calendar tvID--".$episodeID,
  947. "imagetype" => "tv ".$downloaded,
  948. ));
  949. }
  950. if ($i != 0){ return $gotCalendar; }
  951. }