couchpotato.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. trait CouchPotatoHomepageItem
  3. {
  4. public function getCouchPotatoCalendar()
  5. {
  6. if (!$this->config['homepageCouchpotatoEnabled']) {
  7. $this->setAPIResponse('error', 'CouchPotato homepage item is not enabled', 409);
  8. return false;
  9. }
  10. if (!$this->qualifyRequest($this->config['homepageCouchpotatoAuth'])) {
  11. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  12. return false;
  13. }
  14. if (empty($this->config['couchpotatoURL'])) {
  15. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  16. return false;
  17. }
  18. if (empty($this->config['couchpotatoToken'])) {
  19. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  20. return false;
  21. }
  22. $calendarItems = array();
  23. $list = $this->csvHomepageUrlToken($this->config['couchpotatoURL'], $this->config['couchpotatoToken']);
  24. foreach ($list as $key => $value) {
  25. try {
  26. $downloader = new Kryptonit3\CouchPotato\CouchPotato($value['url'], $value['token']);
  27. $calendar = $this->formatCouchCalendar($downloader->getMediaList(array('status' => 'active,done')), $key);
  28. } catch (Exception $e) {
  29. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  30. }
  31. if (!empty($calendar)) {
  32. $calendarItems = array_merge($calendarItems, $calendar);
  33. }
  34. }
  35. $this->setAPIResponse('success', null, 200, $calendarItems);
  36. return $calendarItems;
  37. }
  38. public function formatCouchCalendar($array, $number)
  39. {
  40. $api = json_decode($array, true);
  41. $gotCalendar = array();
  42. $i = 0;
  43. foreach ($api['movies'] as $child) {
  44. $i++;
  45. $movieName = $child['info']['original_title'];
  46. $movieID = $child['info']['tmdb_id'];
  47. if (!isset($movieID)) {
  48. $movieID = "";
  49. }
  50. $physicalRelease = (isset($child['info']['released']) ? $child['info']['released'] : null);
  51. $backupRelease = (isset($child['info']['release_date']['theater']) ? $child['info']['release_date']['theater'] : null);
  52. $physicalRelease = (isset($physicalRelease) ? $physicalRelease : $backupRelease);
  53. $physicalRelease = strtotime($physicalRelease);
  54. $physicalRelease = date("Y-m-d", $physicalRelease);
  55. $oldestDay = new DateTime ($this->currentTime);
  56. $oldestDay->modify('-' . $this->config['calendarStart'] . ' days');
  57. $newestDay = new DateTime ($this->currentTime);
  58. $newestDay->modify('+' . $this->config['calendarEnd'] . ' days');
  59. $startDt = new DateTime ($physicalRelease);
  60. $calendarStartDiff = date_diff($startDt, $newestDay);
  61. $calendarEndDiff = date_diff($startDt, $oldestDay);
  62. if (!$this->calendarDaysCheck($calendarStartDiff->format('%R') . $calendarStartDiff->days, $calendarEndDiff->format('%R') . $calendarEndDiff->days)) {
  63. continue;
  64. }
  65. if (new DateTime() < $startDt) {
  66. $notReleased = "true";
  67. } else {
  68. $notReleased = "false";
  69. }
  70. $downloaded = ($child['status'] == "active") ? "0" : "1";
  71. if ($downloaded == "0" && $notReleased == "true") {
  72. $downloaded = "text-info";
  73. } elseif ($downloaded == "1") {
  74. $downloaded = "text-success";
  75. } else {
  76. $downloaded = "text-danger";
  77. }
  78. if (!empty($child['info']['images']['backdrop_original'])) {
  79. $banner = $child['info']['images']['backdrop_original'][0];
  80. } elseif (!empty($child['info']['images']['backdrop'])) {
  81. $banner = $child['info']['images']['backdrop_original'][0];
  82. } else {
  83. $banner = "/plugins/images/cache/no-np.png";
  84. }
  85. if ($banner !== "/plugins/images/cache/no-np.png") {
  86. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  87. $imageURL = $banner;
  88. $cacheFile = $cacheDirectory . $movieID . '.jpg';
  89. $banner = 'plugins/images/cache/' . $movieID . '.jpg';
  90. if (!file_exists($cacheFile)) {
  91. $this->cacheImage($imageURL, $movieID);
  92. unset($imageURL);
  93. unset($cacheFile);
  94. }
  95. }
  96. $hasFile = (!empty($child['releases']) && !empty($child['releases'][0]['files']['movie']));
  97. $details = array(
  98. "topTitle" => $movieName,
  99. "bottomTitle" => $child['info']['tagline'],
  100. "status" => $child['status'],
  101. "overview" => $child['info']['plot'],
  102. "runtime" => $child['info']['runtime'],
  103. "image" => $banner,
  104. "ratings" => isset($child['info']['rating']['imdb'][0]) ? $child['info']['rating']['imdb'][0] : '',
  105. "videoQuality" => $hasFile ? $child['releases'][0]['quality'] : "unknown",
  106. "audioChannels" => "",
  107. "audioCodec" => "",
  108. "videoCodec" => "",
  109. "genres" => $child['info']['genres'],
  110. "year" => isset($child['info']['year']) ? $child['info']['year'] : '',
  111. "studio" => isset($child['info']['year']) ? $child['info']['year'] : '',
  112. );
  113. array_push($gotCalendar, array(
  114. "id" => "CouchPotato-" . $number . "-" . $i,
  115. "title" => $movieName,
  116. "start" => $physicalRelease,
  117. "className" => "inline-popups bg-calendar calendar-item movieID--" . $movieID,
  118. "imagetype" => "film " . $downloaded,
  119. "imagetypeFilter" => "film",
  120. "downloadFilter" => $downloaded,
  121. "bgColor" => str_replace('text', 'bg', $downloaded),
  122. "details" => $details
  123. ));
  124. }
  125. if ($i != 0) {
  126. return $gotCalendar;
  127. }
  128. return false;
  129. }
  130. }