Ejemplo n.º 1
0
        private MediaCover.MediaCover MapImage(string path, MediaCoverTypes type)
        {
            if (path.IsNotNullOrWhiteSpace())
            {
                return(_configService.GetCoverForURL(path, type));
            }

            return(null);
        }
Ejemplo n.º 2
0
        public Movie GetMovieInfo(int TmdbId, Profile profile = null, bool hasPreDBEntry = false)
        {
            var langCode = profile != null?IsoLanguages.Get(profile.Language).TwoLetterCode : "en";

            var request = _movieBuilder.Create()
                          .SetSegment("route", "movie")
                          .SetSegment("id", TmdbId.ToString())
                          .SetSegment("secondaryRoute", "")
                          .AddQueryParam("append_to_response", "alternative_titles,release_dates,videos")
                          .AddQueryParam("language", langCode.ToUpper())
                          // .AddQueryParam("country", "US")
                          .Build();

            request.AllowAutoRedirect = true;
            request.SuppressHttpError = true;

            var response = _httpClient.Get <MovieResourceRoot>(request);

            if (response.StatusCode == HttpStatusCode.NotFound)
            {
                throw new MovieNotFoundException("Movie not found.");
            }
            if (response.StatusCode != HttpStatusCode.OK)
            {
                throw new HttpException(request, response);
            }

            if (response.Headers.ContentType != HttpAccept.JsonCharset.Value)
            {
                throw new HttpException(request, response);
            }

            // The dude abides, so should us, Lets be nice to TMDb
            // var allowed = int.Parse(response.Headers.GetValues("X-RateLimit-Limit").First()); // get allowed
            // var reset = long.Parse(response.Headers.GetValues("X-RateLimit-Reset").First()); // get time when it resets
            var remaining = int.Parse(response.Headers.GetValues("X-RateLimit-Remaining").First());

            if (remaining <= 5)
            {
                _logger.Trace("Waiting 5 seconds to get information for the next 35 movies");
                Thread.Sleep(5000);
            }

            var resource = response.Resource;

            if (resource.status_message != null)
            {
                if (resource.status_code == 34)
                {
                    _logger.Warn("Movie with TmdbId {0} could not be found. This is probably the case when the movie was deleted from TMDB.", TmdbId);
                    return(null);
                }

                _logger.Warn(resource.status_message);
                return(null);
            }

            var movie     = new Movie();
            var altTitles = new List <AlternativeTitle>();

            if (langCode != "en")
            {
                var iso = IsoLanguages.Find(resource.original_language);
                if (iso != null)
                {
                    altTitles.Add(new AlternativeTitle(resource.original_title, SourceType.TMDB, TmdbId, iso.Language));
                }

                //movie.AlternativeTitles.Add(resource.original_title);
            }

            foreach (var alternativeTitle in resource.alternative_titles.titles)
            {
                if (alternativeTitle.iso_3166_1.ToLower() == langCode)
                {
                    altTitles.Add(new AlternativeTitle(alternativeTitle.title, SourceType.TMDB, TmdbId, IsoLanguages.Find(alternativeTitle.iso_3166_1.ToLower())?.Language ?? Language.English));
                }
                else if (alternativeTitle.iso_3166_1.ToLower() == "us")
                {
                    altTitles.Add(new AlternativeTitle(alternativeTitle.title, SourceType.TMDB, TmdbId, Language.English));
                }
            }

            movie.TmdbId     = TmdbId;
            movie.ImdbId     = resource.imdb_id;
            movie.Title      = resource.title;
            movie.TitleSlug  = Parser.Parser.ToUrlSlug(resource.title);
            movie.CleanTitle = Parser.Parser.CleanSeriesTitle(resource.title);
            movie.SortTitle  = Parser.Parser.NormalizeTitle(resource.title);
            movie.Overview   = resource.overview;
            movie.Website    = resource.homepage;

            if (resource.release_date.IsNotNullOrWhiteSpace())
            {
                movie.InCinemas = DateTime.Parse(resource.release_date);

                // get the lowest year in all release date
                var lowestYear = new List <int>();
                foreach (ReleaseDates releaseDates in resource.release_dates.results)
                {
                    foreach (ReleaseDate releaseDate in releaseDates.release_dates)
                    {
                        lowestYear.Add(DateTime.Parse(releaseDate.release_date).Year);
                    }
                }
                movie.Year = lowestYear.Min();
            }

            movie.TitleSlug += "-" + movie.TmdbId.ToString();

            movie.Images.Add(_configService.GetCoverForURL(resource.poster_path, MediaCoverTypes.Poster));//TODO: Update to load image specs from tmdb page!
            movie.Images.Add(_configService.GetCoverForURL(resource.backdrop_path, MediaCoverTypes.Fanart));
            movie.Runtime = resource.runtime;

            //foreach(Title title in resource.alternative_titles.titles)
            //{
            //    movie.AlternativeTitles.Add(title.title);
            //}

            foreach (ReleaseDates releaseDates in resource.release_dates.results)
            {
                foreach (ReleaseDate releaseDate in releaseDates.release_dates)
                {
                    if (releaseDate.type == 5 || releaseDate.type == 4)
                    {
                        if (movie.PhysicalRelease.HasValue)
                        {
                            if (movie.PhysicalRelease.Value.After(DateTime.Parse(releaseDate.release_date)))
                            {
                                movie.PhysicalRelease     = DateTime.Parse(releaseDate.release_date); //Use oldest release date available.
                                movie.PhysicalReleaseNote = releaseDate.note;
                            }
                        }
                        else
                        {
                            movie.PhysicalRelease     = DateTime.Parse(releaseDate.release_date);
                            movie.PhysicalReleaseNote = releaseDate.note;
                        }
                    }
                }
            }

            movie.Ratings       = new Ratings();
            movie.Ratings.Votes = resource.vote_count;
            movie.Ratings.Value = (decimal)resource.vote_average;

            foreach (Genre genre in resource.genres)
            {
                movie.Genres.Add(genre.name);
            }

            //this is the way it should be handled
            //but unfortunately it seems
            //tmdb lacks alot of release date info
            //omdbapi is actually quite good for this info
            //except omdbapi has been having problems recently
            //so i will just leave this in as a comment
            //and use the 3 month logic that we were using before

            /*var now = DateTime.Now;
             * if (now < movie.InCinemas)
             *  movie.Status = MovieStatusType.Announced;
             * if (now >= movie.InCinemas)
             *  movie.Status = MovieStatusType.InCinemas;
             * if (now >= movie.PhysicalRelease)
             *  movie.Status = MovieStatusType.Released;
             */


            var now = DateTime.Now;

            //handle the case when we have both theatrical and physical release dates
            if (movie.InCinemas.HasValue && movie.PhysicalRelease.HasValue)
            {
                if (now < movie.InCinemas)
                {
                    movie.Status = MovieStatusType.Announced;
                }
                else if (now >= movie.InCinemas)
                {
                    movie.Status = MovieStatusType.InCinemas;
                }
                if (now >= movie.PhysicalRelease)
                {
                    movie.Status = MovieStatusType.Released;
                }
            }
            //handle the case when we have theatrical release dates but we dont know the physical release date
            else if (movie.InCinemas.HasValue && (now >= movie.InCinemas))
            {
                movie.Status = MovieStatusType.InCinemas;
            }
            //handle the case where we only have a physical release date
            else if (movie.PhysicalRelease.HasValue && (now >= movie.PhysicalRelease))
            {
                movie.Status = MovieStatusType.Released;
            }
            //otherwise the title has only been announced
            else
            {
                movie.Status = MovieStatusType.Announced;
            }

            //since TMDB lacks alot of information lets assume that stuff is released if its been in cinemas for longer than 3 months.
            if (!movie.PhysicalRelease.HasValue && (movie.Status == MovieStatusType.InCinemas) && (((DateTime.Now).Subtract(movie.InCinemas.Value)).TotalSeconds > 60 * 60 * 24 * 30 * 3))
            {
                movie.Status = MovieStatusType.Released;
            }

            if (!hasPreDBEntry)
            {
                if (_predbService.HasReleases(movie))
                {
                    movie.HasPreDBEntry = true;
                }
                else
                {
                    movie.HasPreDBEntry = false;
                }
            }

            //this matches with the old behavior before the creation of the MovieStatusType.InCinemas

            /*if (resource.status == "Released")
             * {
             *  if (movie.InCinemas.HasValue && (((DateTime.Now).Subtract(movie.InCinemas.Value)).TotalSeconds <= 60 * 60 * 24 * 30 * 3))
             *  {
             *      movie.Status = MovieStatusType.InCinemas;
             *  }
             *  else
             *  {
             *      movie.Status = MovieStatusType.Released;
             *  }
             * }
             * else
             * {
             *  movie.Status = MovieStatusType.Announced;
             * }*/

            if (resource.videos != null)
            {
                foreach (Video video in resource.videos.results)
                {
                    if (video.type == "Trailer" && video.site == "YouTube")
                    {
                        if (video.key != null)
                        {
                            movie.YouTubeTrailerId = video.key;
                            break;
                        }
                    }
                }
            }

            if (resource.production_companies != null)
            {
                if (resource.production_companies.Any())
                {
                    movie.Studio = resource.production_companies[0].name;
                }
            }

            movie.AlternativeTitles.AddRange(altTitles);

            return(movie);
        }