couchpotato.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 getCouchPotatoCalendar()
  91. {
  92. if (!$this->config['homepageCouchpotatoEnabled']) {
  93. $this->setAPIResponse('error', 'CouchPotato homepage item is not enabled', 409);
  94. return false;
  95. }
  96. if (!$this->qualifyRequest($this->config['homepageCouchpotatoAuth'])) {
  97. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  98. return false;
  99. }
  100. if (empty($this->config['couchpotatoURL'])) {
  101. $this->setAPIResponse('error', 'Radarr URL is not defined', 422);
  102. return false;
  103. }
  104. if (empty($this->config['couchpotatoToken'])) {
  105. $this->setAPIResponse('error', 'Radarr Token is not defined', 422);
  106. return false;
  107. }
  108. $calendarItems = array();
  109. $list = $this->csvHomepageUrlToken($this->config['couchpotatoURL'], $this->config['couchpotatoToken']);
  110. foreach ($list as $key => $value) {
  111. try {
  112. $downloader = new Kryptonit3\CouchPotato\CouchPotato($value['url'], $value['token']);
  113. $calendar = $this->formatCouchCalendar($downloader->getMediaList(array('status' => 'active,done')), $key);
  114. } catch (Exception $e) {
  115. $this->writeLog('error', 'Radarr Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  116. }
  117. if (!empty($calendar)) {
  118. $calendarItems = array_merge($calendarItems, $calendar);
  119. }
  120. }
  121. $this->setAPIResponse('success', null, 200, $calendarItems);
  122. return $calendarItems;
  123. }
  124. public function formatCouchCalendar($array, $number)
  125. {
  126. $api = json_decode($array, true);
  127. $gotCalendar = array();
  128. $i = 0;
  129. foreach ($api['movies'] as $child) {
  130. $i++;
  131. $movieName = $child['info']['original_title'];
  132. $movieID = $child['info']['tmdb_id'];
  133. if (!isset($movieID)) {
  134. $movieID = "";
  135. }
  136. $physicalRelease = (isset($child['info']['released']) ? $child['info']['released'] : null);
  137. $backupRelease = (isset($child['info']['release_date']['theater']) ? $child['info']['release_date']['theater'] : null);
  138. $physicalRelease = (isset($physicalRelease) ? $physicalRelease : $backupRelease);
  139. $physicalRelease = strtotime($physicalRelease);
  140. $physicalRelease = date("Y-m-d", $physicalRelease);
  141. $oldestDay = new DateTime ($this->currentTime);
  142. $oldestDay->modify('-' . $this->config['calendarStart'] . ' days');
  143. $newestDay = new DateTime ($this->currentTime);
  144. $newestDay->modify('+' . $this->config['calendarEnd'] . ' days');
  145. $startDt = new DateTime ($physicalRelease);
  146. $calendarStartDiff = date_diff($startDt, $newestDay);
  147. $calendarEndDiff = date_diff($startDt, $oldestDay);
  148. if (!$this->calendarDaysCheck($calendarStartDiff->format('%R') . $calendarStartDiff->days, $calendarEndDiff->format('%R') . $calendarEndDiff->days)) {
  149. continue;
  150. }
  151. if (new DateTime() < $startDt) {
  152. $notReleased = "true";
  153. } else {
  154. $notReleased = "false";
  155. }
  156. $downloaded = ($child['status'] == "active") ? "0" : "1";
  157. if ($downloaded == "0" && $notReleased == "true") {
  158. $downloaded = "text-info";
  159. } elseif ($downloaded == "1") {
  160. $downloaded = "text-success";
  161. } else {
  162. $downloaded = "text-danger";
  163. }
  164. if (!empty($child['info']['images']['backdrop_original'])) {
  165. $banner = $child['info']['images']['backdrop_original'][0];
  166. } elseif (!empty($child['info']['images']['backdrop'])) {
  167. $banner = $child['info']['images']['backdrop_original'][0];
  168. } else {
  169. $banner = "/plugins/images/cache/no-np.png";
  170. }
  171. if ($banner !== "/plugins/images/cache/no-np.png") {
  172. $cacheDirectory = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  173. $imageURL = $banner;
  174. $cacheFile = $cacheDirectory . $movieID . '.jpg';
  175. $banner = 'plugins/images/cache/' . $movieID . '.jpg';
  176. if (!file_exists($cacheFile)) {
  177. $this->cacheImage($imageURL, $movieID);
  178. unset($imageURL);
  179. unset($cacheFile);
  180. }
  181. }
  182. $hasFile = (!empty($child['releases']) && !empty($child['releases'][0]['files']['movie']));
  183. $details = array(
  184. "topTitle" => $movieName,
  185. "bottomTitle" => $child['info']['tagline'],
  186. "status" => $child['status'],
  187. "overview" => $child['info']['plot'],
  188. "runtime" => $child['info']['runtime'],
  189. "image" => $banner,
  190. "ratings" => isset($child['info']['rating']['imdb'][0]) ? $child['info']['rating']['imdb'][0] : '',
  191. "videoQuality" => $hasFile ? $child['releases'][0]['quality'] : "unknown",
  192. "audioChannels" => "",
  193. "audioCodec" => "",
  194. "videoCodec" => "",
  195. "genres" => $child['info']['genres'],
  196. "year" => isset($child['info']['year']) ? $child['info']['year'] : '',
  197. "studio" => isset($child['info']['year']) ? $child['info']['year'] : '',
  198. );
  199. array_push($gotCalendar, array(
  200. "id" => "CouchPotato-" . $number . "-" . $i,
  201. "title" => $movieName,
  202. "start" => $physicalRelease,
  203. "className" => "inline-popups bg-calendar calendar-item movieID--" . $movieID,
  204. "imagetype" => "film " . $downloaded,
  205. "imagetypeFilter" => "film",
  206. "downloadFilter" => $downloaded,
  207. "bgColor" => str_replace('text', 'bg', $downloaded),
  208. "details" => $details
  209. ));
  210. }
  211. if ($i != 0) {
  212. return $gotCalendar;
  213. }
  214. return false;
  215. }
  216. }