ombi.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. <?php
  2. trait OmbiHomepageItem
  3. {
  4. public function testConnectionOmbi()
  5. {
  6. if (empty($this->config['ombiURL'])) {
  7. $this->setAPIResponse('error', 'Ombi URL is not defined', 422);
  8. return false;
  9. }
  10. if (empty($this->config['ombiToken'])) {
  11. $this->setAPIResponse('error', 'Ombi Token is not defined', 422);
  12. return false;
  13. }
  14. $headers = array(
  15. "Accept" => "application/json",
  16. "Apikey" => $this->config['ombiToken'],
  17. );
  18. $url = $this->qualifyURL($this->config['ombiURL']);
  19. try {
  20. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  21. $test = Requests::get($url . "/api/v1/Settings/about", $headers, $options);
  22. if ($test->success) {
  23. $this->setAPIResponse('success', 'API Connection succeeded', 200);
  24. return true;
  25. }
  26. } catch (Requests_Exception $e) {
  27. $this->writeLog('error', 'OMBI Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  28. $this->setAPIResponse('error', $e->getMessage(), 500);
  29. return false;
  30. };
  31. }
  32. public function ombiTVDefault($type)
  33. {
  34. return $type == $this->config['ombiTvDefault'];
  35. }
  36. public function getOmbiRequests($type = "both", $limit = 50)
  37. {
  38. if (!$this->config['homepageOmbiEnabled']) {
  39. $this->setAPIResponse('error', 'Ombi homepage item is not enabled', 409);
  40. return false;
  41. }
  42. if (!$this->qualifyRequest($this->config['homepageOmbiAuth'])) {
  43. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  44. return false;
  45. }
  46. if (empty($this->config['ombiURL'])) {
  47. $this->setAPIResponse('error', 'Ombi URL is not defined', 422);
  48. return false;
  49. }
  50. if (empty($this->config['ombiToken'])) {
  51. $this->setAPIResponse('error', 'Ombi Token is not defined', 422);
  52. return false;
  53. }
  54. $api['count'] = array(
  55. 'movie' => 0,
  56. 'tv' => 0,
  57. 'limit' => (integer)$limit
  58. );
  59. $headers = array(
  60. "Accept" => "application/json",
  61. "Apikey" => $this->config['ombiToken'],
  62. );
  63. $requests = array();
  64. $url = $this->qualifyURL($this->config['ombiURL']);
  65. try {
  66. $options = ($this->localURL($url)) ? array('verify' => false) : array();
  67. switch ($type) {
  68. case 'movie':
  69. $movie = Requests::get($url . "/api/v1/Request/movie", $headers, $options);
  70. break;
  71. case 'tv':
  72. $tv = Requests::get($url . "/api/v1/Request/tv", $headers, $options);
  73. break;
  74. default:
  75. $movie = Requests::get($url . "/api/v1/Request/movie", $headers, $options);
  76. $tv = Requests::get($url . "/api/v1/Request/tv", $headers, $options);
  77. break;
  78. }
  79. if ($movie->success || $tv->success) {
  80. if (isset($movie)) {
  81. $movie = json_decode($movie->body, true);
  82. //$movie = array_reverse($movie);
  83. foreach ($movie as $key => $value) {
  84. $proceed = (($this->config['ombiLimitUser']) && strtolower($this->user['username']) == strtolower($value['requestedUser']['userName'])) || (!$this->config['ombiLimitUser']) || $this->qualifyRequest(1);
  85. if ($proceed) {
  86. $api['count']['movie']++;
  87. $requests[] = array(
  88. 'id' => $value['theMovieDbId'],
  89. 'title' => $value['title'],
  90. 'overview' => $value['overview'],
  91. 'poster' => (isset($value['posterPath']) && $value['posterPath'] !== '') ? 'https://image.tmdb.org/t/p/w300/' . $value['posterPath'] : 'plugins/images/cache/no-list.png',
  92. 'background' => (isset($value['background']) && $value['background'] !== '') ? 'https://image.tmdb.org/t/p/w1280/' . $value['background'] : '',
  93. 'approved' => $value['approved'],
  94. 'available' => $value['available'],
  95. 'denied' => $value['denied'],
  96. 'deniedReason' => $value['deniedReason'],
  97. 'user' => $value['requestedUser']['userName'],
  98. 'userAlias' => $value['requestedUser']['userAlias'],
  99. 'request_id' => $value['id'],
  100. 'request_date' => $value['requestedDate'],
  101. 'release_date' => $value['releaseDate'],
  102. 'type' => 'movie',
  103. 'icon' => 'mdi mdi-filmstrip',
  104. 'color' => 'palette-Deep-Purple-900 bg white',
  105. );
  106. }
  107. }
  108. }
  109. if (isset($tv) && (is_array($tv) || is_object($tv))) {
  110. $tv = json_decode($tv->body, true);
  111. foreach ($tv as $key => $value) {
  112. if (count($value['childRequests']) > 0) {
  113. $proceed = (($this->config['ombiLimitUser']) && strtolower($this->user['username']) == strtolower($value['childRequests'][0]['requestedUser']['userName'])) || (!$this->config['ombiLimitUser']) || $this->qualifyRequest(1);
  114. if ($proceed) {
  115. $api['count']['tv']++;
  116. $requests[] = array(
  117. 'id' => $value['tvDbId'],
  118. 'title' => $value['title'],
  119. 'overview' => $value['overview'],
  120. 'poster' => (isset($value['posterPath']) && $value['posterPath'] !== '') ? $value['posterPath'] : 'plugins/images/cache/no-list.png',
  121. 'background' => (isset($value['background']) && $value['background'] !== '') ? 'https://image.tmdb.org/t/p/w1280/' . $value['background'] : '',
  122. 'approved' => $value['childRequests'][0]['approved'],
  123. 'available' => $value['childRequests'][0]['available'],
  124. 'denied' => $value['childRequests'][0]['denied'],
  125. 'deniedReason' => $value['childRequests'][0]['deniedReason'],
  126. 'user' => $value['childRequests'][0]['requestedUser']['userName'],
  127. 'userAlias' => $value['childRequests'][0]['requestedUser']['userAlias'],
  128. 'request_id' => $value['id'],
  129. 'request_date' => $value['childRequests'][0]['requestedDate'],
  130. 'release_date' => $value['releaseDate'],
  131. 'type' => 'tv',
  132. 'icon' => 'mdi mdi-television',
  133. 'color' => 'grayish-blue-bg',
  134. );
  135. }
  136. }
  137. }
  138. }
  139. //sort here
  140. usort($requests, function ($item1, $item2) {
  141. if ($item1['request_date'] == $item2['request_date']) {
  142. return 0;
  143. }
  144. return $item1['request_date'] > $item2['request_date'] ? -1 : 1;
  145. });
  146. }
  147. } catch (Requests_Exception $e) {
  148. $this->writeLog('error', 'OMBI Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  149. $this->setAPIResponse('error', $e->getMessage(), 500);
  150. return false;
  151. };
  152. $api['content'] = isset($requests) ? array_slice($requests, 0, $limit) : false;
  153. $this->setAPIResponse('success', null, 200, $api);
  154. return $api;
  155. }
  156. public function addOmbiRequest($id, $type)
  157. {
  158. $id = ($id) ?? null;
  159. $type = ($type) ?? null;
  160. if (!$id) {
  161. $this->setAPIResponse('error', 'Id was not supplied', 422);
  162. return false;
  163. }
  164. if (!$type) {
  165. $this->setAPIResponse('error', 'Type was not supplied', 422);
  166. return false;
  167. }
  168. if (!$this->config['homepageOmbiEnabled']) {
  169. $this->setAPIResponse('error', 'Ombi homepage item is not enabled', 409);
  170. return false;
  171. }
  172. if (!$this->qualifyRequest($this->config['homepageOmbiAuth'])) {
  173. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  174. return false;
  175. }
  176. if (empty($this->config['ombiURL'])) {
  177. $this->setAPIResponse('error', 'Ombi URL is not defined', 422);
  178. return false;
  179. }
  180. if (empty($this->config['ombiToken'])) {
  181. $this->setAPIResponse('error', 'Ombi Token is not defined', 422);
  182. return false;
  183. }
  184. $url = $this->qualifyURL($this->config['ombiURL']);
  185. switch ($type) {
  186. case 'season':
  187. case 'tv':
  188. $type = 'tv';
  189. $add = array(
  190. 'tvDbId' => $id,
  191. 'requestAll' => $this->ombiTVDefault('all'),
  192. 'latestSeason' => $this->ombiTVDefault('last'),
  193. 'firstSeason' => $this->ombiTVDefault('first')
  194. );
  195. break;
  196. default:
  197. $type = 'movie';
  198. $add = array("theMovieDbId" => (int)$id);
  199. break;
  200. }
  201. try {
  202. $options = ($this->localURL($url)) ? array('verify' => false, 'timeout' => 30) : array('timeout' => 30);
  203. if (isset($_COOKIE['Auth'])) {
  204. $headers = array(
  205. "Accept" => "application/json",
  206. "Content-Type" => "application/json",
  207. "Authorization" => "Bearer " . $_COOKIE['Auth']
  208. );
  209. } else {
  210. $this->setAPIResponse('error', 'User does not have Auth Cookie', 500);
  211. return false;
  212. }
  213. //https://api.themoviedb.org/3/movie/157336?api_key=83cf4ee97bb728eeaf9d4a54e64356a1
  214. // Lets check if it exists inside Ombi first... but since I can't search with ID - i have to query title from id
  215. $tmdbResponse = Requests::get('https://api.themoviedb.org/3/' . $type . '/' . $id . '?api_key=83cf4ee97bb728eeaf9d4a54e64356a1', [], $options);
  216. if ($tmdbResponse->success) {
  217. $details = json_decode($tmdbResponse->body, true);
  218. if (count($details) > 0) {
  219. switch ($type) {
  220. case 'tv':
  221. $title = $details['name'];
  222. $idType = 'theTvDbId';
  223. $tmdbResponseID = Requests::get('https://api.themoviedb.org/3/tv/' . $id . '/external_ids?api_key=83cf4ee97bb728eeaf9d4a54e64356a1', [], $options);
  224. if ($tmdbResponseID->success) {
  225. $detailsID = json_decode($tmdbResponseID->body, true);
  226. if (count($detailsID) > 0) {
  227. if (isset($detailsID['tvdb_id'])) {
  228. $id = $detailsID['tvdb_id'];
  229. $add['tvDbId'] = $id;
  230. } else {
  231. $this->setAPIResponse('error', 'Could not get TVDB Id', 422);
  232. return false;
  233. }
  234. } else {
  235. $this->setAPIResponse('error', 'Could not get TVDB Id', 422);
  236. return false;
  237. }
  238. }
  239. break;
  240. case 'movie':
  241. $title = $details['title'];
  242. $idType = 'theMovieDbId';
  243. break;
  244. default:
  245. $this->setAPIResponse('error', 'Ombi Type was not found', 422);
  246. return false;
  247. }
  248. } else {
  249. $this->setAPIResponse('error', 'No data returned from TMDB', 422);
  250. return false;
  251. }
  252. } else {
  253. $this->setAPIResponse('error', 'Could not contact TMDB', 422);
  254. return false;
  255. }
  256. $searchResponse = Requests::get($url . '/api/v1/Search/' . $type . '/' . urlencode($title), $headers, $options);
  257. if ($searchResponse->success) {
  258. $details = json_decode($searchResponse->body, true);
  259. if (count($details) > 0) {
  260. foreach ($details as $k => $v) {
  261. if ($v[$idType] == $id) {
  262. if ($v['available']) {
  263. $this->setAPIResponse('error', 'Request is already available', 409);
  264. return false;
  265. } elseif ($v['requested']) {
  266. $this->setAPIResponse('error', 'Request is already requested', 409);
  267. return false;
  268. }
  269. }
  270. }
  271. }
  272. } else {
  273. $this->setAPIResponse('error', 'Ombi Error Occurred', 500);
  274. return false;
  275. }
  276. $response = Requests::post($url . "/api/v1/Request/" . $type, $headers, json_encode($add), $options);
  277. if ($response->success) {
  278. $this->setAPIResponse('success', 'Ombi Request submitted', 200);
  279. return true;
  280. } else {
  281. $this->setAPIResponse('error', 'Ombi Error Occurred', 500);
  282. return false;
  283. }
  284. } catch (Requests_Exception $e) {
  285. $this->writeLog('error', 'OMBI Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  286. $this->setAPIResponse('error', $e->getMessage(), 500);
  287. return false;
  288. }
  289. }
  290. public function actionOmbiRequest($id, $type, $action)
  291. {
  292. $id = ($id) ?? null;
  293. $type = ($type) ?? null;
  294. $action = ($action) ?? null;
  295. if (!$id) {
  296. $this->setAPIResponse('error', 'Id was not supplied', 422);
  297. return false;
  298. }
  299. if (!$type) {
  300. $this->setAPIResponse('error', 'Type was not supplied', 422);
  301. return false;
  302. }
  303. if (!$action) {
  304. $this->setAPIResponse('error', 'Action was not supplied', 422);
  305. return false;
  306. }
  307. if (!$this->config['homepageOmbiEnabled']) {
  308. $this->setAPIResponse('error', 'Ombi homepage item is not enabled', 409);
  309. return false;
  310. }
  311. if (!$this->qualifyRequest($this->config['homepageOmbiAuth'])) {
  312. $this->setAPIResponse('error', 'User not approved to view this homepage item', 401);
  313. return false;
  314. }
  315. if (!$this->qualifyRequest(1)) {
  316. $this->setAPIResponse('error', 'User must be an admin', 401);
  317. return false;
  318. }
  319. if (empty($this->config['ombiURL'])) {
  320. $this->setAPIResponse('error', 'Ombi URL is not defined', 422);
  321. return false;
  322. }
  323. if (empty($this->config['ombiToken'])) {
  324. $this->setAPIResponse('error', 'Ombi Token is not defined', 422);
  325. return false;
  326. }
  327. $url = $this->qualifyURL($this->config['ombiURL']);
  328. $headers = array(
  329. "Accept" => "application/json",
  330. "Content-Type" => "application/json",
  331. "Apikey" => $this->config['ombiToken']
  332. );
  333. $data = array(
  334. 'id' => $id,
  335. );
  336. switch ($type) {
  337. case 'season':
  338. case 'tv':
  339. $type = 'tv';
  340. break;
  341. default:
  342. $type = 'movie';
  343. break;
  344. }
  345. try {
  346. $options = ($this->localURL($url)) ? array('verify' => false, 'timeout' => 30) : array('timeout' => 30);
  347. switch ($action) {
  348. case 'approve':
  349. $response = Requests::post($url . "/api/v1/Request/" . $type . "/approve", $headers, json_encode($data), $options);
  350. $message = 'Ombi Request has been approved';
  351. break;
  352. case 'available':
  353. $response = Requests::post($url . "/api/v1/Request/" . $type . "/available", $headers, json_encode($data), $options);
  354. $message = 'Ombi Request has been marked available';
  355. break;
  356. case 'unavailable':
  357. $response = Requests::post($url . "/api/v1/Request/" . $type . "/unavailable", $headers, json_encode($data), $options);
  358. $message = 'Ombi Request has been marked unavailable';
  359. break;
  360. case 'deny':
  361. $response = Requests::put($url . "/api/v1/Request/" . $type . "/deny", $headers, json_encode($data), $options);
  362. $message = 'Ombi Request has been denied';
  363. break;
  364. case 'delete':
  365. $response = Requests::delete($url . "/api/v1/Request/" . $type . "/" . $id, $headers, $options);
  366. $message = 'Ombi Request has been deleted';
  367. break;
  368. default:
  369. return false;
  370. }
  371. if ($response->success) {
  372. $this->setAPIResponse('success', $message, 200);
  373. return true;
  374. } else {
  375. $this->setAPIResponse('error', 'Ombi Error Occurred', 500);
  376. return false;
  377. }
  378. } catch (Requests_Exception $e) {
  379. $this->writeLog('error', 'OMBI Connect Function - Error: ' . $e->getMessage(), 'SYSTEM');
  380. $this->setAPIResponse('error', $e->getMessage(), 500);
  381. return false;
  382. };
  383. }
  384. }