Esempio n. 1
0
        public async Task <bool> SaveFullInfo(TmdbWrapper.Movies.Movie selectedMovieInfo)
        {
            if (selectedMovieInfo == null)
            {
                return(false);
            }
            //The movie might already exist in the universe so this would replace it
            //This is a search Result for a specific movie
            selectedMovie = u.GetMovie(selectedMovieSummary.Id);
            if (selectedMovie != null)
            {
                if (MessageBox.Show("Replace " + selectedMovie.Title + " with web data?", "Confirm Replace", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    return(false);
                }
            }

            //Clear or create
            selectedMovie = new Movie(selectedMovieInfo.Title, selectedMovieInfo.ReleaseDate.Value.Year);
            selectedMovie.BackdropPath  = selectedMovieInfo.BackdropPath;
            selectedMovie.TmdbId        = selectedMovieInfo.Id;
            selectedMovie.ImdbId        = selectedMovieInfo.ImdbId;
            selectedMovie.OriginalTitle = selectedMovieInfo.OriginalTitle;
            selectedMovie.Overview      = selectedMovieInfo.Overview;
            selectedMovie.PosterPath    = selectedMovieInfo.PosterPath;
            selectedMovie.ReleaseDate   = selectedMovieInfo.ReleaseDate;
            selectedMovie.Revenue       = selectedMovieInfo.Revenue;
            selectedMovie.Runtime       = selectedMovieInfo.Runtime;
            selectedMovie.HomePage      = selectedMovieInfo.Homepage;
            //TODO - add remaining properties

            //Add Collection Id to movie ONLY if one exists
            if (selectedMovieInfo.BelongsToCollection != null)
            {
                //Add Collection ID to Movie
                //selectedMovie.CollectionId = selectedMovieInfo.BelongsToCollection.Id;
                MovieCollection mc = u.GetMovieCollection(selectedMovieInfo.Id);
                if (mc == null)
                {
                    //Lookup selectedMovieInfo.BelongsToCollection.Id to see if it exists..
                    //Add Collection to Universe
                    MovieCollection m = u.AddMovieCollection(selectedMovieInfo.BelongsToCollection.Name, selectedMovieInfo.BelongsToCollection.Id, selectedMovieInfo.BelongsToCollection.PosterPath, selectedMovieInfo.BelongsToCollection.BackdropPath);

                    //ONly need to do this once per movie in the collection!!
                    //update the parts of the collection below for Universe
                    TmdbWrapper.Collections.Collection coll = await selectedMovieInfo.BelongsToCollection.CollectionAsync();

                    if (coll != null)
                    {
                        m.Overview = coll.Overview; //Update the Movie Collection Overview.
                        foreach (TmdbWrapper.Collections.Part p in coll.Parts)
                        {
                            u.AddMovieToCollection(m.Id, p.Id); // Add the movie ids of the parts to the Movie Collection Part

                            //m.Parts.Add(p.Id); // Add the movie ids of the parts to the Movie Collection Part
                        }
                    }
                    else
                    {
                        Console.WriteLine("Should never be here! Movie Belongs to a collection but Collection is null!");
                    }
                }
            }


            //Add Production Companies - Already have

            foreach (var item in selectedMovieInfo.ProductionCompanies)
            {
                //Try to get the Production Company and see if it has been updated if Null or not updated then Do web lookup

                ProductionCompany pc = u.GetProductionCompany(item.Id);
                if (u.GetProductionCompany(item.Id) == null || pc.Updated == false)
                {
                    TmdbWrapper.Companies.Company c = await item.CompanyAsync(); // await FindProductionCompany(item.Id);

                    pc              = new ProductionCompany(item.Name, item.Id, item.LogoPath, item.OriginCountry);
                    pc.Description  = c.Description;
                    pc.Headquarters = c.Headquarters;
                    pc.Homepage     = c.Homepage;
                    pc.Updated      = true; // Flag so it will not cause a lookup again.
                                            //Add Parent Production Companies
                    if (c.ParentCompany != null)
                    {
                        pc.ParentCompanyId = c.ParentCompany.Id; // This could change based on movie release data.. (before purchase of A by B)
                    }
                    u.AddProductionCompany(pc);
                }
                MovieProductionCompanyMap mpcm = new MovieProductionCompanyMap();
                mpcm.MovieId             = selectedMovieInfo.Id;
                mpcm.ProductionCompanyId = item.Id;
                u.MovieProductionCompanyMap.Add(mpcm);
            }

            //Get the Movie Credits Now.
            TmdbWrapper.Movies.Credits credits = await selectedMovieInfo.CreditsAsync();

            //credits.Cast
            int  personCount = 0;
            bool skip        = false;
            int  castLimit   = 100;

            foreach (TmdbWrapper.Movies.CastPerson cp in credits.Cast)
            {
                //try
                //{
                String cpCharacter = cp.Character;
                if (cpCharacter.Length == 0)     // Seriously??!!??
                {
                    switch (cp.Gender)
                    {
                    case 0:
                        cpCharacter = "Themself";
                        break;

                    case 1:
                        cpCharacter = "Herself";
                        break;

                    case 2:
                        cpCharacter = "Himself";
                        break;
                    }
                }
                ////Add Character To Movie (Could be Alias able character so mapping would be necessary)
                Character c = u.AddCharacter(cpCharacter,
                                             selectedMovie.TmdbId,
                                             cp.Id,
                                             cp.CreditId,
                                             cp.CastId,
                                             cp.Order
                                             );
                //}
                //catch (Exception err)
                //{

                //}
                //Get full person Info
                //THis is a problem because TMDB will return delay if getting too many at once.
                //Also SERIOUSLY consider only getting the first 10% of the cast - but probably have them already from other movies..
                //Need on demand.
                Person p = new Person(cp.Name);
                p.Id          = cp.Id;
                p.ProfilePath = cp.ProfilePath;
                skip          = false;
                //See if we already have the person in the list
                if (u.People.Select(o => o.Id == cp.Id).Contains(true))
                {
                    //Get the person and see if they have been updated with full information
                    Person testPC = u.People.First(o => o.Id == cp.Id);
                    if (testPC.Updated)
                    {
                        skip = true;
                    }
                }
                if (!skip && personCount < 2)
                {
                    Thread.Sleep(100); // Slow it down..
                    TmdbWrapper.Persons.Person tmdbPerson = await cp.PersonAsync();

                    p.HomePage     = tmdbPerson.Homepage;
                    p.Biography    = tmdbPerson.Biography;
                    p.Birthday     = tmdbPerson.Birthday;
                    p.Deathday     = tmdbPerson.Deathday;
                    p.PlaceOfBirth = tmdbPerson.PlaceOfBirth;
                    p.Updated      = true;
                    personCount++;
                }
                bool personAdded = u.AddPerson(p); // Will fail to add existing person and add basic and updated people.
                if (--castLimit == 0)
                {
                    break;
                }
            }

            ////credits.Crew - Adds significant numbers of People and data Entry
            #region Crew import
            int crewLimit = 10;
            foreach (TmdbWrapper.Movies.CrewPerson crewPerson in credits.Crew)
            {
                u.AddCrewMember(crewPerson, selectedMovie);
                if (--crewLimit == 0)
                {
                    break;
                }
            }
            #endregion

            //Want this for movie search results..
            TmdbWrapper.Movies.Trailers t = await selectedMovieInfo.TrailersAsync(); // Want to have.. does nothing now..

            if (t != null && t.Youtube.Count > 0)
            {
                String firstSource = t.Youtube[0].Source;
                //https://www.youtube.com/watch?v=CmRih_VtVAs // Example of how to use the source.
                selectedMovie.TrailerLink = "https://www.youtube.com/watch?v=" + firstSource;
            }


            u.AddMovie(selectedMovie);
            //Verify This is OK...
            PersistenceBase.Save(PrivateData.GetRelativePath(@"\Cache\uinverse3.json"), u);
            return(true);
        }