ombi.php 14 KB

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