Exemple #1
0
        public MovieItem GetMovieSingle(int?userId, int movieId)
        {
            using (var db = new DbTorronto())
            {
                var item = db.Movie
                           .NoCopyrighted()
                           .SelectMany(m => db.MovieUser.Where(x => x.MovieID == m.ID && x.UserID == userId).DefaultIfEmpty(),
                                       (m, movieUser) => new { movie = m, muUser = movieUser })
                           .FirstOrDefault(x => x.movie.ID == movieId);

                if (item != null)
                {
                    var persons = db.MoviePerson
                                  .Where(mp => mp.MovieID == movieId)
                                  .OrderBy(mp => mp.Position)
                                  .Select(mp => mp.Person);

                    var genres = db.MovieGenre
                                 .Where(mg => mg.MovieID == movieId)
                                 .OrderBy(mg => mg.Position)
                                 .Select(mg => mg.Genre);

                    Movie.AddRelated(item.movie, persons.Take(6));
                    Movie.AddRelated(item.movie, genres);

                    var movieItem = new MovieItem
                    {
                        Self = item.movie
                    };

                    if (item.muUser != null)
                    {
                        movieItem.InWaitList = item.muUser.IsWaitlist;
                        movieItem.IsWatched  = item.muUser.IsWatched;
                        movieItem.Mark       = item.muUser.Mark;
                        movieItem.IsDontWant = item.muUser.IsDontWant;
                    }

                    return(movieItem);
                }
            }

            return(null);
        }
        public void ProcessDetailsSingle(int movieId)
        {
            _logger.Info("Detailing movie #{0}", movieId);

            Movie movie;

            using (var db = new DbTorronto())
            {
                movie = db.Movie.FirstOrDefault(m => m.ID == movieId);
            }

            if (movie == null)
            {
                _logger.Warn("No such movie #{0}", movieId);
                return;
            }

            if (movie.IsDetailed)
            {
                _logger.Trace("Already detailed movie #{0}", movieId);
                return;
            }

            using (var client = GetClient())
            {
                client.DefaultRequestHeaders.Referrer = new Uri("http://www.kinopoisk.ru/film/" + movie.KinopoiskID);

                var response = client.GetAsync("film/" + movie.KinopoiskID).Result;

                if (response.IsSuccessStatusCode)
                {
                    var content = response.Content.ReadAsStringAsync().Result;

                    File.WriteAllText(Path.Combine("html", "kinopoisk", movieId + ".html"), content);

                    if (content.Contains("www.google.com/recaptcha/api/challenge"))
                    {
                        throw new Exception("Captcha found");
                    }

                    var dom   = CQ.Create(content, Encoding.UTF8);
                    var title = dom
                                .Find(@"#headerFilm h1[itemprop=name]")
                                .Select(x => x.InnerText)
                                .FirstOrDefault();
                    var originalTitle = dom
                                        .Find(@"#headerFilm span[itemprop=alternativeHeadline]")
                                        .Select(x => x.InnerText)
                                        .FirstOrDefault();
                    var release = dom
                                  .Find(@"#div_world_prem_td2 .prem_ical")
                                  .Select(x => x.GetAttribute("data-date-premier-start-link"))
                                  .FirstOrDefault();
                    var description = dom
                                      .Find(@".brand_words[itemprop=description]")
                                      .Select(x => x.InnerHTML)
                                      .FirstOrDefault();
                    var actors = dom
                                 .Find(@"#actorList a")
                                 .Select(x =>
                    {
                        var attribute = x.GetAttribute("href") ?? string.Empty;
                        var rr        = _actorSiteIDRegex.Match(attribute);
                        return(new Person
                        {
                            Name = x.InnerText,
                            SiteID = rr.Success ? Convert.ToInt32(rr.Groups[1].Value) : 0
                        });
                    })
                                 .TakeWhile(x => x.Name != "...")
                                 .Where(x => x.SiteID != 0);
                    var genres = dom
                                 .Find(@"span[itemprop=genre] a")
                                 .Select(x =>
                    {
                        var attribute = x.GetAttribute("href") ?? string.Empty;
                        var rr        = _genreSiteIDRegex.Match(attribute);
                        return(new Genre
                        {
                            Name = x.InnerText,
                            SiteID = rr.Success ? Convert.ToInt32(rr.Groups[1].Value) : 0
                        });
                    })
                                 .Where(x => x.SiteID != 0);
                    var duration =
                        dom
                        .Find(@"#runtime")
                        .Select(x =>
                    {
                        var r = _durationMinutesRegex.Match(x.InnerHTML ?? string.Empty);

                        return(r.Success ? Convert.ToInt32(r.Groups[1].Value) : 0);
                    })
                        .FirstOrDefault();

                    DateTime?releaseDate = null;

                    if (release != null)
                    {
                        var year  = Convert.ToInt32(release.Substring(0, 4));
                        var month = Convert.ToInt32(release.Substring(4, 2));
                        var day   = Convert.ToInt32(release.Substring(6, 2));

                        if (year > 0 && month > 0 && day > 0)
                        {
                            releaseDate = new DateTime(year, month, day);
                        }
                    }

                    int?imdbId = null;

                    if (movie.ImdbID == null)
                    {
                        imdbId = TryGetImdbId(originalTitle, releaseDate);

                        if (imdbId != null)
                        {
                            _logger.Info("movie #{0}, imdb = {1}", movie.ID, imdbId);
                        }
                    }

                    movie.IsDetailed      = true;
                    movie.Title           = title;
                    movie.OriginalTitle   = originalTitle;
                    movie.ReleaseDate     = releaseDate;
                    movie.Description     = description;
                    movie.ImdbID          = imdbId;
                    movie.DurationMinutes = duration;

                    Movie.AddRelated(movie, actors);
                    Movie.AddRelated(movie, genres);

                    _movieService.SaveDetails(movie);

                    QueueService.AddMovieForReindex(movieId);
                    QueueService.AddMovieForRecommendation(movieId);
                }
            }
        }