public function randomMovie(Request $request, Pio $pio) { if (session('user_id')) { $movies_viewed = session('movies_viewed'); $es_client = new \Elasticsearch\Client(); $search_params['index'] = 'movierecommendation_app'; $search_params['type'] = 'movie'; $search_params['id'] = mt_rand(1, 1000); $movie = $es_client->get($search_params); if (!empty($request->input('movie_id'))) { $user_id = session('user_id'); $movie_id = $request->input('movie_id'); $action = $request->input('action'); $pio_eventclient = $pio->eventClient(); $pio_eventclient->recordUserActionOnItem($action, $user_id, $movie_id); $movies_viewed += 1; if ($movies_viewed == 20) { $movie['has_recommended'] = true; } $movie['movies_viewed'] = $movies_viewed; session(array('movies_viewed' => $movies_viewed)); } return $movie; } }
public function importMovies(Pio $pio) { $index = 1; $pio_eventclient = $pio->eventClient(); $http_client = new \GuzzleHttp\Client(); $es_client = new \Elasticsearch\Client(); for ($x = 1; $x <= 100; $x++) { $movies_url = 'https://api.themoviedb.org/3/movie/popular?api_key=' . env('TMDB_KEY') . '&page=' . $x; $movies_response = $http_client->get($movies_url); $movies_body = $movies_response->getBody(); $movies_result = json_decode($movies_body, true); $movies = $movies_result['results']; if (!empty($movies)) { foreach ($movies as $row) { $id = $row['id']; $title = $row['title']; $poster_path = ''; if (!empty($row['poster_path'])) { $poster_path = $row['poster_path']; } $moviedetails_url = 'https://api.themoviedb.org/3/movie/' . $id . '?api_key=' . env('TMDB_KEY'); $moviedetails_response = $http_client->get($moviedetails_url); $movie_details_body = $moviedetails_response->getBody(); $movie = json_decode($movie_details_body, true); $overview = $movie['overview']; $release_date = $movie['release_date']; $genre = ''; if (!empty($movie['genres'][0])) { $genre = $movie['genres'][0]['name']; } $popularity = $movie['popularity']; $movie_data = array('itypes' => 1, 'tmdb_id' => $id, 'title' => $title, 'poster_path' => $poster_path, 'overview' => $overview, 'release_date' => $release_date, 'genre' => $genre, 'popularity' => $popularity); $pio_response = $pio_eventclient->setItem($index, $movie_data); //create elasticsearch index $params = array(); $params['body'] = $movie_data; $params['index'] = 'movierecommendation_app'; $params['type'] = 'movie'; $params['id'] = $index; $es_res = $es_client->index($params); /* optional if you want to see what's happening echo "<pre>"; print_r($pio_response); echo "</pre>"; echo "---"; echo "<pre>"; print_r($es_res); echo "</pre>"; echo "<br><br>"; */ $index++; } } } return 'awesome!'; }