コード例 #1
0
        private void ProcessFilmInfo(Cineworld.Configuration config, Dictionary <int, FilmInfo> currentFilms, Dictionary <int, List <CinemaInfo> > filmCinemas, RegionDef region, bool searchTMDB = true)
        {
            CineworldService cws    = new CineworldService();
            Films            films  = null;
            Task <Films>     tfilms = cws.GetFilms(region, true);

            tfilms.Wait();

            if (!tfilms.IsCanceled && tfilms.Result != null)
            {
                films = tfilms.Result;
            }

            TMDBService tmdb = new TMDBService();

            HashSet <int> newFilmIds = new HashSet <int>();

            CineMobileService mobileService = new CineMobileService();

            //foreach (var film in films.films)
            for (int i = 0; i < films.films.Count; i++)
            {
                //if (i > 0)
                //    break;

                var film = films.films[i];

                newFilmIds.Add(film.edi);

                //if (film.edi != 128529 && film.edi != 139438)
                //    continue;

                Task <Cinemas> tcinemas = cws.GetCinemas(region, false, int.MinValue, film.edi);

                if (!currentFilms.ContainsKey(film.edi) || !currentFilms[film.edi].TMDBDataLoaded)
                {
                    Movie             movieData    = null;
                    Task <FilmImages> tmovieImages = null;

                    FilmInfo filminfo = new FilmInfo()
                    {
                        EDI = film.edi, Classification = !String.IsNullOrWhiteSpace(film.classification) ? film.classification : "TBC", Title = film.ProcessedTitle, TMDBDataLoaded = false
                    };

                    if (movieData == null)
                    {
                        Results res = null;
                        try
                        {
                            DateTime       ukRelease = film.GetReleaseDate();
                            Task <Results> tres      = tmdb.GetSearchResults(film.CleanTitle, ukRelease);
                            tres.Wait();
                            if (!tres.IsFaulted && tres.Result != null)
                            {
                                res = tres.Result;
                            }

                            if (res.SearchResults == null || res.SearchResults.Count == 0 && ukRelease != DateTime.MinValue)
                            {
                                Task <Results> tres2 = tmdb.GetSearchResults(film.CleanTitle, DateTime.MinValue);
                                tres2.Wait();
                                if (!tres2.IsFaulted && tres2.Result != null)
                                {
                                    res = tres2.Result;
                                }
                            }

                            if (res.SearchResults != null && res.SearchResults.Count > 0)
                            {
                                Result movieSearchRes = res.SearchResults.First();

                                Task <Movie> tmovieData = tmdb.GetMovieDetails(movieSearchRes.Id);
                                tmovieData.Wait();

                                tmovieImages = tmdb.GetFilmImages(movieSearchRes.Id);

                                if (!tmovieData.IsFaulted && tmovieData.Result != null)
                                {
                                    movieData = tmovieData.Result;
                                }

                                if (movieData.Trailers != null && movieData.Trailers.Youtube != null && movieData.Trailers.Youtube.Count > 0 && !String.IsNullOrEmpty(movieData.Trailers.Youtube[0].Source))
                                {
                                    if (movieData.Trailers.Youtube[0].Source.IndexOf('=') == -1)
                                    {
                                        string[] trailerparts = movieData.Trailers.Youtube[0].Source.Split('=');
                                        movieData.YouTubeTrailerID = trailerparts[trailerparts.Length - 1];
                                    }
                                    else
                                    {
                                        movieData.YouTubeTrailerID = movieData.Trailers.Youtube[0].Source;
                                    }
                                }
                                else
                                {
                                    Task <string> tyoutube = (new YouTubeService().GetVideoId(film.CleanTitle));
                                    tyoutube.Wait();

                                    if (!tyoutube.IsCanceled && tyoutube.Result != null)
                                    {
                                        movieData.YouTubeTrailerID = tyoutube.Result;
                                    }
                                }

                                if (ukRelease == DateTime.MinValue)
                                {
                                    Country c = movieData.Releases.Countries.FirstOrDefault(country => String.Compare(country.Iso31661, region == RegionDef.GB ? "GB" : "IE", StringComparison.OrdinalIgnoreCase) == 0);
                                    if (c != null)
                                    {
                                        ukRelease = DateTime.ParseExact(c.ReleaseDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                                    }
                                    else if (!String.IsNullOrEmpty(movieSearchRes.ReleaseDate))
                                    {
                                        ukRelease = DateTime.ParseExact(movieSearchRes.ReleaseDate, "yyyy-MM-dd", CultureInfo.InvariantCulture);
                                    }
                                }

                                movieData.CineworldReleaseDate = ukRelease;
                            }
                        }
                        catch { }
                    }

                    if (movieData != null)
                    {
                        filminfo.TMDBDataLoaded = true;

                        filminfo.TmdbId   = movieData.Id;
                        filminfo.Overview = movieData.Overview;
                        filminfo.Tagline  = movieData.Tagline;
                        filminfo.Runtime  = movieData.Runtime;
                        filminfo.Genres   = new List <string>();
                        if (movieData.Genres != null)
                        {
                            foreach (var g in movieData.Genres)
                            {
                                filminfo.Genres.Add(g.Name);
                            }
                        }

                        if (tmovieImages != null)
                        {
                            tmovieImages.Wait();

                            if (!tmovieImages.IsFaulted && tmovieImages.Result != null)
                            {
                                if (tmovieImages.Result.Backdrops != null)
                                {
                                    foreach (var backdrop in tmovieImages.Result.Backdrops)
                                    {
                                        string url = string.Format("{0}{1}{2}", config.Images.BaseUrl, ConfigurationManager.AppSettings["mediumposterwidth"], backdrop.FilePath);
                                        filminfo.Backdrops.Add(new Uri(url));
                                    }
                                }

                                if (tmovieImages.Result.Posters != null)
                                {
                                    foreach (var poster in tmovieImages.Result.Posters)
                                    {
                                        string url = string.Format("{0}{1}{2}", config.Images.BaseUrl, ConfigurationManager.AppSettings["mediumposterwidth"], poster.FilePath);
                                        filminfo.Posters.Add(new Uri(url));
                                    }
                                }
                            }
                        }

                        if (!String.IsNullOrEmpty(movieData.PosterPath))
                        {
                            string poster = string.Format("{0}{1}{2}", config.Images.BaseUrl, ConfigurationManager.AppSettings["posterwidth"], movieData.PosterPath);

                            string mediumposter = string.Format("{0}{1}{2}", config.Images.BaseUrl, ConfigurationManager.AppSettings["mediumposterwidth"], movieData.PosterPath);

                            if (!MoviePosters.ContainsKey(filminfo.TmdbId))
                            {
                                MoviePosters.Add(filminfo.TmdbId, mediumposter);
                            }

                            filminfo.PosterUrl       = new Uri(poster);
                            filminfo.MediumPosterUrl = new Uri(mediumposter);
                        }
                        else if (!String.IsNullOrEmpty(film.poster_url))
                        {
                            filminfo.PosterUrl = new Uri(film.poster_url);
                        }

                        if (!String.IsNullOrEmpty(movieData.BackdropPath))
                        {
                            string backdrop = string.Format("{0}{1}{2}", config.Images.BaseUrl, ConfigurationManager.AppSettings["backdropwidth"], movieData.BackdropPath);
                            filminfo.BackdropUrl = new Uri(backdrop);
                        }

                        filminfo.YoutubeTrailer = movieData.YouTubeTrailerID;

                        filminfo.Release = movieData.CineworldReleaseDate;

                        if (movieData.Casts != null && movieData.Casts.Cast != null)
                        {
                            foreach (var cast in movieData.Casts.Cast)
                            {
                                filminfo.FilmCast.Add(new CastInfo()
                                {
                                    ID          = cast.Id,
                                    Name        = cast.Name,
                                    Character   = cast.Character,
                                    ProfilePath = (!String.IsNullOrEmpty(cast.ProfilePath) ? new Uri(string.Format("{0}{1}{2}", config.Images.BaseUrl, ConfigurationManager.AppSettings["posterwidth"], cast.ProfilePath)) : null)
                                });
                            }
                        }
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(film.poster_url))
                        {
                            filminfo.PosterUrl = new Uri(film.poster_url);
                        }
                    }

                    Task <List <FilmReview> > tReviews = mobileService.GetFilmReviews(filminfo);

                    tReviews.Wait();

                    if (!tReviews.IsFaulted && tReviews.Result != null)
                    {
                        filminfo.Reviews = tReviews.Result;
                    }

                    currentFilms[filminfo.EDI] = filminfo;
                }

                tcinemas.Wait();

                if (!tcinemas.IsFaulted && tcinemas.Result != null)
                {
                    List <CinemaInfo> fCinemas = new List <CinemaInfo>();
                    foreach (var cinema in tcinemas.Result.cinemas)
                    {
                        fCinemas.Add(new CinemaInfo()
                        {
                            ID = cinema.id, Name = cinema.name
                        });
                    }

                    filmCinemas[film.edi] = fCinemas;
                }
            }

            HashSet <int> currentFilmIds = new HashSet <int>(currentFilms.Keys);

            currentFilmIds.ExceptWith(newFilmIds);

            foreach (var id in currentFilmIds)
            {
                currentFilms.Remove(id);
            }
        }