public Movie CollectAndAddMovieToDb(Movie movie2, string moviePage = null, bool collectKeyword = true) { if (string.IsNullOrEmpty(moviePage)) { InvokeOnNotificationFired("Started collecting movie: " + movie2.Title); moviePage = HttpHelper.FetchWebPage(ImdbTitle + movie2.ImdbId); } var parseMovieInfo = ParseMovieInfo(moviePage); if (parseMovieInfo == null) { return(null); } var movie = _entities.Movies.Where(o => o.ImdbId == parseMovieInfo.ImdbId).FirstOrDefault(); if (movie == null) { movie = parseMovieInfo; movie.IsUpdated = false; _entities.AddToMovies(movie); _entities.SaveChanges(); } else { movie.CopyFromMovie(parseMovieInfo); _entities.SaveChanges(); } foreach (var g in parseMovieInfo.Genres) { var genre = GetGenre(g); var movieGenre = _entities.MovieGenres.Where(o => o.Movie.Id == movie.Id && o.Genre.Id == genre.Id).FirstOrDefault(); if (movieGenre != null) { continue; } movieGenre = new MovieGenre { Movie = movie, Genre = genre }; _entities.AddToMovieGenres(movieGenre); _entities.SaveChanges(); } foreach (var g in parseMovieInfo.Languages) { var language = GetLanguage(g); var movieLanguage = _entities.MovieLanguages.Where(o => o.Movie.Id == movie.Id && o.Language.Id == language.Id).FirstOrDefault(); if (movieLanguage != null) { continue; } movieLanguage = new MovieLanguage { Movie = movie, Language = language }; _entities.AddToMovieLanguages(movieLanguage); _entities.SaveChanges(); } foreach (var g in parseMovieInfo.Countries) { var country = GetCountry(g); var movieCountry = _entities.MovieCountries.Where(o => o.Movie.Id == movie.Id && o.Country.Id == country.Id).FirstOrDefault(); if (movieCountry != null) { continue; } movieCountry = new MovieCountry { Movie = movie, Country = country }; _entities.AddToMovieCountries(movieCountry); _entities.SaveChanges(); } foreach (var g in parseMovieInfo.PersonDirectors) { var person = GetPerson(g); var director = _entities.Directors.Where(o => o.Movie.Id == movie.Id && o.Person.Id == person.Id).FirstOrDefault(); if (director != null) { continue; } director = new Director { Movie = movie, Person = person }; _entities.AddToDirectors(director); _entities.SaveChanges(); } foreach (var g in parseMovieInfo.PersonStars) { var person = GetPerson(g); var star = _entities.Stars.Where(o => o.Movie.Id == movie.Id && o.Person.Id == person.Id).FirstOrDefault(); if (star != null) { continue; } star = new Star { Movie = movie, Person = person }; _entities.AddToStars(star); _entities.SaveChanges(); } foreach (var g in parseMovieInfo.PersonWriters) { var person = GetPerson(g); var writer = _entities.Writers.Where(o => o.Movie.Id == movie.Id && o.Person.Id == person.Id).FirstOrDefault(); if (writer != null) { continue; } writer = new Writer { Movie = movie, Person = person }; _entities.AddToWriters(writer); _entities.SaveChanges(); } if (collectKeyword) { var keywordPage = HttpHelper.FetchWebPage(string.Format(ImdbKeywordUrl, parseMovieInfo.ImdbId)); parseMovieInfo.Keywords = ImdbParser.ParseKeywords(keywordPage); foreach (var g in parseMovieInfo.Keywords) { var keyword = GetKeyword(g); var movieKeyword = _entities.MovieKeywords.Where(o => o.Movie.Id == movie.Id && o.Keyword.Id == keyword.Id). FirstOrDefault(); if (movieKeyword != null) { continue; } movieKeyword = new MovieKeyword { Movie = movie, Keyword = keyword }; _entities.AddToMovieKeywords(movieKeyword); _entities.SaveChanges(); } } InvokeOnNotificationFired("Fiished collecting movie: " + movie.Title); movie.IsUpdated = true; _entities.SaveChanges(); return(movie); }