public ActionResult EditForm(int?id)
        {
            var obj = new ArticleAutoItem();

            var listProductType = _articleAutoRepository.GetListForTree <object>();
            var listTypeArticle = _articleTypeRepository.GetListForTree <object>();

            if (id.HasValue)
            {
                obj = _articleAutoRepository.GetItemById <ArticleAutoItem>(id.Value);
            }

            return(Json(new
            {
                data = obj,
                listType = listProductType,
                listTypeArticle = listTypeArticle
            }, JsonRequestBehavior.AllowGet));
        }
示例#2
0
        public bool ReadLink(ArticleAutoItem objTypeAuto, string linkRead, int TypeID, string groupID, int page = 1)
        {
            try
            {
                var linkImage = string.Empty;
                var url       = new Uri(objTypeAuto.Link);
                var web       = new HtmlWeb();
                // load link
                var doc = web.Load(linkRead);

                var aTags = doc.DocumentNode.SelectSingleNode(page > 1 ? objTypeAuto.XpathPaging : objTypeAuto.Xpath);

                if (aTags != null)
                {
                    var htmlDoc = new HtmlDocument();
                    htmlDoc.LoadHtml(aTags.InnerHtml);
                    // get tags a of xpath
                    var listA = htmlDoc.DocumentNode.SelectNodes(".//a");

                    if (listA != null)
                    {
                        // check link content
                        foreach (var item in listA.Where(t => t.Attributes["href"] != null))
                        {
                            try
                            {
                                var link = item.Attributes["href"].Value;
                                if (!item.Attributes["href"].Value.Contains(url.Host))
                                {
                                    link = url.Scheme + "://" + url.Host + link;
                                }
                                var docDetail     = web.Load(link);
                                var htmlDocDetail = new HtmlDocument();
                                htmlDocDetail.LoadHtml(item.InnerHtml);

                                // get image default news if image exits
                                var img = htmlDocDetail.DocumentNode.SelectNodes(".//img");
                                if (img != null)
                                {
                                    // image src
                                    var imgDetail = img.FirstOrDefault().Attributes["src"].Value;
                                    if (!imgDetail.Contains(url.Scheme + "//") && !imgDetail.Contains("https://") && !imgDetail.Contains("http://"))
                                    {
                                        imgDetail = url.Scheme + "://" + url.Host + imgDetail;
                                    }
                                    linkImage = imgDetail;
                                }
                                var linkRefer = link;
                                if (docDetail.DocumentNode.SelectNodes(objTypeAuto.XpathTitle) == null)
                                {
                                    continue;
                                }

                                var title      = docDetail.DocumentNode.SelectNodes(objTypeAuto.XpathTitle)[0].InnerHtml.Trim();
                                var titleAscii = Web365Utility.Web365Utility.ConvertToAscii(title);
                                var objExits   = articleRepository.GetItemByTitleAscii(titleAscii);
                                if (objExits != null)
                                {
                                    if (string.IsNullOrEmpty(objExits.LinkReferenPicture) && !string.IsNullOrEmpty(linkImage))
                                    {
                                        objExits.LinkReferenPicture = linkImage;
                                        articleRepository.Update(objExits);
                                    }
                                    continue;
                                }
                                string summary;
                                try
                                {
                                    if (string.IsNullOrEmpty(objTypeAuto.XpathSummary))
                                    {
                                        summary = string.Empty;
                                    }
                                    else
                                    {
                                        summary = Web365Utility.Web365Utility.RemoveStyleInHtmlTag(docDetail.DocumentNode.SelectNodes(objTypeAuto.XpathSummary)[0].InnerHtml);
                                    }
                                }
                                catch (Exception e)
                                {
                                    summary = string.Empty;
                                    WriteLogs("Error read Sumary " + item.Attributes["href"].Value + "AutoID : " + objTypeAuto.ID);
                                }
                                string detail;
                                try
                                {
                                    if (string.IsNullOrEmpty(objTypeAuto.XpathDetail))
                                    {
                                        detail = string.Empty;
                                    }
                                    else
                                    {
                                        detail = Web365Utility.Web365Utility.RemoveStyleInHtmlTag(docDetail.DocumentNode.SelectNodes(objTypeAuto.XpathDetail)[0].InnerHtml);
                                        if (string.IsNullOrEmpty(linkImage))
                                        {
                                            try
                                            {
                                                htmlDocDetail.LoadHtml(detail);
                                            }
                                            catch (Exception ex)
                                            {
                                                WriteLogs("Error read Detail" + item.Attributes["href"].Value + "AutoID : " + objTypeAuto.ID);
                                            }

                                            // get image default news if image exits
                                            var imgDetailContent = htmlDocDetail.DocumentNode.SelectNodes(".//img");
                                            if (imgDetailContent != null)
                                            {
                                                try
                                                {
                                                    // image src details
                                                    var imgDetailSrc = imgDetailContent.FirstOrDefault().Attributes["src"].Value;
                                                    if (!imgDetailSrc.Contains(url.Scheme + "//") && !imgDetailSrc.Contains("https://") && !imgDetailSrc.Contains("http://"))
                                                    {
                                                        imgDetailSrc = url.Scheme + "://" + url.Host + imgDetailSrc;
                                                    }
                                                    linkImage = imgDetailSrc;
                                                }
                                                catch (Exception ex)
                                                {
                                                    WriteLogs("Error read images Detail" + item.Attributes["href"].Value + "AutoID : " + objTypeAuto.ID);
                                                }
                                            }
                                        }
                                    }
                                }
                                catch (Exception ex)
                                {
                                    WriteLogs("Error " + item.Attributes["href"].Value + "AutoID : " + objTypeAuto.ID);
                                    detail = string.Empty;
                                }
                                var objNews = new tblArticle
                                {
                                    DateCreated        = DateTime.Now,
                                    DateUpdated        = DateTime.Now,
                                    SEOTitle           = title,
                                    SEOKeyword         = title,
                                    SEODescription     = title,
                                    Number             = 0,
                                    TypeID             = TypeID,
                                    IsDeleted          = false,
                                    IsShow             = true,
                                    Title              = title,
                                    TitleAscii         = titleAscii,
                                    Summary            = summary,
                                    Detail             = detail,
                                    LinkReferenPicture = linkImage,
                                    LinkReference      = linkRefer
                                };

                                autoArticleRepository.Add(objNews);
                                articleGroupMapRepository.ResetGroupOfNews(objNews.ID, Web365Utility.Web365Utility.StringToArrayInt(groupID));
                            }
                            catch (Exception ex)
                            {
                                WriteLogs("Error " + item.Attributes["href"].Value + "AutoID : " + objTypeAuto.ID);
                                Elmah.ErrorLog.GetDefault(System.Web.HttpContext.Current).Log(new Elmah.Error(ex));
                            }
                        }
                    }
                }
                else
                {
                    WriteLogs("Can't read tags <a> with link " + linkRead + "AutoID : " + objTypeAuto.ID);
                    return(false);
                }
            }
            catch (Exception)
            {
                WriteLogs("Error " + linkRead + "AutoID : " + objTypeAuto.ID);
                return(false);
            }

            return(true);
        }