Ejemplo n.º 1
0
        public bool Search(User user, string url)
        {
            if (!string.IsNullOrEmpty(url))
            {
                var count = Repository.Find(x => x.OriginLink == url).Count();
                if (count > 0)
                {
                    return(false);
                }
                var objNVB = NVHelper.NVBaseObject(url);
                if (objNVB == null)
                {
                    return(false);
                }
                ThreadPool.QueueUserWorkItem(x =>
                {
                    var websiteNovelApp           = AutofacExt.GetFromFac <WebsiteNovelApp>();
                    var websiteApp                = AutofacExt.GetFromFac <WebsiteApp>();
                    var webSearchHelper           = new WebSearchHelper();
                    webSearchHelper.SpecRegStr    = objNVB.GetSpecRegex();
                    webSearchHelper.GetNovelInfo += objNVB.GetNovelInfo;
                    webSearchHelper.Search(url);

                    string guid = Guid.NewGuid().ToString();
                    websiteApp.Repository.Add(new Website {
                        Id = guid, Name = "新的搜索任务", OriginLink = url, CreatorId = user.Id
                    });
                    var websiteId = guid;

                    ThreadPool.QueueUserWorkItem(m =>
                    {
                        CheckWebsiteState(websiteId, websiteApp, ref webSearchHelper);
                    });

                    try
                    {
                        websiteApp.UpdateState(websiteId, 1);
                        while (true)
                        {
                            if (webSearchHelper.IsAllDone)
                            {
                                websiteApp.UpdateState(websiteId, 2);
                                break;
                            }
                            Thread.Sleep(100);

                            if (webSearchHelper.WebSiteBookInfoQueue.Count > 0)
                            {
                                NVNovel nVNovel = (NVNovel)webSearchHelper.WebSiteBookInfoQueue.Dequeue();
                                if (nVNovel != null)
                                {
                                    nVNovel.WebsiteId = websiteId;
                                    var websiteNovel  = Mapper.Map <WebsiteNovel>(nVNovel);
                                    websiteNovelApp.AddWebsitNovel(websiteId, websiteNovel);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        websiteApp.UpdateState(websiteId, -1);
                        //throw ex;
                    }
                }, objNVB);
                return(true);
            }
            return(false);
        }
Ejemplo n.º 2
0
        public bool ReSearch(string websiteId)
        {
            var website = Repository.FindSingle(x => x.Id == websiteId);

            if (website != null)
            {
                var objNVB = NVHelper.NVBaseObject(website.OriginLink);
                if (objNVB == null)
                {
                    return(false);
                }

                ThreadPool.QueueUserWorkItem(x =>
                {
                    var websiteNovelApp           = AutofacExt.GetFromFac <WebsiteNovelApp>();
                    var websiteApp                = AutofacExt.GetFromFac <WebsiteApp>();
                    var webSearchHelper           = new WebSearchHelper();
                    webSearchHelper.SpecRegStr    = objNVB.GetSpecRegex();
                    webSearchHelper.GetNovelInfo += objNVB.GetNovelInfo;
                    webSearchHelper.Search(website.OriginLink);
                    var guid = website.Id;

                    ThreadPool.QueueUserWorkItem(m =>
                    {
                        CheckWebsiteState(websiteId, websiteApp, ref webSearchHelper);
                    });

                    try
                    {
                        websiteApp.UpdateState(guid, 1);
                        while (true)
                        {
                            if (webSearchHelper.IsAllDone)
                            {
                                websiteApp.UpdateState(guid, 2);
                                break;
                            }
                            Thread.Sleep(100);

                            if (webSearchHelper.WebSiteBookInfoQueue.Count > 0)
                            {
                                NVNovel nVNovel = (NVNovel)webSearchHelper.WebSiteBookInfoQueue.Dequeue();
                                if (nVNovel != null)
                                {
                                    nVNovel.WebsiteId = guid;
                                    var websiteNovel  = Mapper.Map <WebsiteNovel>(nVNovel);
                                    websiteNovelApp.AddWebsitNovel(guid, websiteNovel);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        websiteApp.UpdateState(guid, -1);
                        //throw ex;
                    }
                });
                return(true);
            }
            return(false);
        }