Пример #1
0
        private string GenerateMovieRoles(HtmlDocument htmlDocument, string rolePath, Movie movie, JavlibRoleType roleType)
        {
            var    roleNodes = htmlDocument.DocumentNode.SelectNodes(rolePath);
            string roleStr   = string.Empty;

            if (roleNodes != null)
            {
                foreach (var roleNode in roleNodes)
                {
                    var name = roleNode.InnerHtml.Trim();
                    var url  = roleNode.Attributes["href"].Value;
                    roleStr += name + ",";

                    switch (roleType)
                    {
                    case JavlibRoleType.Actor:
                        Actor actor = _actorService.FindActorByName(name, url) ?? _actorService.SaveActor(new Actor()
                        {
                            Name = name, Url = url
                        });
                        movie.MovieRelations.Add(new MovieRelation()
                        {
                            IdRelation = actor.IdActor, IdTyRole = JavlibRoleType.Actor
                        });
                        break;

                    case JavlibRoleType.Category:
                        Category category = _categoryService.FindCategoryByName(name, url) ?? _categoryService.SaveCategory(new Category()
                        {
                            Name = name, Url = url
                        });
                        movie.MovieRelations.Add(new MovieRelation()
                        {
                            IdRelation = category.IdCategory, IdTyRole = JavlibRoleType.Category
                        });
                        break;

                    case JavlibRoleType.Company:
                        Company company = _companyService.FinCompanyByName(name, url) ?? _companyService.SaveCompany(new Company()
                        {
                            Name = name, Url = url
                        });
                        movie.MovieRelations.Add(new MovieRelation()
                        {
                            IdRelation = company.IdCompany, IdTyRole = JavlibRoleType.Company
                        });
                        break;

                    case JavlibRoleType.Director:
                        Director director = _directorService.FindDirectorByName(name, url) ?? _directorService.SaveDirector(new Director()
                        {
                            Name = name, Url = url
                        });
                        movie.MovieRelations.Add(new MovieRelation()
                        {
                            IdRelation = director.IdDirector, IdTyRole = JavlibRoleType.Director
                        });
                        break;

                    case JavlibRoleType.Publisher:
                        Publisher publisher = _publisherService.FindPublisherByName(name, url) ?? _publisherService.SavePublisher(new Publisher()
                        {
                            Name = name, Url = url
                        });
                        movie.MovieRelations.Add(new MovieRelation()
                        {
                            IdRelation = publisher.IdPublisher, IdTyRole = JavlibRoleType.Publisher
                        });
                        break;
                    }
                }
            }
            return(roleStr.TrimEnd(','));
        }