API.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /*
  3. * Copyright (C) 2021 Echedey López Romero <elr@disroot.org>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. class API {
  19. private static $Instance = null;
  20. private $BaseURL;
  21. private $APIKey;
  22. private function __construct($BaseURL, $APIKey) {
  23. $this->BaseURL = $BaseURL;
  24. $this->APIKey = $APIKey;
  25. }
  26. public static function GetInstance($BaseURL, $APIKey) {
  27. if (self::$Instance == null) {
  28. self::$Instance = new API($BaseURL, $APIKey);
  29. }
  30. return self::$Instance;
  31. }
  32. private function CURL($URL) {
  33. $Request = curl_init();
  34. curl_setopt($Request, CURLOPT_URL, $this->BaseURL . $URL);
  35. curl_setopt($Request, CURLOPT_RETURNTRANSFER, 1);
  36. $Data = curl_exec($Request);
  37. curl_close($Request);
  38. return json_decode($Data, true);
  39. }
  40. public function GetMovies($Query = '') {
  41. $URL = '';
  42. if ($Query !== '') {
  43. $URL = '/search/movie?api_key=' . $this->APIKey
  44. . '&language=en-US&include_adult=true&query='
  45. . urlencode($Query);
  46. } else {
  47. $URL = '/trending/movie/week?api_key=' . $this->APIKey
  48. . '&language=en-US';
  49. }
  50. $Data = $this->CURL($URL);
  51. if (!isset($Data['status_message'])) {
  52. $Results = $Data['results'];
  53. $MoviesList = [];
  54. if (count($Data['results']) !== 0) {
  55. for ($Position = 0; $Position < count($Results); $Position++) {
  56. $Result = $Results[$Position];
  57. $Movie = [];
  58. $Movie['id'] = $Result['id'];
  59. $Movie['title'] = $Result['title'];
  60. $Movie['poster'] = $Result['poster_path'];
  61. $Movie['rate'] = $Result['vote_average'] . ' / 10';
  62. array_push($MoviesList, $Movie);
  63. }
  64. }
  65. return $MoviesList;
  66. } else {
  67. return false;
  68. }
  69. }
  70. public function GetMovie($MovieId) {
  71. $URLDetails = '/movie/' . $MovieId . '?api_key=' . $this->APIKey
  72. . '&language=en-US';
  73. $URLCredits = '/movie/' . $MovieId . '/credits?api_key='
  74. . $this->APIKey . '&language=en-US';
  75. $DataDetails = $this->CURL($URLDetails);
  76. $DataCredits = $this->CURL($URLCredits);
  77. if (!isset($DataDetails['status_message']) && !isset($DataCredits['status_message'])) {
  78. $Cast = isset($DataCredits['cast']) ? $DataCredits['cast'] : [];
  79. $Movie = [];
  80. $Movie['id'] = $DataDetails['id'];
  81. $Movie['title'] = $DataDetails['title'];
  82. $Movie['poster'] = $DataDetails['poster_path'];
  83. $Movie['rate'] = $DataDetails['vote_average'] . ' / 10';
  84. $Movie['release'] = $DataDetails['release_date'];
  85. $Movie['genres'] = [];
  86. for ($Position = 0; $Position < count($DataDetails['genres']); $Position++) {
  87. $Genre = $DataDetails['genres'][$Position];
  88. array_push($Movie['genres'], $Genre['name']);
  89. }
  90. $Movie['plot'] = $DataDetails['overview'];
  91. $Movie['cast'] = [];
  92. for ($Position = 0; $Position < count($Cast); $Position++) {
  93. $PersonData = $Cast[$Position];
  94. $Person = [];
  95. $Person['id'] = $PersonData['id'];
  96. $Person['name'] = $PersonData['name'];
  97. $Person['character'] = $PersonData['character'];
  98. array_push($Movie['cast'], $Person);
  99. }
  100. return $Movie;
  101. } else {
  102. return false;
  103. }
  104. }
  105. public function GetPerson($PersonId) {
  106. $URL = '/person/' . $PersonId . '?api_key=' . $this->APIKey
  107. . '&language=en-US';
  108. $Data = $this->CURL($URL);
  109. if (!isset($Data['status_message'])) {
  110. $Person = [];
  111. $Person['id'] = $Data['id'];
  112. $Person['name'] = $Data['name'];
  113. $Person['profile'] = $Data['profile_path'];
  114. switch ($Data['gender']) {
  115. case 0:
  116. $Person['gender'] = 'Not specified';
  117. break;
  118. case 1:
  119. $Person['gender'] = 'Female';
  120. break;
  121. case 2:
  122. $Person['gender'] = 'Male';
  123. break;
  124. case 3:
  125. $Person['gender'] = 'Non-binary';
  126. break;
  127. }
  128. $Person['birthday'] = $Data['birthday'];
  129. $Person['origin'] = $Data['place_of_birth'];
  130. $Person['biography'] = $Data['biography'];
  131. return $Person;
  132. } else {
  133. return false;
  134. }
  135. }
  136. }