예제 #1
0
        public void StartSearch(IList <AliEngine> aliEngineArray)
        {
            try
            {
                PauseSearch();
                if (task != null)
                {
                    task.Wait();
                }

                var list = new List <Searcher>(aliEngineArray.Count);
                foreach (var aliEngine in aliEngineArray)
                {
                    var s = Searchers?.FirstOrDefault(a => a.AliEngine == aliEngine);
                    if (s == null)
                    {
                        //from cache
                        s = FromCache(aliEngine);
                        if (s == null)  //init new
                        {
                            s = new Searcher(aliEngine);
                        }
                    }
                    list.Add(s);
                }
                Searchers = list;

                //run async
                task = Task.Run(() =>
                {
                    Continue();
                });
            }
            catch (Exception ex) { Catch.Set(ex); }
        }
예제 #2
0
        public void Search(SearchModel model)
        {
            try
            {
                if (model == null)
                {
                    return;
                }
                //if (Searchers != null && LastSearchModel == model) //!+todo need run search if all paused
                //{
                //    lastSearchModel = model;
                //    return;
                //}
                //var saveModel
                if (Searchers == null || !model.IsEqualByAli(LastSearchModel))
                {
                    IEnumerable <string> arr;
                    var customUrl = !model.Url.IsEmpty();
                    if (!customUrl)
                    {
                        arr = model.AliSearchText.SplitExt(';');
                    }
                    else
                    {
                        arr = model.Url.SplitExt(';');
                    }

                    var list = new List <AliEngine>();
                    foreach (var item in arr)
                    {
                        var aliEngine = new AliEngine
                        {
                            Currency    = model.Currency,
                            MinPrice    = model.AliMinPrice,
                            MaxPrice    = model.AliMaxPrice,
                            ShipCountry = model.ShipCountry,
                            Cookies     = Cookies
                        };

                        var tmp = item.Trim();
                        if (customUrl)
                        {
                            aliEngine.CustomUrl = tmp;
                        }
                        else
                        {
                            aliEngine.SearchText = tmp;
                        }

                        list.Add(aliEngine);
                    }

                    StartSearch(list);
                }

                LastSearchModel = model;
            }
            catch (Exception ex) { Catch.Set(ex); }
        }
예제 #3
0
        public object SearchStatus(SearchModel model)
        {
            try
            {
                var  searchStatusItems = GetStatusSearch(Worker.Searchers);
                var  item      = searchStatusItems?.FirstOrDefault(a => !a.IsCanceled);
                bool cancelled = item == null;

                IEnumerable <Goods> goods = null;
                int totalCount            = 0;
                int totalPages            = 0;

                if (cancelled)
                {
                    if (model.Page == 0)
                    {
                        model.Page = 1;
                    }

                    //sort by second properties
                    goods = Worker.ListGoods
                            .MaxPrice(model.ResMaxPrice)
                            .MinPrice(model.ResMinPrice)
                            .WhereMaxQuantity(model.MaxQuantity)
                            .WhereText(model.ResSearchText)
                            .WhereNoText(model.ResExcludeText) //exclude text
                            .MinOrders(model.MinOrders)
                            .MinRating(model.MinRating)
                            .Distinct()
                            .Sort(model.SortMode)
                            .ToList()
                            .CountOut(out totalCount)
                            .Skip((model.Page - 1) * pageSize)
                            .Take(pageSize)
                            .ToList();

                    totalPages = (totalCount + pageSize - 1) / pageSize;
                }

                return(new
                {
                    Canceled = cancelled,
                    SearchStatus = searchStatusItems,
                    Goods = goods,
                    TotalCount = totalCount,
                    TotalPages = totalPages,
                    ExceptionExist = !Catch.LastException.IsEmpty(),
                    AliSearchHist = model.AliSearchHist
                });
            }
            catch (Exception ex)
            {
                Catch.Set(ex);
                return(new
                {
                    ExceptionExist = !Catch.LastException.IsEmpty()
                });
            }
        }
예제 #4
0
 public void AutoContinue()
 {
     try
     {
         Search(LastSearchModel);
     }
     catch (Exception ex) { Catch.Set(ex); }
 }
예제 #5
0
 protected override void OnStart(string[] args)
 {
     try
     {
         app = new WebApiApplication();
         app.Run();
     }
     catch (Exception ex) { Catch.Set(ex); }
     base.OnStart(args);
 }
예제 #6
0
        public void SetCookies(IEnumerable <CookieChrome> collection)
        {
            try
            {
                var container = InitCookies();
                foreach (var item in collection)
                {
                    container.Add(new Cookie(item.Name, item.Value, item.Path, item.Domain));
                }

                Cookies = container;
            }
            catch (Exception ex) { Catch.Set(ex); }
        }
예제 #7
0
 void Exit()
 {
     try
     {
         if (app == null)
         {
             return;
         }
         Worker.Instance.PauseSearch();
         app.Dispose();
         app = null;
     }
     catch (Exception ex) { Catch.Set(ex); }
 }
예제 #8
0
 public void PauseSearch()
 {
     try
     {
         if (Searchers != null)
         {
             foreach (var s in Searchers)
             {
                 s.Stop();
             }
         }
     }
     catch (Exception ex) { Catch.Set(ex); }
 }
예제 #9
0
        public static void DisposeByExit()
        {
            try
            {
                Console.WriteLine("Cache data");
                Worker.Instance.PauseSearch();
            }
            catch (Exception ex)
            {
                Console.WriteLine("DisposeByExit exception...See log file in " + Config.Pathes.ExceptionsLog);
                Catch.Set(ex);
                Console.WriteLine(Catch.LastException);

                Console.ReadLine();
            }
        }
예제 #10
0
 public bool SetCookies(List <CookieChrome> cookies)
 {
     //var cookies = ActionContext.Request.Headers.GetCookies();
     if (cookies == null || cookies.Count < 1 || cookies[0]?.Name == null)
     {
         return(false);
     }
     try
     {
         Worker.SetCookies(cookies);
         return(true);
     }
     catch (Exception ex)
     {
         Catch.Set(ex);
         return(false);
     }
 }
예제 #11
0
        static void Main(string[] args)
        {
            try
            {
                _handler += new EventHandler(Handler);
                SetConsoleCtrlHandler(_handler, true);
                var p = new Program();
                p.Start();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Global exception...See log file in " + Config.Pathes.ExceptionsLog);
                Catch.Set(ex);
                Console.WriteLine(Catch.LastException);

                Console.ReadLine();
            }
        }
예제 #12
0
 void Continue()
 {
     try
     {
         foreach (var s in Searchers)  //first start for statistics
         {
             if (s.CountItems == null) //!+todo when pause than next s.Start
             {
                 s.Start(isOneCycle: true);
             }
         }
         foreach (var s in Searchers)
         {
             s.Start();
             CacheSearcher(s);
         }
     }
     catch (Exception ex) { Catch.Set(ex); }
 }
예제 #13
0
파일: Config.cs 프로젝트: vudev/AliSearcher
        static bool HasWritePermissionOnDir(string path)
        {
            try
            {
                var wrAllow           = false;
                var wrDeny            = false;
                var accessControlList = Directory.GetAccessControl(path);
                if (accessControlList == null)
                {
                    return(false);
                }
                var accessRules = accessControlList.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier));
                if (accessRules == null)
                {
                    return(false);
                }

                foreach (FileSystemAccessRule rule in accessRules)
                {
                    if ((FileSystemRights.Write & rule.FileSystemRights) != FileSystemRights.Write)
                    {
                        continue;
                    }

                    if (rule.AccessControlType == AccessControlType.Allow)
                    {
                        wrAllow = true;
                    }
                    else if (rule.AccessControlType == AccessControlType.Deny)
                    {
                        wrDeny = true;
                    }
                }

                return(wrAllow && !wrDeny);
            }
            catch (Exception ex) { Catch.Set(ex); }
            return(false);
        }
예제 #14
0
        public Searcher FromCache(AliEngine engine)
        {
            try
            {
                if (Config.CacheTime < 1 || !Directory.Exists(Config.Pathes.Cache))
                {
                    return(null);
                }

                var path = Config.Pathes.CacheFile(engine.SearchText);
                if (!File.Exists(path))
                {
                    return(null);
                }

                var str  = File.ReadAllText(path);
                var item = JsonConvert.DeserializeObject(str, typeof(Searcher)) as Searcher;

                if (item.AliEngine != engine)
                {
                    return(null);
                }

                if (item.DateResponse == null)
                {
                    return(null);
                }

                if ((DateTime.UtcNow - (DateTime)item.DateResponse).TotalDays > Config.CacheTime)
                {
                    return(null);
                }

                return(item);
            }
            catch (Exception ex) { Catch.Set(ex); }
            return(null);
        }
예제 #15
0
        void CacheSearcher(Searcher item)
        {
            try
            {
                if (!Config.Pathes.WriteAllow)
                {
                    return;
                }
                if (Config.CacheTime < 1 || item == null || item.Goods.IsEmpty())
                {
                    return;
                }
                if (!Directory.Exists(Config.Pathes.Cache))
                {
                    Directory.CreateDirectory(Config.Pathes.Cache);
                }

                var json = JsonConvert.SerializeObject(item, Formatting.Indented);
                var path = Config.Pathes.CacheFile(item.SearchText);
                File.WriteAllText(path, json);
            }
            catch (Exception ex) { Catch.Set(ex); }
        }
예제 #16
0
        public static void ClearOldCache()
        {
            try
            {
                var now = DateTime.UtcNow;
                if (!Config.AutoClearOldCache || !Directory.Exists(Config.Pathes.Cache))
                {
                    return;
                }
                foreach (var path in Directory.EnumerateFiles(Config.Pathes.Cache))
                {
                    var str = File.ReadAllText(path);

                    const string dtResponse = "DateResponse";
                    var          startIndex = str.IndexOf(dtResponse);
                    if (startIndex < 0)
                    {
                        break;
                    }
                    startIndex += dtResponse.Length + 2;
                    startIndex  = str.IndexOf('\"', startIndex) + 1;
                    var endIndex = str.IndexOf('\"', startIndex) - 1;
                    var dtStr    = str.Substring(startIndex, endIndex - startIndex);
                    if (DateTime.TryParse(dtStr, out DateTime dt))
                    {
                        if (dt.AddDays(Config.CacheTime) < now)
                        {
                            if (Config.Pathes.WriteAllow)
                            {
                                File.Delete(path);
                            }
                        }
                    }
                }
            }
            catch (Exception ex) { Catch.Set(ex); }
        }
예제 #17
0
파일: Config.cs 프로젝트: vudev/AliSearcher
        public static void Init()
        {
            if (inited)
            {
                return;
            }
            inited = true;
            try
            {
                var pricipal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                AdminRights = pricipal.IsInRole(WindowsBuiltInRole.Administrator);

                var location = Assembly.GetExecutingAssembly().Location;
                appPath  = Path.GetDirectoryName(location) + "\\";
                workPath = appPath;

                if (Debugger.IsAttached)
                {
                    var str = Assembly.GetExecutingAssembly().GetName().Name;
                    appPath = Path.GetFullPath(Path.Combine(appPath, @"..\..\..\", str));
                }


                if (File.Exists(Pathes.Config))
                {
                    var lines = File.ReadAllLines(Pathes.Config);
                    if (lines == null || lines.Length < 1)
                    {
                        return;
                    }

                    var dict = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);

                    foreach (var line in lines)
                    {
                        if (line.IsEmpty())
                        {
                            continue;
                        }
                        var arr = line.Split(new char[] { '=', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (arr.Length < 2 || arr[1].IsEmpty())
                        {
                            continue;
                        }
                        dict.Add(arr[0], arr[1]);
                    }

                    if (dict.TryGetValue("CacheTime", out string value))
                    {
                        if (int.TryParse(value, out int v))
                        {
                            if (v < 0)
                            {
                                v = 0;
                            }
                            CacheTime = v;
                        }
                    }

                    if (dict.TryGetValue("AutoClearOldCache", out value))
                    {
                        AutoClearOldCache = int.TryParse(value, out int v) && v == 1;
                    }

                    if (dict.TryGetValue("LocalPort", out value))
                    {
                        if (int.TryParse(value, out int v))
                        {
                            BasePort = v;
                        }
                    }

                    if (dict.TryGetValue("AppPath", out value))
                    {
                        workPath = value;
                    }

                    if (dict.TryGetValue("AutoContinueSearches", out value))
                    {
                        AutoContinueSearches = int.TryParse(value, out int v) && v == 1;
                    }

                    if (!Directory.Exists(workPath))
                    {
                        Directory.CreateDirectory(workPath);
                    }

                    writeAllow = HasWritePermissionOnDir(workPath);
                    if (!writeAllow)
                    {
                        Catch.Set("Error: not allowed for write in " + workPath);
                    }
                }
            }
            catch (Exception ex) { Catch.Set(ex); }
        }