couchpotato.php 7.7 KB

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