예제 #1
0
        public async Task <MostPopularItems> GetMostPopularItems(string userId)
        {
            MostPopularItems results = null;

            try
            {
                //USER_PERSONALIZATION -- userid required
                var request = new GetRecommendationsRequest
                {
                    CampaignArn = AwsParameterStoreClient.GetSimsMostPopularArn(), //sims-most-popular
                    UserId      = userId,
                    NumResults  = 10
                };

                var response = await AmazonPersonalizeRuntimeClient.GetRecommendationsAsync(request);

                var recommendedItems = response.ItemList;

                var json = JsonConvert.SerializeObject(recommendedItems);

                Logger.LogInformation("GetMostPopularItems for userid:" + userId + ": " + json);

                results = new MostPopularItems();

                List <string> itemIds = recommendedItems.Select(s => s.ItemId).ToList();

                results.Movies = await _storageService.GetMovieData(itemIds.ToArray());

                return(results);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #2
0
        public async Task <ImdbMovieResponse> FindMovie(string imdbid)
        {
            var imdbMovieid = "tt" + imdbid;

            ImdbMovieResponse imdbObj = null;

            try
            {
                var apiKey = AwsParameterStoreClient.GetImdbApiKey();

                var url = string.Format("http://www.omdbapi.com/?apikey={0}&i={1}", apiKey, imdbMovieid);

                using (var result = await _httpClient.GetAsync(url))
                {
                    string content = await result.Content.ReadAsStringAsync();

                    imdbObj = JsonConvert.DeserializeObject <ImdbMovieResponse>(content);
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return(imdbObj);
        }
예제 #3
0
        public async Task <List <UserRating> > GetAllMoviesRatedByUser(string userId)
        {
            try
            {
                var query = string.Format("select * from S3Object s where s.USER_ID ='{0}' LIMIT 15", userId);

                var response = await _client.SelectObjectContentAsync(new SelectObjectContentRequest
                {
                    Bucket             = AwsParameterStoreClient.GetS3BucketKey(),
                    Key                = "ratings/ratings.csv",
                    ExpressionType     = ExpressionType.SQL,
                    Expression         = query,
                    InputSerialization = new InputSerialization
                    {
                        CSV = new CSVInput
                        {
                            FileHeaderInfo = FileHeaderInfo.Use,
                            FieldDelimiter = ","
                        }
                    },
                    OutputSerialization = new OutputSerialization
                    {
                        JSON = new JSONOutput()
                    }
                });

                var payload = response.Payload;

                var result = "";

                using (payload)
                {
                    foreach (var ev in payload)
                    {
                        if (ev is RecordsEvent records)
                        {
                            using (var reader = new StreamReader(records.Payload, Encoding.UTF8))
                            {
                                result = reader.ReadToEnd();
                            }
                        }
                    }
                }

                var umresult = result.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);

                var userResult = new List <UserRating>();

                foreach (var o in umresult)
                {
                    userResult.Add(JsonConvert.DeserializeObject <UserRating>(o));
                }

                return(userResult);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #4
0
        public async Task <Item> GetItemMovieData(string movieId)
        {
            try
            {
                var query = $"select * from S3Object s where s.item_Id ='{movieId}'";

                var response = await _client.SelectObjectContentAsync(new SelectObjectContentRequest
                {
                    Bucket             = AwsParameterStoreClient.GetS3BucketKey(),
                    Key                = "items/items.csv",
                    ExpressionType     = ExpressionType.SQL,
                    Expression         = query,
                    InputSerialization = new InputSerialization
                    {
                        CSV = new CSVInput
                        {
                            FileHeaderInfo = FileHeaderInfo.Use,
                            FieldDelimiter = ","
                        }
                    },
                    OutputSerialization = new OutputSerialization
                    {
                        JSON = new JSONOutput()
                    }
                });

                var payload = response.Payload;

                var result = "";

                using (payload)
                {
                    foreach (var ev in payload)
                    {
                        if (ev is RecordsEvent records)
                        {
                            using (var reader = new StreamReader(records.Payload, Encoding.UTF8))
                            {
                                result = reader.ReadToEnd();
                            }
                        }
                    }
                }

                var userResult = JsonConvert.DeserializeObject <Item>(result);

                return(userResult);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #5
0
        public void AddEventTracker(string movieItemId, string userId)
        {
            try
            {
                //record events
                var eventRequest = new PutEventsRequest
                {
                    TrackingId =
                        AwsParameterStoreClient.GetClickStreamTrackingId(),
                    UserId    = userId,                   //USER_ID
                    SessionId = Guid.NewGuid().ToString() //SESSION_ID
                };

                var ev1 = new TrackingEvent {
                    itemId = movieItemId
                };

                var ev = JsonConvert.SerializeObject(ev1);

                var e = new Event
                {
                    //e.EventId = "event1";
                    EventType  = "click",     //EVENT_TYPE
                    Properties = ev,
                    SentAt     = DateTime.Now //TIMESTAMP
                };

                var events = new List <Event> {
                    e
                };

                eventRequest.EventList = events;

                var amazonPersonalizeEventsClient = new AmazonPersonalizeEventsClient(RegionEndpoint.USEast1);

                amazonPersonalizeEventsClient.PutEventsAsync(eventRequest);

                Logger.LogInformation("Adding a clickstream event for userid:" + userId + ", movieitemid: " + movieItemId);
            }
            catch (Exception exception)
            {
                throw;
            }
        }
예제 #6
0
        public async Task <PersonalizeViewModel> GetRecommendations(string userId)
        {
            PersonalizeViewModel results = null;

            try
            {
                //USER_PERSONALIZATION -- userid required
                var request = new GetRecommendationsRequest
                {
                    CampaignArn = AwsParameterStoreClient.GetPersonalRecommendationsArn(), //personal-recommendations-metadata
                    UserId      = userId,
                    NumResults  = 10
                };

                var response = await AmazonPersonalizeRuntimeClient.GetRecommendationsAsync(request);

                var recommendedItems = response.ItemList;

                var json = JsonConvert.SerializeObject(recommendedItems);

                Logger.LogInformation("GetRecommendations:" + userId + ": " + json);

                results = new PersonalizeViewModel {
                    User = new User()
                    {
                        User_Id = userId
                    }
                };

                //get movie thumbnail from imdb
                IList <RecommendedItems> recItems = new List <RecommendedItems>();

                List <string> itemIds = recommendedItems.Select(s => s.ItemId).ToList();

                results.RecommendedItems.Movies = await _storageService.GetMovieData(itemIds.ToArray());

                return(results);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #7
0
        public async Task <SimilarItemViewModel> GetSimilarItems(string movieItemId)
        {
            SimilarItemViewModel results = null;

            try
            {
                //RELATED_ITEMS -- itemId required
                var relateditemsrequest = new GetRecommendationsRequest
                {
                    CampaignArn = AwsParameterStoreClient.GetSimsArn(), //sims-arn
                    ItemId      = movieItemId,
                    NumResults  = 10
                };

                var relateditemsrequestresponse = await AmazonPersonalizeRuntimeClient.GetRecommendationsAsync(relateditemsrequest);

                var relateditemsrequestrecommendedItems = relateditemsrequestresponse.ItemList;

                var json = JsonConvert.SerializeObject(relateditemsrequestrecommendedItems);

                Logger.LogInformation("GetSimilarItems for movie:" + movieItemId + ": " + json);

                results = new SimilarItemViewModel
                {
                    //selected movie
                    Movie = await _storageService.GetMovieData(movieItemId)
                };


                List <string> itemIds = relateditemsrequestrecommendedItems.Select(s => s.ItemId).ToList();

                results.SimilarItems.Movies = await _storageService.GetMovieData(itemIds.ToArray());

                return(results);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #8
0
        public async Task <PersonalizedRankingResults> GetPersonalizedResults(string userId, List <Movie> movies)
        {
            PersonalizedRankingResults results = null;

            try
            {
                //USER_PERSONALIZATION -- userid required
                var request = new GetPersonalizedRankingRequest //personal-ranking
                {
                    CampaignArn =
                        AwsParameterStoreClient.GetPersonalRankingArn(),
                    UserId    = userId,
                    InputList = movies.Select(s => s.Id).ToList()
                };

                var response = await AmazonPersonalizeRuntimeClient.GetPersonalizedRankingAsync(request);

                var recommendedItems = response.PersonalizedRanking;

                var json = JsonConvert.SerializeObject(recommendedItems);

                Logger.LogInformation("GetPersonalizedResults:" + userId + ": " + json);

                results = new PersonalizedRankingResults();

                List <string> itemIds = recommendedItems.Select(s => s.ItemId).ToList();

                results.Movies = await _storageService.GetMovieData(itemIds.ToArray());

                return(results);
            }
            catch (Exception ex)
            {
                throw;
            }
        }
예제 #9
0
        public async Task <List <Item> > GetItemMovieData(string[] movieIds)
        {
            try
            {
                string movieIds1 = string.Join(',', movieIds);

                string rmovieIds = string.Join(",", movieIds1.Split(',').Select(x => string.Format("'{0}'", x)).ToList());

                var query = $"select * from S3Object s where s.item_Id IN ({rmovieIds})";

                var response = await _client.SelectObjectContentAsync(new SelectObjectContentRequest
                {
                    Bucket             = AwsParameterStoreClient.GetS3BucketKey(),
                    Key                = "items/items.csv",
                    ExpressionType     = ExpressionType.SQL,
                    Expression         = query,
                    InputSerialization = new InputSerialization
                    {
                        CSV = new CSVInput
                        {
                            FileHeaderInfo = FileHeaderInfo.Use,
                            FieldDelimiter = ","
                        }
                    },
                    OutputSerialization = new OutputSerialization
                    {
                        JSON = new JSONOutput()
                    }
                });

                var payload = response.Payload;

                List <Item> results = new List <Item>();

                using (payload)
                {
                    foreach (var ev in payload)
                    {
                        if (ev is RecordsEvent records)
                        {
                            using (var reader = new StreamReader(records.Payload, Encoding.UTF8))
                            {
                                string s = reader.ReadToEnd();

                                var jsonReader = new JsonTextReader(new StringReader(s))
                                {
                                    SupportMultipleContent = true
                                };

                                var jsonSerializer = new JsonSerializer();

                                while (jsonReader.Read())
                                {
                                    results.Add(jsonSerializer.Deserialize <Item>(jsonReader));
                                }
                            }
                        }
                    }
                }

                return(results);
            }
            catch (Exception ex)
            {
                throw;
            }
        }