internal async void DownloadNewInit(string Date)
        {
            Loger.Instance.LocalInfo("新数据下载初始化");
            var WebClient   = new WebClientEx.WebClientEx();
            var TheFirstRet = new HandlerHtml(await WebClient.DownloadStringTaskAsync(AddOneDay()), null,
                                              DateTime.Now.ToString("yyyy-MM-dd"));
            var FirstDay = TheFirstRet.AnalysisData.Values.ElementAt(0).Day;
            var LastDay  = TheFirstRet.AnalysisData.Values.ElementAt(TheFirstRet.AnalysisData.Count - 1).Day;

            if (FirstDay == LastDay)
            {
                if (FirstDay == Date)
                {
                    Loger.Instance.LocalInfo("数据循环下载");
                    DownloadNewLoop(TheFirstRet.AnalysisData, Date);
                }
                else
                {
                    Loger.Instance.LocalInfo("下载初始化");
                    DownloadNewInit(Date);
                }
            }
            else
            {
                if (LastDay == Date)
                {
                    Loger.Instance.LocalInfo("数据循环下载");
                    DownloadNewLoop(TheFirstRet.NextDayData, Date);
                }
                else
                {
                    Loger.Instance.LocalInfo("下载初始化");
                    DownloadNewInit(Date);
                }
            }
        }
Beispiel #2
0
        private void DownloadWork()
        {
            if (!CancelSign.IsCancellationRequested && !DownloadCollect.IsAddingCompleted)
            {
                try
                {
                    Stopwatch Time = new Stopwatch();
                    WebClientEx.WebClientEx WebClientEX = new WebClientEx.WebClientEx();
                    Loger.Instance.WithTimeStart($"下载网页page={CurrectPageIndex}", Time);
                    DownloadCollect.TryAdd(new Tuple <int, string>(CurrectPageIndex,
                                                                   WebClientEX.DownloadStringTaskAsync(new Uri($"{Setting.Address}?p={CurrectPageIndex}"))
                                                                   .Result));
                    Loger.Instance.WithTimeStop("下载网页完毕", Time);
                }
                catch (InvalidOperationException ex)
                {
                    if (DownloadCollect.IsCompleted)
                    {
                        Loger.Instance.Error($"管道状态已经完成");
                        CancelSign.Cancel();
                    }

                    /*    if (ex.Message.IndexOf("Object", StringComparison.Ordinal) != -1)
                     *  {
                     *      Loger.Instance.Error($"Object错误,开始重试");
                     *      Thread.Sleep(10000);
                     *      DownloadWork();
                     *  }
                     *
                     *  if (WebClient.ErrorInfo == "Timeout")
                     *  {
                     *      Loger.Instance.Error($"访问超时");
                     *  }
                     *  else if ((ex as WebException).Status == WebExceptionStatus.UnknownError)
                     *  {
                     *      Loger.Instance.Error($"推测下载完毕");
                     *      DownloadCollect.CompleteAdding();
                     *      CancelSign.Cancel();
                     *  }
                     *  else
                     *  {
                     *      Loger.Instance.Error($"发生错误,错误信息{ex}");
                     *      var T = new Task(() =>
                     *      {
                     *          var time = new Random().Next(10000, 100000);
                     *          for (var i = time; i > 0; i -= 1000)
                     *          {
                     *              Loger.Instance.WaitTime(i / 1000);
                     *              Thread.Sleep(1000);
                     *          }
                     *      });
                     *      T.Start();
                     *      T.ContinueWith(obj => { DownloadWork(); });
                     *  }*/
                }
            }
            else if (CancelSign.IsCancellationRequested)
            {
                Loger.Instance.Error("下载终止");
                DownloadCollect.CompleteAdding();
            }
        }
        internal async void DownloadOldInit()
        {
            Loger.Instance.LocalInfo("开始获得旧数据");

            /*思路设想
             * 1.先获取第一次数据,然后判断数据库内该时间段是否已经完成//分析获得数据第一个和最后一个的时间
             * 2.未完成的情况下,开始不断获取任务,获取一天的时间//获取阶段使用单线程分析
             * 3.检测每次获得的数据时间,如果出现差别就暂停任务,重置数据后进行新一轮下载
             */
            //首先获得一波页面数据分析
            var WebClient = new WebClientEx.WebClientEx();

            //  CurrectPageIndex = Setting.LastPageIndex;
            Loger.Instance.LocalInfo("开始第一次获取初始化");
            var TheFirstRet = new HandlerHtml(await WebClient.DownloadStringTaskAsync(
                                                  new Uri($"{Setting.Address}?p={CurrectPageIndex}")));
            //于数据库交流,获得数据库时间状态
            //首先判断首尾是否相同 用来判断同一页是否有日期交替

            var FirstDay = TheFirstRet.AnalysisData.Values.ElementAt(0).Day;
            var LastDay  = TheFirstRet.AnalysisData.Values.ElementAt(TheFirstRet.AnalysisData.Count - 1).Day;

            if (FirstDay == LastDay)
            {
                Loger.Instance.WithTimeStart("查询数据库时间差", Time);
                if (PageInDateStatus(FirstDay) == 1)
                {
                    Loger.Instance.WithTimeStop("查询完毕,当前页为第一天", Time);
                    AddOneDay();
                    DownloadOldInit();
                }
                else
                {
                    Loger.Instance.WithTimeStop("查询完毕,当前页非第一天", Time);
                    DownloadOldLoop(TheFirstRet.AnalysisData, LastDay);
                }
            }
            else
            {
                Loger.Instance.WithTimeStart("查询完毕当前页非第一天,再次查询", Time);
                if (PageInDateStatus(FirstDay) == 1)
                {
                    if (PageInDateStatus(LastDay) == 1)
                    {
                        Loger.Instance.WithTimeStop("查询完毕当前页为第一天", Time);
                        AddOneDay();
                        DownloadOldInit();
                    }
                    else
                    {
                        Loger.Instance.WithTimeStop("查询完毕,当前页非第一天", Time);
                        TheFirstRet.NextDayData = new ConcurrentDictionary <int, TorrentInfo>();
                        foreach (var VARIABLE in TheFirstRet.AnalysisData)
                        {
                            if (VARIABLE.Value.Day == LastDay)
                            {
                                TheFirstRet.NextDayData.TryAdd(VARIABLE.Key, VARIABLE.Value);
                            }
                            DownloadOldLoop(TheFirstRet.NextDayData, LastDay);
                        }
                    }
                }
                else
                {
                    Loger.Instance.WithTimeStop("查询完毕", Time);
                    DownloadOldLoop(TheFirstRet.AnalysisData, FirstDay);
                }
            }
        }