예제 #1
0
        public override async void SearchAsync(string word)
        {
            string[]      words = word.Split(' ', '\t');
            List <Result> list  = new List <Result>();

            try
            {
                using (BufferedStream fs = new BufferedStream(Utils.HistoryStreamForRead))
                {
                    while (true)
                    {
                        Result result = await Utils.ReadResult(fs).ConfigureAwait(false);

                        if (result == null)
                        {
                            break;
                        }

                        result.From = Name;
                        if (Matches(words, result.Name))
                        {
                            list.Add(result);
                        }
                        if (list.Count == 100)
                        {
                            if (OnResults == null)
                            {
                                break;
                            }
                            if (!OnResults.Invoke(this, list))
                            {
                                break;
                            }
                            list.Clear();
                        }
                    }
                }
            }
            catch { }

            try
            {
                if (list.Count != 0)
                {
                    if (OnResults != null)
                    {
                        OnResults.Invoke(this, list);
                    }
                    list.Clear();
                }
            }
            catch { }
        }
예제 #2
0
        /// <summary>
        /// 搜索指定关键词的实现。
        /// </summary>
        /// <param name="word">关键词</param>
        /// <returns></returns>
        public sealed override async void SearchAsync(string word)
        {
            for (int page = 1; page < 20; page++)
            {
                try
                {
                    string url = await GetURLAsync(word, page).ConfigureAwait(false);

                    List <Result> list = new List <Result>();
                    for (int ntry = 0; ntry < 5; ntry++)
                    {
                        using (HttpClient hc = CreateHttpClient())
                        {
                            hc.Timeout = TimeSpan.FromMilliseconds(8000 + ntry * 2000);

                            try
                            {
                                var resp = await hc.GetAsync(url).ConfigureAwait(false);

                                if (resp.StatusCode == System.Net.HttpStatusCode.OK)
                                {
                                    string html = await hc.GetStringAsync(url).ConfigureAwait(false);

                                    var results = await GetResultsAsync(html).ConfigureAwait(false);

                                    if (results != null)
                                    {
                                        foreach (var result in results)
                                        {
                                            if (result != null)
                                            {
                                                result.Name = HandleCfEmail(result.Name);
                                                list.Add(result);
                                            }
                                        }

                                        if (results.FirstOrDefault() != null)
                                        {
                                            break;
                                        }
                                    }
                                }
                                else
                                {
                                    await Task.Delay(250).ConfigureAwait(false);
                                }
                            }
                            catch
                            {
                                await Task.Delay(250).ConfigureAwait(false);
                            }
                        }
                    }

                    if (list.Count == 0)
                    {
                        break;
                    }
                    if (OnResults == null)
                    {
                        break;
                    }
                    if (!OnResults.Invoke(this, list))
                    {
                        break;
                    }
                    list.Clear();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }