コード例 #1
0
ファイル: Form1.cs プロジェクト: chakian/movie-archive
        private void getMovieInfoByIdAndSave(string imdbId, MovieDO movie, TextWriter tw, DateTime start)
        {
            ImdbModel imdbInfo = new ImdbModel( );

            try {
                imdbInfo = ImdbHelper.getMovieInformation(imdbId, tw, start);

                logHelper.logLine(tw, "Imdb information parsed successfully. Starting operation 'Basic Info'");

                #region Basic Info (not updated in db yet)
                if (string.IsNullOrEmpty(movie.ImdbID))
                {
                    movie.ImdbID = imdbId;
                }
                if (string.IsNullOrEmpty(movie.ImdbPoster) && !string.IsNullOrEmpty(imdbInfo.picturePath))
                {
                    string savePath = getSavePath( );
                    string fileName = FileSystemHelper.prepareFileNameForPicture(imdbInfo.picturePath.Substring(imdbInfo.picturePath.LastIndexOf('/')), new MovieNameModel {
                        OriginalName = movie.OriginalName
                    }, savePath);
                    WebRequestHelper.Download(imdbInfo.picturePath, savePath + fileName);
                    movie.ImdbPoster = fileName;
                }

                movie.ImdbRating = imdbInfo.imdbRating;
                if (movie.Year == null || movie.Year == 0)
                {
                    movie.Year = imdbInfo.year;
                }
                #endregion Basic Info

                logHelper.logLine(tw, "Basic Info inserted");

                logHelper.logLine(tw, "Starting operation 'Genres'");

                #region types
                if (MovieTypeBL.GetAllByMovieID(movie.ID).Count() < 1)
                {
                    try {
                        if (imdbInfo.genres != null)
                        {
                            foreach (string item in imdbInfo.genres)
                            {
                                if (allTypes.ContainsKey(item))
                                {
                                    MovieTypeBL.Save(new MovieTypeDO( )
                                    {
                                        MovieID = movie.ID, TypeID = allTypes[item]
                                    }, 1);
                                }
                                else
                                {
                                    int currentID = TypeBL.Save(new MArchive.Domain.Lookup.TypeDO( )
                                    {
                                        Name = item
                                    }, 1).ID;
                                    //MovieTypeBL.Save( movie.ID, item, 1 );
                                    MovieTypeBL.Save(new MovieTypeDO( )
                                    {
                                        MovieID = movie.ID, TypeID = currentID
                                    }, 1);
                                    allTypes.Add(item, currentID);
                                }
                            }
                        }
                        logHelper.logLine(tw, "Genres inserted");
                    } catch (Exception ex) {
                        logHelper.logLine(tw, "Genres could not be inserted.");
                        logHelper.logException(tw, ex);
                        logHelper.logLine(tw);
                    }
                }
                #endregion types

                logHelper.logLine(tw, "Starting operation 'Actors'");

                #region actors
                if (MovieActorBL.GetAllByMovieID(movie.ID).Count( ) < 1)
                {
                    try {
                        if (imdbInfo.cast != null)
                        {
                            foreach (string item in imdbInfo.cast)
                            {
                                if (allActors.ContainsKey(item))
                                {
                                    MovieActorBL.Save(new MovieActorDO( )
                                    {
                                        MovieID = movie.ID, ActorID = allActors[item]
                                    }, 1);
                                }
                                else
                                {
                                    int currentID = ActorBL.Save(new MArchive.Domain.Lookup.ActorDO( )
                                    {
                                        Name = item
                                    }, 1).ID;
                                    MovieActorBL.Save(new MovieActorDO( )
                                    {
                                        MovieID = movie.ID, ActorID = currentID
                                    }, 1);
                                    allActors.Add(item, currentID);
                                }
                            }
                        }
                        logHelper.logLine(tw, "Actors inserted");
                    } catch (Exception ex) {
                        logHelper.logLine(tw, "Actors could not be inserted.");
                        logHelper.logException(tw, ex);
                        logHelper.logLine(tw);
                    }
                }
                #endregion actors

                logHelper.logLine(tw, "Starting operation 'Languages'");

                #region languages
                if (MovieLanguageBL.GetAllByMovieID(movie.ID).Count( ) < 1)
                {
                    try {
                        if (imdbInfo.languages != null)
                        {
                            foreach (string item in imdbInfo.languages)
                            {
                                if (allLanguages.ContainsKey(item))
                                {
                                    MovieLanguageBL.Save(new MovieLanguageDO( )
                                    {
                                        MovieID = movie.ID, LanguageID = allLanguages[item]
                                    }, 1);
                                }
                                else
                                {
                                    int currentID = LanguageBL.Save(new MArchive.Domain.Lookup.LanguageDO( )
                                    {
                                        Name = item
                                    }, 1).ID;
                                    MovieLanguageBL.Save(new MovieLanguageDO( )
                                    {
                                        MovieID = movie.ID, LanguageID = currentID
                                    }, 1);
                                    allLanguages.Add(item, currentID);
                                }
                            }
                        }
                        logHelper.logLine(tw, "Languages inserted");
                    } catch (Exception ex) {
                        logHelper.logLine(tw, "Languages could not be inserted.");
                        logHelper.logException(tw, ex);
                        logHelper.logLine(tw);
                    }
                }
                #endregion languages

                logHelper.logLine(tw, "Starting operation 'Directors'");

                #region directors
                if (MovieDirectorBL.GetAllByMovieID(movie.ID).Count( ) < 1)
                {
                    try {
                        if (imdbInfo.directors != null)
                        {
                            foreach (string item in imdbInfo.directors)
                            {
                                if (allDirectors.ContainsKey(item))
                                {
                                    MovieDirectorBL.Save(new MovieDirectorDO( )
                                    {
                                        MovieID = movie.ID, DirectorID = allDirectors[item]
                                    }, 1);
                                }
                                else
                                {
                                    int currentID = DirectorBL.Save(new MArchive.Domain.Lookup.DirectorDO( )
                                    {
                                        Name = item
                                    }, 1).ID;
                                    MovieDirectorBL.Save(new MovieDirectorDO( )
                                    {
                                        MovieID = movie.ID, DirectorID = currentID
                                    }, 1);
                                    allDirectors.Add(item, currentID);
                                }
                            }
                        }
                        logHelper.logLine(tw, "Directors inserted");
                    } catch (Exception ex) {
                        logHelper.logLine(tw, "Directors could not be inserted.");
                        logHelper.logException(tw, ex);
                        logHelper.logLine(tw);
                    }
                }
                #endregion directors

                logHelper.logLine(tw, "Starting operation 'Writers'");

                #region writers
                if (MovieWriterBL.GetAllByMovieID(movie.ID).Count( ) < 1)
                {
                    try {
                        if (imdbInfo.writers != null)
                        {
                            foreach (string item in imdbInfo.writers)
                            {
                                if (allWriters.ContainsKey(item))
                                {
                                    MovieWriterBL.Save(new MovieWriterDO( )
                                    {
                                        MovieID = movie.ID, WriterID = allWriters[item]
                                    }, 1);
                                }
                                else
                                {
                                    int currentID = WriterBL.Save(new MArchive.Domain.Lookup.WriterDO( )
                                    {
                                        Name = item
                                    }, 1).ID;
                                    MovieWriterBL.Save(new MovieWriterDO( )
                                    {
                                        MovieID = movie.ID, WriterID = currentID
                                    }, 1);
                                    allWriters.Add(item, currentID);
                                }
                            }
                        }
                        logHelper.logLine(tw, "Writers inserted");
                    } catch (Exception ex) {
                        logHelper.logLine(tw, "Writers could not be inserted.");
                        logHelper.logException(tw, ex);
                        logHelper.logLine(tw);
                    }
                }
                #endregion writers

                logHelper.logLine(tw, "Updating 'movie' table");

                movie.ImdbParsed        = true;
                movie.ImdbLastParseDate = movie.UpdateDate = DateTime.Now;

                MovieBL.Save(movie, 1);

                logHelper.logLine(tw, "Table update successful");
            } catch (Exception ex) { logHelper.logException(tw, ex); }
        }