url = Str::endsWith($url, '/') ? $url : $url . '/'; $this->per_page = $per_page; $this->use_cache = $use_cache; $this->cache_time = $cache_time; $this->domain_src = $domain_src; } public function getMovie(int $id) { $action = sprintf('movie/%s', $id); if ($this->use_cache === true) { $key = $action . $this->page . $this->language; $results = Cache::remember($key, $this->cache_time, function () use ($action) { return $this->_call($action); }); } else { $results = $this->_call($action); } return $results; } public function getMoviesByTag(String $title, $page = 1) { $path = sprintf('movie/tag/%s', $this->per_page); $options = [ 'page' => $page, 'title' => urlencode(Str::of($title)->replace('-', ' ')->title()) ]; $key = Str::slug(implode(' ', [$path, $title, $page]), '_'); if ($this->use_cache === true) { $results = Cache::remember($key, $this->cache_time, function () use ($path, $options, $key) { return $this->_call($path, $options); }); } else { $results = $this->_call($path, $options); } return $results; } public function getShowsByTag(String $title, $page = 1) { $path = sprintf('show/tag/%s', $this->per_page); $options = [ 'page' => $page, 'title' => urlencode(Str::of($title)->replace('-', ' ')->title()) ]; $key = Str::slug(implode(' ', [$path, $title, $page]), '_'); if ($this->use_cache === true) { $results = Cache::remember($key, $this->cache_time, function () use ($path, $options, $key) { return $this->_call($path, $options); }); } else { $results = $this->_call($path, $options); } return $results; } public function getPopularMovies($page = 1, $per_page = null) { $per_page = is_null($per_page) ? $this->per_page : $per_page; $path = 'movie/popular/' . $this->domain_src . '/' . $per_page; $options = compact('page'); $key = Str::slug($path . ' ' . $page, '_'); if ($this->use_cache === true) { $results = Cache::remember($key, $this->cache_time, function () use ($path, $options) { return $this->_call($path, $options); }); } else { $results = $this->_call($path, $options); } return $results; } public function getPopularShows($page = 1, $per_page = null) { $per_page = is_null($per_page) ? $this->per_page : $per_page; $path = 'show/popular/' . $this->domain_src . '/' . $per_page; $options = compact('page'); $key = Str::slug($path . ' ' . $page, '_'); if ($this->use_cache === true) { $results = Cache::remember($key, $this->cache_time, function () use ($path, $options) { return $this->_call($path, $options); }); } else { $results = $this->_call($path, $options); } return $results; } public function getTrendingMovies($page = 1, $per_page = null) { $per_page = is_null($per_page) ? $this->per_page : $per_page; $path = 'movie/trending/' . $this->domain_src . '/' . $per_page; $options = compact('page'); $key = Str::slug($path . ' ' . $page, '_'); if ($this->use_cache === true) { $results = Cache::remember($key, $this->cache_time, function () use ($path, $options) { return $this->_call($path, $options); }); } else { $results = $this->_call($path, $options); } return $results; } public function getTrendingShows($page = 1, $per_page = null) { $per_page = is_null($per_page) ? $this->per_page : $per_page; $path = 'show/trending/' . $this->domain_src . '/' . $per_page; $options = compact('page'); $key = Str::slug($path . ' ' . $page, '_'); if ($this->use_cache === true) { $results = Cache::remember($key, $this->cache_time, function () use ($path, $options) { return $this->_call($path, $options); }); } else { $results = $this->_call($path, $options); } return $results; } public function getGenreMovies($id, $page = 1, $per_page = null) { $per_page = is_null($per_page) ? $this->per_page : $per_page; $path = 'movie/popular/' . $this->domain_src . '/' . $per_page; $options = [ 'page' => $page, 'genre_id' => $id ]; $key = Str::slug(implode(' ', [$path, $id, $page, $per_page]), '_'); if ($this->use_cache === true) { $results = Cache::remember($key, $this->cache_time, function () use ($path, $options) { return $this->_call($path, $options); }); } else { $results = $this->_call($path, $options); } return $results; } public function getGenreShows($id, $page = 1, $per_page = null) { $per_page = is_null($per_page) ? $this->per_page : $per_page; $path = 'show/popular/' . $this->domain_src . '/' . $per_page; $options = [ 'page' => $page, 'genre_id' => $id ]; $key = Str::slug(implode(' ', [$path, $id, $page, $per_page]), '_'); if ($this->use_cache === true) { $results = Cache::remember($key, $this->cache_time, function () use ($path, $options) { return $this->_call($path, $options); }); } else { $results = $this->_call($path, $options); } return $results; } public function getSearch(string $query) { $path = 'search'; $options = [ 'query' => $query ]; $key = Str::slug(implode(' ', [$path, str($query)->slug()]), '_'); if ($this->use_cache === true) { $results = Cache::remember($key, $this->cache_time, function () use ($path, $options) { return $this->_call($path, $options); }); } else { $results = $this->_call($path, $options); } return $results; } private function _call(string $path, array $options = []) { $url = empty($options) ? $this->url . $path : $this->url . $path . '?' . Arr::query($options); // $response = Http::get($url); $response = Http::withHeaders(['referer' => route('home')])->get($url); if (!$response) { return false; } $body = $response->getBody(); $results = json_decode($body); if (isset($results->status_code)) { abort(403, $results->status_message); } return json_decode($body); } }