couchpotato.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. trait CouchPotatoHomepageItem
  3. {
  4. public function couchPotatoSettingsArray($infoOnly = false)
  5. {
  6. $homepageInformation = [
  7. 'name' => 'CouchPotato',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/couchpotato.png',
  10. 'category' => 'PVR',
  11. 'settingsArray' => __FUNCTION__
  12. ];
  13. if ($infoOnly) {
  14. return $homepageInformation;
  15. }
  16. $homepageSettings = [
  17. 'debug' => true,
  18. 'settings' => [
  19. 'Enable' => [
  20. $this->settingsOption('enable', 'homepageCouchpotatoEnabled'),
  21. $this->settingsOption('auth', 'homepageCouchpotatoAuth'),
  22. ],
  23. 'Connection' => [
  24. $this->settingsOption('multiple-url', 'couchpotatoURL'),
  25. $this->settingsOption('multiple-token', 'couchpotatoToken'),
  26. $this->settingsOption('disable-cert-check', 'couchpotatoDisableCertCheck'),
  27. $this->settingsOption('use-custom-certificate', 'couchpotatoUseCustomCertificate'),
  28. ],
  29. 'Misc Options' => [
  30. $this->settingsOption('calendar-start', 'calendarStart'),
  31. $this->settingsOption('calendar-end', 'calendarEnd'),
  32. $this->settingsOption('calendar-starting-day', 'calendarFirstDay'),
  33. $this->settingsOption('calendar-default-view', 'calendarDefault'),
  34. $this->settingsOption('calendar-time-format', 'calendarTimeFormat'),
  35. $this->settingsOption('calendar-locale', 'calendarLocale'),
  36. $this->settingsOption('calendar-limit', 'calendarLimit'),
  37. $this->settingsOption('refresh', 'calendarRefresh'),
  38. ]
  39. ]
  40. ];
  41. return array_merge($homepageInformation, $homepageSettings);
  42. }
  43. public function couchPotatoHomepagePermissions($key = null)
  44. {
  45. $permissions = [
  46. 'calendar' => [
  47. 'enabled' => [
  48. 'homepageCouchpotatoEnabled'
  49. ],
  50. 'auth' => [
  51. 'homepageCouchpotatoAuth'
  52. ],
  53. 'not_empty' => [
  54. 'couchpotatoURL',
  55. 'couchpotatoToken'
  56. ]
  57. ]
  58. ];
  59. return $this->homepageCheckKeyPermissions($key, $permissions);
  60. }
  61. public function getCouchPotatoCalendar()
  62. {
  63. if (!$this->homepageItemPermissions($this->couchPotatoHomepagePermissions('calendar'), true)) {
  64. return false;
  65. }
  66. $calendarItems = array();
  67. $list = $this->csvHomepageUrlToken($this->config['couchpotatoURL'], $this->config['couchpotatoToken']);
  68. foreach ($list as $key => $value) {
  69. try {
  70. $options = $this->requestOptions($value['url'], 60, $this->config['couchpotatoDisableCertCheck'], $this->config['couchpotatoUseCustomCertificate']);
  71. $downloader = new Kryptonit3\CouchPotato\CouchPotato($value['url'], $value['token'], null, null, $options);
  72. $calendar = $this->formatCouchCalendar($downloader->getMediaList(array('status' => 'active,done')), $key);
  73. } catch (Exception $e) {
  74. $this->setLoggerChannel('Radarr')->error($e);
  75. }
  76. if (!empty($calendar)) {
  77. $calendarItems = array_merge($calendarItems, $calendar);
  78. }
  79. }
  80. $this->setAPIResponse('success', null, 200, $calendarItems);
  81. return $calendarItems;
  82. }
  83. public function formatCouchCalendar($array, $number)
  84. {
  85. $api = json_decode($array, true);
  86. $gotCalendar = array();
  87. $i = 0;
  88. foreach ($api['movies'] as $child) {
  89. $i++;
  90. $movieName = $child['info']['original_title'];
  91. $movieID = $child['info']['tmdb_id'];
  92. if (!isset($movieID)) {
  93. $movieID = "";
  94. }
  95. $physicalRelease = (isset($child['info']['released']) ? $child['info']['released'] : null);
  96. $backupRelease = (isset($child['info']['release_date']['theater']) ? $child['info']['release_date']['theater'] : null);
  97. $physicalRelease = (isset($physicalRelease) ? $physicalRelease : $backupRelease);
  98. $physicalRelease = strtotime($physicalRelease);
  99. $physicalRelease = date("Y-m-d", $physicalRelease);
  100. $oldestDay = new DateTime ($this->currentTime);
  101. $oldestDay->modify('-' . $this->config['calendarStart'] . ' days');
  102. $newestDay = new DateTime ($this->currentTime);
  103. $newestDay->modify('+' . $this->config['calendarEnd'] . ' days');
  104. $startDt = new DateTime ($physicalRelease);
  105. $calendarStartDiff = date_diff($startDt, $newestDay);
  106. $calendarEndDiff = date_diff($startDt, $oldestDay);
  107. if (!$this->calendarDaysCheck($calendarStartDiff->format('%R') . $calendarStartDiff->days, $calendarEndDiff->format('%R') . $calendarEndDiff->days)) {
  108. continue;
  109. }
  110. if (new DateTime() < $startDt) {
  111. $notReleased = "true";
  112. } else {
  113. $notReleased = "false";
  114. }
  115. $downloaded = ($child['status'] == "active") ? "0" : "1";
  116. if ($downloaded == "0" && $notReleased == "true") {
  117. $downloaded = "text-info";
  118. } elseif ($downloaded == "1") {
  119. $downloaded = "text-success";
  120. } else {
  121. $downloaded = "text-danger";
  122. }
  123. if (!empty($child['info']['images']['backdrop_original'])) {
  124. $banner = $child['info']['images']['backdrop_original'][0];
  125. } elseif (!empty($child['info']['images']['backdrop'])) {
  126. $banner = $child['info']['images']['backdrop_original'][0];
  127. } else {
  128. $banner = "/plugins/images/homepage/no-np.png";
  129. }
  130. if ($banner !== "/plugins/images/homepage/no-np.png") {
  131. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'data' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  132. $imageURL = $banner;
  133. $cacheFile = $cacheDirectory . $movieID . '.jpg';
  134. $banner = 'data/cache/' . $movieID . '.jpg';
  135. if (!file_exists($cacheFile)) {
  136. $this->cacheImage($imageURL, $movieID);
  137. unset($imageURL);
  138. unset($cacheFile);
  139. }
  140. }
  141. $hasFile = (!empty($child['releases']) && !empty($child['releases'][0]['files']['movie']));
  142. $details = array(
  143. "topTitle" => $movieName,
  144. "bottomTitle" => $child['info']['tagline'],
  145. "status" => $child['status'],
  146. "overview" => $child['info']['plot'],
  147. "runtime" => $child['info']['runtime'],
  148. "image" => $banner,
  149. "ratings" => isset($child['info']['rating']['imdb'][0]) ? $child['info']['rating']['imdb'][0] : '',
  150. "videoQuality" => $hasFile ? $child['releases'][0]['quality'] : "unknown",
  151. "audioChannels" => "",
  152. "audioCodec" => "",
  153. "videoCodec" => "",
  154. "genres" => $child['info']['genres'],
  155. "year" => isset($child['info']['year']) ? $child['info']['year'] : '',
  156. "studio" => isset($child['info']['year']) ? $child['info']['year'] : '',
  157. );
  158. array_push($gotCalendar, array(
  159. "id" => "CouchPotato-" . $number . "-" . $i,
  160. "title" => $movieName,
  161. "start" => $physicalRelease,
  162. "className" => "inline-popups bg-calendar calendar-item movieID--" . $movieID,
  163. "imagetype" => "film " . $downloaded,
  164. "imagetypeFilter" => "film",
  165. "downloadFilter" => $downloaded,
  166. "bgColor" => str_replace('text', 'bg', $downloaded),
  167. "details" => $details
  168. ));
  169. }
  170. if ($i != 0) {
  171. return $gotCalendar;
  172. }
  173. return false;
  174. }
  175. }