示例#1
0
        public async Task <RottenTomatoesMainServiceModel> GetMovieDataAsync(string movieTitleId)
        {
            string contentFilePath = GlobalConstants.MainContentRottenTomatoesLogFilePath;
            string mainContentUrl  = string.Format(GlobalConstants.RottenTomatoesMainContentUrl, movieTitleId);

            HtmlNode docNode = await GetDocNode(contentFilePath, mainContentUrl);

            HtmlNode criticsScoreNode = docNode
                                        .SelectSingleNode("//span[contains(@class, 'meter-value')]/span[normalize-space(text())]");

            HtmlNode titleNode = docNode
                                 .SelectSingleNode("//h1[@id='movie-title'][normalize-space(text())]");

            HtmlNode usersScoreNode = docNode
                                      .SelectSingleNode("//div[@class='meter-value']/span[normalize-space(text())]");

            if (criticsScoreNode == null)
            {
                throw new InvalidOperationException("Getting Rotten Tomatoes Critics Score failed!!");
            }

            if (usersScoreNode == null)
            {
                throw new InvalidOperationException("Getting Rotten Tomatoes Users Score failed!!");
            }

            string title = string.Empty;

            if (titleNode != null)
            {
                title = titleNode.InnerText.Trim();
            }

            string criticsScoreValue = criticsScoreNode.InnerText.Trim();
            int    criticsScore      = criticsScoreValue.ParseToInt();

            string usersScoreValue = usersScoreNode.InnerText.Replace("%", "").Trim();
            int    usersScore      = usersScoreValue.ParseToInt();

            var model = new RottenTomatoesMainServiceModel()
            {
                RottenTomatoesCriticsScore = criticsScore,
                RottenTomatoesId           = movieTitleId,
                RottenTomatoesTitle        = title,
                RottenTomatoesUsersScore   = usersScore
            };

            return(model);
        }
        public async Task<IActionResult> Search(MovieSearchTitleFormModel formSearchModel)
        {
            const string none = WebConstants.RadioButtonNoneValue;

            string blurayDotComTitleId = formSearchModel.BlurayDotComSelectedTitle;
            string boxOfficeMojoTitleId = formSearchModel.BoxOfficeMojoSelectedTitle;
            string dvdEmpireTiteId = formSearchModel.DvdEmpireSelectedTitle;
            string imdbTitleId = formSearchModel.ImdbSelectedTitle;
            string rottenTomatoesTitleId = formSearchModel.RottenTomattoesSelectedTitle;

            MovieFormMainModel formMainModel = new MovieFormMainModel();

            #region Blu-ray.com

            if (!string.IsNullOrEmpty(blurayDotComTitleId) && blurayDotComTitleId != none)
            {
                //BlurayDotComMainServiceModel blurayDotComServiceModel
                //    = await this.blurayDotComService.GetMovieDataAsync(blurayDotComTitleId);
            }

            #endregion Blu-ray.com

            #region Box Office Mojo

            if (!string.IsNullOrEmpty(boxOfficeMojoTitleId) && boxOfficeMojoTitleId != none)
            {
                try
                {
                    BoxOfficeMojoMainServiceModel boxOfficeMojoServiceModel
                                = await this.boxOfficeMojoService.GetMovieDataAsync(boxOfficeMojoTitleId);

                    IMapper config = new MapperConfiguration(cfg =>
                    {
                        cfg.CreateMap<BoxOfficeMojoMainServiceModel, MovieFormMainModel>();
                    })
                    .CreateMapper();

                    config.Map(boxOfficeMojoServiceModel, formMainModel);
                }
                catch (Exception ex)
                {
                    await Task.Run(() => ex.Log(nameof(HomeController), nameof(Search)));
                }
            }

            #endregion Box Office Mojo

            #region IMDb

            if (!string.IsNullOrEmpty(imdbTitleId) && imdbTitleId != none)
            {
                try
                {
                    ImdbMainServiceModel imdbServiceModel
                                = await this.imdbService.GetMovieDataAsync(imdbTitleId);

                    this.mapper.Map(imdbServiceModel, formMainModel);

                    IEnumerable<string> colorNames = imdbServiceModel.Colors;
                    formMainModel.SelectedColors
                        = await this.movieDbService.GetColorsIdFromNameAsync(colorNames);

                    IEnumerable<string> countryNames = imdbServiceModel.Countries;
                    formMainModel.SelectedCountries
                        = await this.movieDbService.GetCountriesIdFromNameAsync(countryNames);

                    IEnumerable<string> genreNames = imdbServiceModel.Genres;
                    formMainModel.SelectedGenres
                        = await this.movieDbService.GetGenresIdFromNameAsync(genreNames);

                    IEnumerable<string> languageNames = imdbServiceModel.Languages;
                    formMainModel.SelectedLanguages
                        = await this.movieDbService.GetLanguagesIdFromNameAsync(languageNames);
                }
                catch (Exception ex)
                {
                    await Task.Run(() => ex.Log(nameof(HomeController), nameof(Search)));
                }
            }

            #endregion IMDb

            #region Rotten Tomattoes

            if (!string.IsNullOrEmpty(rottenTomatoesTitleId) && rottenTomatoesTitleId != none)
            {
                try
                {
                    RottenTomatoesMainServiceModel rottenTomatoesServiceModel
                                = await this.rottenTomatoesService.GetMovieDataAsync(rottenTomatoesTitleId);

                    IMapper config = new MapperConfiguration(cfg =>
                    {
                        cfg.CreateMap<RottenTomatoesMainServiceModel, MovieFormMainModel>();
                    })
                   .CreateMapper();

                    config.Map(rottenTomatoesServiceModel, formMainModel);
                }
                catch (Exception ex)
                {
                    await Task.Run(() => ex.Log(nameof(HomeController), nameof(Search)));
                }
            }

            #endregion Rotten Tomattoes

            await this.PopulateFormMainModel(formMainModel);

            return View("Edit", formMainModel);
        }