couchpotato.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. trait CouchPotatoHomepageItem
  3. {
  4. public function couchPotatoSettingsArray()
  5. {
  6. return array(
  7. 'name' => 'CouchPotato',
  8. 'enabled' => strpos('personal', $this->config['license']) !== false,
  9. 'image' => 'plugins/images/tabs/couchpotato.png',
  10. 'category' => 'PVR',
  11. 'settings' => array(
  12. 'Enable' => array(
  13. array(
  14. 'type' => 'switch',
  15. 'name' => 'homepageCouchpotatoEnabled',
  16. 'label' => 'Enable',
  17. 'value' => $this->config['homepageCouchpotatoEnabled']
  18. ),
  19. array(
  20. 'type' => 'select',
  21. 'name' => 'homepageCouchpotatoAuth',
  22. 'label' => 'Minimum Authentication',
  23. 'value' => $this->config['homepageCouchpotatoAuth'],
  24. 'options' => $this->groupOptions
  25. )
  26. ),
  27. 'Connection' => array(
  28. array(
  29. 'type' => 'input',
  30. 'name' => 'couchpotatoURL',
  31. 'label' => 'URL',
  32. 'value' => $this->config['couchpotatoURL'],
  33. 'help' => 'Please make sure to use local IP address and port - You also may use local dns name too.',
  34. 'placeholder' => 'http(s)://hostname:port'
  35. ),
  36. array(
  37. 'type' => 'password-alt',
  38. 'name' => 'couchpotatoToken',
  39. 'label' => 'Token',
  40. 'value' => $this->config['couchpotatoToken']
  41. )
  42. ),
  43. 'Misc Options' => array(
  44. array(
  45. 'type' => 'select',
  46. 'name' => 'calendarFirstDay',
  47. 'label' => 'Start Day',
  48. 'value' => $this->config['calendarFirstDay'],
  49. 'options' => $this->daysOptions()
  50. ),
  51. array(
  52. 'type' => 'select',
  53. 'name' => 'calendarDefault',
  54. 'label' => 'Default View',
  55. 'value' => $this->config['calendarDefault'],
  56. 'options' => $this->calendarDefaultOptions()
  57. ),
  58. array(
  59. 'type' => 'select',
  60. 'name' => 'calendarTimeFormat',
  61. 'label' => 'Time Format',
  62. 'value' => $this->config['calendarTimeFormat'],
  63. 'options' => $this->timeFormatOptions()
  64. ),
  65. array(
  66. 'type' => 'select',
  67. 'name' => 'calendarLocale',
  68. 'label' => 'Locale',
  69. 'value' => $this->config['calendarLocale'],
  70. 'options' => $this->calendarLocaleOptions()
  71. ),
  72. array(
  73. 'type' => 'select',
  74. 'name' => 'calendarLimit',
  75. 'label' => 'Items Per Day',
  76. 'value' => $this->config['calendarLimit'],
  77. 'options' => $this->limitOptions()
  78. ),
  79. array(
  80. 'type' => 'select',
  81. 'name' => 'calendarRefresh',
  82. 'label' => 'Refresh Seconds',
  83. 'value' => $this->config['calendarRefresh'],
  84. 'options' => $this->timeOptions()
  85. )
  86. )
  87. )
  88. );
  89. }
  90. public function couchPotatoHomepagePermissions($key = null)
  91. {
  92. $permissions = [
  93. 'calendar' => [
  94. 'enabled' => [
  95. 'homepageCouchpotatoEnabled'
  96. ],
  97. 'auth' => [
  98. 'homepageCouchpotatoAuth'
  99. ],
  100. 'not_empty' => [
  101. 'couchpotatoURL',
  102. 'couchpotatoToken'
  103. ]
  104. ]
  105. ];
  106. if (array_key_exists($key, $permissions)) {
  107. return $permissions[$key];
  108. } elseif ($key == 'all') {
  109. return $permissions;
  110. } else {
  111. return [];
  112. }
  113. }
  114. public function getCouchPotatoCalendar()
  115. {
  116. if (!$this->homepageItemPermissions($this->couchPotatoHomepagePermissions('calendar'), true)) {
  117. return false;
  118. }
  119. $calendarItems = array();
  120. $list = $this->csvHomepageUrlToken($this->config['couchpotatoURL'], $this->config['couchpotatoToken']);
  121. foreach ($list as $key => $value) {
  122. try {
  123. $downloader = new Kryptonit3\CouchPotato\CouchPotato($value['url'], $value['token']);
  124. $calendar = $this->formatCouchCalendar($downloader->getMediaList(array('status' => 'active,done')), $key);
  125. } catch (Exception $e) {
  126. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  127. }
  128. if (!empty($calendar)) {
  129. $calendarItems = array_merge($calendarItems, $calendar);
  130. }
  131. }
  132. $this->setAPIResponse('success', null, 200, $calendarItems);
  133. return $calendarItems;
  134. }
  135. public function formatCouchCalendar($array, $number)
  136. {
  137. $api = json_decode($array, true);
  138. $gotCalendar = array();
  139. $i = 0;
  140. foreach ($api['movies'] as $child) {
  141. $i++;
  142. $movieName = $child['info']['original_title'];
  143. $movieID = $child['info']['tmdb_id'];
  144. if (!isset($movieID)) {
  145. $movieID = "";
  146. }
  147. $physicalRelease = (isset($child['info']['released']) ? $child['info']['released'] : null);
  148. $backupRelease = (isset($child['info']['release_date']['theater']) ? $child['info']['release_date']['theater'] : null);
  149. $physicalRelease = (isset($physicalRelease) ? $physicalRelease : $backupRelease);
  150. $physicalRelease = strtotime($physicalRelease);
  151. $physicalRelease = date("Y-m-d", $physicalRelease);
  152. $oldestDay = new DateTime ($this->currentTime);
  153. $oldestDay->modify('-' . $this->config['calendarStart'] . ' days');
  154. $newestDay = new DateTime ($this->currentTime);
  155. $newestDay->modify('+' . $this->config['calendarEnd'] . ' days');
  156. $startDt = new DateTime ($physicalRelease);
  157. $calendarStartDiff = date_diff($startDt, $newestDay);
  158. $calendarEndDiff = date_diff($startDt, $oldestDay);
  159. if (!$this->calendarDaysCheck($calendarStartDiff->format('%R') . $calendarStartDiff->days, $calendarEndDiff->format('%R') . $calendarEndDiff->days)) {
  160. continue;
  161. }
  162. if (new DateTime() < $startDt) {
  163. $notReleased = "true";
  164. } else {
  165. $notReleased = "false";
  166. }
  167. $downloaded = ($child['status'] == "active") ? "0" : "1";
  168. if ($downloaded == "0" && $notReleased == "true") {
  169. $downloaded = "text-info";
  170. } elseif ($downloaded == "1") {
  171. $downloaded = "text-success";
  172. } else {
  173. $downloaded = "text-danger";
  174. }
  175. if (!empty($child['info']['images']['backdrop_original'])) {
  176. $banner = $child['info']['images']['backdrop_original'][0];
  177. } elseif (!empty($child['info']['images']['backdrop'])) {
  178. $banner = $child['info']['images']['backdrop_original'][0];
  179. } else {
  180. $banner = "/plugins/images/cache/no-np.png";
  181. }
  182. if ($banner !== "/plugins/images/cache/no-np.png") {
  183. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  184. $imageURL = $banner;
  185. $cacheFile = $cacheDirectory . $movieID . '.jpg';
  186. $banner = 'plugins/images/cache/' . $movieID . '.jpg';
  187. if (!file_exists($cacheFile)) {
  188. $this->cacheImage($imageURL, $movieID);
  189. unset($imageURL);
  190. unset($cacheFile);
  191. }
  192. }
  193. $hasFile = (!empty($child['releases']) && !empty($child['releases'][0]['files']['movie']));
  194. $details = array(
  195. "topTitle" => $movieName,
  196. "bottomTitle" => $child['info']['tagline'],
  197. "status" => $child['status'],
  198. "overview" => $child['info']['plot'],
  199. "runtime" => $child['info']['runtime'],
  200. "image" => $banner,
  201. "ratings" => isset($child['info']['rating']['imdb'][0]) ? $child['info']['rating']['imdb'][0] : '',
  202. "videoQuality" => $hasFile ? $child['releases'][0]['quality'] : "unknown",
  203. "audioChannels" => "",
  204. "audioCodec" => "",
  205. "videoCodec" => "",
  206. "genres" => $child['info']['genres'],
  207. "year" => isset($child['info']['year']) ? $child['info']['year'] : '',
  208. "studio" => isset($child['info']['year']) ? $child['info']['year'] : '',
  209. );
  210. array_push($gotCalendar, array(
  211. "id" => "CouchPotato-" . $number . "-" . $i,
  212. "title" => $movieName,
  213. "start" => $physicalRelease,
  214. "className" => "inline-popups bg-calendar calendar-item movieID--" . $movieID,
  215. "imagetype" => "film " . $downloaded,
  216. "imagetypeFilter" => "film",
  217. "downloadFilter" => $downloaded,
  218. "bgColor" => str_replace('text', 'bg', $downloaded),
  219. "details" => $details
  220. ));
  221. }
  222. if ($i != 0) {
  223. return $gotCalendar;
  224. }
  225. return false;
  226. }
  227. }