Exemple #1
0
        public static bool InsertOrUpdateSubLinkDTO(SubLinkDTO dto)
        {
            bool result = false;

            try
            {
                using (var _db = new CrawlerModel())
                {
                    if (string.IsNullOrEmpty(dto.ID))
                    {
                        CrawlerSubLink subLink = new CrawlerSubLink()
                        {
                            ID               = Guid.NewGuid().ToString(),
                            CrawlerLinkID    = dto.LinkID,
                            Name             = dto.Name,
                            SubLink          = dto.SubLink,
                            IsRun            = dto.IsRun,
                            XPathTitle       = dto.XPathTitle,
                            XPathDate        = dto.XPathDate,
                            XPathImage       = dto.XPathImage,
                            XPathDescription = dto.XPathDescription,
                            XPathContent     = dto.XPathContent,
                            IsDelete         = false,
                            CreateUser       = "******",
                            CreateDate       = DateTime.Now,
                            ModifierUser     = "******",
                            ModifierDate     = DateTime.Now
                        };
                        _db.CrawlerSubLinks.Add(subLink);
                    }
                    else
                    {
                        var subLink = _db.CrawlerSubLinks.Where(o => o.ID == dto.ID).FirstOrDefault();
                        if (subLink != null)
                        {
                            subLink.Name             = dto.Name;
                            subLink.SubLink          = dto.SubLink;
                            subLink.IsRun            = dto.IsRun;
                            subLink.XPathTitle       = dto.XPathTitle;
                            subLink.XPathDate        = dto.XPathDate;
                            subLink.XPathImage       = dto.XPathImage;
                            subLink.XPathDescription = dto.XPathDescription;
                            subLink.XPathContent     = dto.XPathContent;
                            subLink.ModifierDate     = DateTime.Now;
                        }
                    }
                    if (_db.SaveChanges() > 0)
                    {
                        result = true;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            return(result);
        }
Exemple #2
0
        static private bool CreateCrawlerSub(string crawlerID, string url)
        {
            bool ret = false;

            try
            {
                using (var _db = new CrawlerModel())
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(url);
                    var crawlerSubLinkCheck = _db.CrawlerSubLinks.Where(o => o.SubLink == url).FirstOrDefault();
                    if (crawlerSubLinkCheck == null)
                    {
                        /* create crawler Link */
                        crawlerSubLinkCheck = new CrawlerSubLink()
                        {
                            ID               = Guid.NewGuid().ToString(),
                            CrawlerLinkID    = crawlerID,
                            Name             = url,
                            SubLink          = url,
                            IsRun            = true,
                            XPathTitle       = "//title",
                            XPathDate        = "//Date",
                            XPathImage       = "//Image",
                            XPathDescription = "//Description",
                            XPathContent     = "//Content",
                            IsDelete         = false,
                            CreateUser       = "******",
                            CreateDate       = DateTime.Now,
                            ModifierUser     = "******",
                            ModifierDate     = DateTime.Now,
                            ParentID         = null,
                        };
                        _db.CrawlerSubLinks.Add(crawlerSubLinkCheck);
                    }
                    else
                    {
                        crawlerSubLinkCheck.IsRun            = true;
                        crawlerSubLinkCheck.XPathTitle       = "//title";
                        crawlerSubLinkCheck.XPathDate        = "//Date";
                        crawlerSubLinkCheck.XPathImage       = "//Image";
                        crawlerSubLinkCheck.XPathDescription = "//Description";
                        crawlerSubLinkCheck.XPathContent     = "//Content";
                        crawlerSubLinkCheck.IsDelete         = false;
                        crawlerSubLinkCheck.CreateUser       = "******";
                        crawlerSubLinkCheck.CreateDate       = DateTime.Now;
                        crawlerSubLinkCheck.ModifierUser     = "******";
                        crawlerSubLinkCheck.ModifierDate     = DateTime.Now;
                        crawlerSubLinkCheck.ParentID         = null;
                    }
                    _db.SaveChanges();

                    /* loop all item */
                    var listItem = doc.SelectNodes("//item");
                    foreach (XmlNode item in listItem)
                    {
                        var title   = item["title"].InnerText;
                        var ItemUrl = item["link"].InnerText;
                        var image   = getImage(item["description"].InnerText);

                        /* save post news */
                        RobotCrawler.Crawling(ItemUrl, image, crawlerSubLinkCheck.ID, "admin");
                    }


                    ret = true;
                }
            }
            catch (Exception ex)
            { }
            return(ret);
        }