couchpotato.php 7.7 KB

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