couchpotato.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. if (array_key_exists($key, $permissions)) {
  60. return $permissions[$key];
  61. } elseif ($key == 'all') {
  62. return $permissions;
  63. } else {
  64. return [];
  65. }
  66. }
  67. public function getCouchPotatoCalendar()
  68. {
  69. if (!$this->homepageItemPermissions($this->couchPotatoHomepagePermissions('calendar'), true)) {
  70. return false;
  71. }
  72. $calendarItems = array();
  73. $list = $this->csvHomepageUrlToken($this->config['couchpotatoURL'], $this->config['couchpotatoToken']);
  74. foreach ($list as $key => $value) {
  75. try {
  76. $options = $this->requestOptions($value['url'], 60, $this->config['couchpotatoDisableCertCheck'], $this->config['couchpotatoUseCustomCertificate']);
  77. $downloader = new Kryptonit3\CouchPotato\CouchPotato($value['url'], $value['token'], null, null, $options);
  78. $calendar = $this->formatCouchCalendar($downloader->getMediaList(array('status' => 'active,done')), $key);
  79. } catch (Exception $e) {
  80. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  81. }
  82. if (!empty($calendar)) {
  83. $calendarItems = array_merge($calendarItems, $calendar);
  84. }
  85. }
  86. $this->setAPIResponse('success', null, 200, $calendarItems);
  87. return $calendarItems;
  88. }
  89. public function formatCouchCalendar($array, $number)
  90. {
  91. $api = json_decode($array, true);
  92. $gotCalendar = array();
  93. $i = 0;
  94. foreach ($api['movies'] as $child) {
  95. $i++;
  96. $movieName = $child['info']['original_title'];
  97. $movieID = $child['info']['tmdb_id'];
  98. if (!isset($movieID)) {
  99. $movieID = "";
  100. }
  101. $physicalRelease = (isset($child['info']['released']) ? $child['info']['released'] : null);
  102. $backupRelease = (isset($child['info']['release_date']['theater']) ? $child['info']['release_date']['theater'] : null);
  103. $physicalRelease = (isset($physicalRelease) ? $physicalRelease : $backupRelease);
  104. $physicalRelease = strtotime($physicalRelease);
  105. $physicalRelease = date("Y-m-d", $physicalRelease);
  106. $oldestDay = new DateTime ($this->currentTime);
  107. $oldestDay->modify('-' . $this->config['calendarStart'] . ' days');
  108. $newestDay = new DateTime ($this->currentTime);
  109. $newestDay->modify('+' . $this->config['calendarEnd'] . ' days');
  110. $startDt = new DateTime ($physicalRelease);
  111. $calendarStartDiff = date_diff($startDt, $newestDay);
  112. $calendarEndDiff = date_diff($startDt, $oldestDay);
  113. if (!$this->calendarDaysCheck($calendarStartDiff->format('%R') . $calendarStartDiff->days, $calendarEndDiff->format('%R') . $calendarEndDiff->days)) {
  114. continue;
  115. }
  116. if (new DateTime() < $startDt) {
  117. $notReleased = "true";
  118. } else {
  119. $notReleased = "false";
  120. }
  121. $downloaded = ($child['status'] == "active") ? "0" : "1";
  122. if ($downloaded == "0" && $notReleased == "true") {
  123. $downloaded = "text-info";
  124. } elseif ($downloaded == "1") {
  125. $downloaded = "text-success";
  126. } else {
  127. $downloaded = "text-danger";
  128. }
  129. if (!empty($child['info']['images']['backdrop_original'])) {
  130. $banner = $child['info']['images']['backdrop_original'][0];
  131. } elseif (!empty($child['info']['images']['backdrop'])) {
  132. $banner = $child['info']['images']['backdrop_original'][0];
  133. } else {
  134. $banner = "/plugins/images/cache/no-np.png";
  135. }
  136. if ($banner !== "/plugins/images/cache/no-np.png") {
  137. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  138. $imageURL = $banner;
  139. $cacheFile = $cacheDirectory . $movieID . '.jpg';
  140. $banner = 'plugins/images/cache/' . $movieID . '.jpg';
  141. if (!file_exists($cacheFile)) {
  142. $this->cacheImage($imageURL, $movieID);
  143. unset($imageURL);
  144. unset($cacheFile);
  145. }
  146. }
  147. $hasFile = (!empty($child['releases']) && !empty($child['releases'][0]['files']['movie']));
  148. $details = array(
  149. "topTitle" => $movieName,
  150. "bottomTitle" => $child['info']['tagline'],
  151. "status" => $child['status'],
  152. "overview" => $child['info']['plot'],
  153. "runtime" => $child['info']['runtime'],
  154. "image" => $banner,
  155. "ratings" => isset($child['info']['rating']['imdb'][0]) ? $child['info']['rating']['imdb'][0] : '',
  156. "videoQuality" => $hasFile ? $child['releases'][0]['quality'] : "unknown",
  157. "audioChannels" => "",
  158. "audioCodec" => "",
  159. "videoCodec" => "",
  160. "genres" => $child['info']['genres'],
  161. "year" => isset($child['info']['year']) ? $child['info']['year'] : '',
  162. "studio" => isset($child['info']['year']) ? $child['info']['year'] : '',
  163. );
  164. array_push($gotCalendar, array(
  165. "id" => "CouchPotato-" . $number . "-" . $i,
  166. "title" => $movieName,
  167. "start" => $physicalRelease,
  168. "className" => "inline-popups bg-calendar calendar-item movieID--" . $movieID,
  169. "imagetype" => "film " . $downloaded,
  170. "imagetypeFilter" => "film",
  171. "downloadFilter" => $downloaded,
  172. "bgColor" => str_replace('text', 'bg', $downloaded),
  173. "details" => $details
  174. ));
  175. }
  176. if ($i != 0) {
  177. return $gotCalendar;
  178. }
  179. return false;
  180. }
  181. }