コード例 #1
0
 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!';
 }
コード例 #2
0
 public function recommendedMovies(Pio $pio)
 {
     $recommended_movies = array();
     try {
         $user_id = session('user_id');
         $pio_predictionclient = $pio->predictionClient();
         $recommended_movies_raw = $pio_predictionclient->sendQuery(array('user' => $user_id, 'num' => 9));
         $movie_ids = array_map(function ($item) {
             return $item['item'];
         }, $recommended_movies_raw['itemScores']);
         $es_client = new \Elasticsearch\Client();
         $search_params['index'] = 'movierecommendation_app';
         $search_params['type'] = 'movie';
         $search_params['body']['query']['bool']['must']['terms']['_id'] = $movie_ids;
         $es_response = $es_client->search($search_params);
         $recommended_movies = $es_response['hits']['hits'];
     } catch (Exception $e) {
         echo 'Caught exception: ', $e->getMessage(), "\n";
     }
     session(array('movies_viewed' => 0, 'user_id' => null));
     return view('recommended_movies', array('recommended_movies' => $recommended_movies));
 }