public RegularTin(string url) { string _host = string.Empty; _host = new Uri(url).Host.ToLower(); HtmlAgilityPack.HtmlDocument doc = loadDoc(url); List <QuyTac> List = QuyTacDal.SelectByHost(_host).ToList(); WrapperClean(_host, url, doc); #region Lấy Ten,Mota, Noi Dung foreach (QuyTac item in List) { HtmlNode _c = doc.DocumentNode.SelectSingleNode(@item.Xpath); switch (item.Loai) { case 0: // renew doc if (_c != null) { doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(_c.InnerHtml); } break; case 1: // Title if (_c != null) { Ten = _c.InnerText; } else { _c = doc.DocumentNode.SelectNodes(@"//title | //TITLE")[0]; if (_c != null) { Ten = _c.InnerText; } } break; case 2: if (_c != null) { MoTa = _c.InnerHtml; } else { _c = doc.DocumentNode.SelectNodes(@"//meta[@name='description'] | //meta[@name='DESCRIPTION'] | //meta[@name='Description']")[0]; if (_c != null) { MoTa = _c.InnerText; } } break; case 3: if (item.Xoa) { if (_c != null) { _c.ParentNode.RemoveChild(_c, false); } } else { if (_c != null) { NoiDung = _c.InnerHtml; } } break; default: break; } } #endregion #region Images if (!string.IsNullOrEmpty(NoiDung)) { HtmlAgilityPack.HtmlDocument _doc1 = new HtmlAgilityPack.HtmlDocument(); _doc1.LoadHtml(NoiDung); #region Keywords using (LinkKeyword _linkKeyword = new LinkKeyword(_doc1.DocumentNode.InnerText)) { if (_linkKeyword.ListKeyWord != null) { KeyWords = _linkKeyword.ListKeyWord; } } #endregion string domain = "http://" + _host; string saveLocation = HostingEnvironment.MapPath("~/lib/up/"); //string uploadDir = @"D:\Work\linh\ktt_x1\web\lib\up\rss\"; string uploadDir = @"C:\inetpub\wwwroot\kttvn\web\lib\up\rss\"; if (_doc1.DocumentNode.SelectNodes("//img | //IMG") != null) { List <string> _list = new List <string>(); foreach (HtmlNode _img in _doc1.DocumentNode.SelectNodes("//img | //IMG")) { if (_img.Attributes["src"] != null) { string src = _img.Attributes["src"].Value; if (src.ToLower().IndexOf("http://") != 0) { if (src.IndexOf("/") != 0) { src = "/" + src; } src = domain + src; } try { var gimg = new ImageProcess(new Uri(src), src); if (gimg.Width > 250 && gimg.Heigth > 200) { _list.Add(src); break; } } catch (Exception ex) { } } } if (_list.Count == 0) { return; } Anh = _list[0]; } } #endregion }
public RegularTin(string url, string uploadDir, List <ImageSize> images) { var host = new Uri(url).Host.ToLower(); var doc = loadDoc(url); var list = QuyTacDal.SelectByHost(host).ToList(); WrapperClean(host, url, doc); #region Lấy Ten,Mota, Noi Dung foreach (var item in list) { var c = doc.DocumentNode.SelectSingleNode(@item.Xpath); switch (item.Loai) { case 0: // renew doc if (c != null) { doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(c.InnerHtml); } break; case 1: // Title if (c != null) { Ten = c.InnerText; } else { c = doc.DocumentNode.SelectNodes(@"//title | //TITLE")[0]; if (c != null) { Ten = c.InnerText; } } break; case 2: if (c != null) { MoTa = c.InnerHtml; } else { c = doc.DocumentNode.SelectNodes(@"//meta[@name='description'] | //meta[@name='DESCRIPTION'] | //meta[@name='Description']")[0]; if (c != null) { MoTa = c.InnerText; } } break; case 3: if (item.Xoa) { if (c != null) { c.ParentNode.RemoveChild(c, false); } } else { if (c != null) { NoiDung = c.InnerHtml; } } break; default: break; } } #endregion #region Images if (string.IsNullOrEmpty(NoiDung)) { return; } var doc1 = new HtmlAgilityPack.HtmlDocument(); doc1.LoadHtml(NoiDung); #region Keywords using (var linkKeyword = new LinkKeyword(doc1.DocumentNode.InnerText)) { if (linkKeyword.ListKeyWord != null) { KeyWords = linkKeyword.ListKeyWord; } } #endregion var domain = "http://" + host; var saveLocation = HostingEnvironment.MapPath("~/lib/up/"); List <string> _list = new List <string>(); if (doc1.DocumentNode.SelectNodes("//img | //IMG") != null) { foreach (var img in doc1.DocumentNode.SelectNodes("//img | //IMG")) { if (img.Attributes["src"] != null) { string src = img.Attributes["src"].Value; if (src.ToLower().IndexOf("http://", System.StringComparison.Ordinal) != 0) { if (src.IndexOf("/", System.StringComparison.Ordinal) != 0) { src = "/" + src; } src = domain + src; } try { var gimg = new ImageProcess(new Uri(src), src); if (gimg.Width > 250 && gimg.Heigth > 200) { var imgTen = Guid.NewGuid().ToString().Replace("-", ""); foreach (var imageSize in images) { gimg.Crop(imageSize.Width, imageSize.Height); gimg.Save(Path.Combine(uploadDir, string.Format("{0}{1}.{2}", imgTen, imageSize.DefaultImage ? "" : string.Format("{0}x{1}", imageSize.Width, imageSize.Height), gimg.Ext))); } break; } } catch (Exception ex) { } } } if (_list.Count == 0) { return; } Anh = _list[0]; } #endregion }