homepage-connect-functions.php 52 KB

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