Exemplo n.º 1
0
 public void SaveToExcel(string path)
 {
     try
     {
         using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
         {
             var worksheet = workbook.AddWorksheet("Каталог");
             using (var writer = new CsvWriter(new ExcelSerializer(worksheet)))
             {
                 if (_isHarvest)
                 {
                     ResultItemHarvestMap.initConfiguration(writer.Configuration);
                 }
                 else
                 {
                     ResultItemMap.initConfiguration(writer.Configuration);
                 }
                 var result = new List <ResultItem>(_process.Result.ToArray()).ToList();
                 if (Settings.Default.RemoveDublicate)
                 {
                     result = result.Distinct(new ResultItemComparer()).ToList();
                 }
                 writer.WriteRecords(result);
             }
             workbook.SaveAs(path);
         }
     }
     catch (Exception ex)
     {
         Logger.Error(ex);
         new ExceptionViewer("SaveToExcel", ex).ShowDialog();
     }
 }
Exemplo n.º 2
0
        public void AutoSaveToExcel(string catalogName = "")
        {
            try
            {
                if (string.IsNullOrWhiteSpace(catalogName))
                {
                    catalogName = "Catalog";
                }
                if (string.IsNullOrWhiteSpace(_sessionFileName))
                {
                    _sessionFileName = $"{catalogName.VaildFileName()}-{DateTime.Now:dd-MM-yyyy HH-mm-ss}";
                }
                using (var workbook = new XLWorkbook(XLEventTracking.Disabled))
                {
                    var worksheet = workbook.AddWorksheet("Каталог");
                    using (var writer = new CsvWriter(new ExcelSerializer(worksheet)))
                    {
                        if (_isHarvest)
                        {
                            ResultItemHarvestMap.initConfiguration(writer.Configuration);
                        }
                        else
                        {
                            ResultItemMap.initConfiguration(writer.Configuration);
                        }

                        var result = new List <ResultItem>(_process.Result.ToArray()).ToList();
                        if (Settings.Default.RemoveDublicate)
                        {
                            result = result.Distinct(new ResultItemComparer()).ToList();
                        }
                        writer.WriteRecords(result);
                    }
                    var path = Path.Combine(Environment.CurrentDirectory, $"{_sessionFileName}.xlsx");
                    if (File.Exists(path))
                    {
                        try
                        {
                            File.Delete(path);
                        }
                        catch
                        {
                            _sessionFileName = $"{catalogName.VaildFileName()}-{DateTime.Now:dd-MM-yyyy HH-mm-ss}";
                            path             = Path.Combine(Environment.CurrentDirectory, $"{_sessionFileName}.xlsx");
                        }
                    }
                    workbook.SaveAs(path);
                }
            }
            catch (Exception ex)
            {
                Logger.Error(ex);
            }
        }
Exemplo n.º 3
0
        public void Go()
        {
            if (_current != null && !_current.IsCompleted)
            {
                if (!_current.IsCanceled)
                {
                    _cts.Cancel();
                }
            }
            else
            {
                _current = null;
                _cts     = new CancellationTokenSource();
                _process.ClearData();
                _process.ClearResult();
                ElementText = "Стоп";
                _process.Message($"Обработано ресурсов {_sitesFound}");
                if (_address.IsFile())
                {
                    #region Process Files
                    Url[] uriToParse = null;
                    try
                    {
                        Status = $"Загрузка документа {_address}";
                        using (var workbook = new XLWorkbook(_address, XLEventTracking.Disabled))
                        {
                            using (var reader = new CsvReader(new ExcelParser(workbook)))
                            {
                                ResultItemHarvestMap.initConfiguration(reader.Configuration);
                                uriToParse = reader.GetRecords <ResultItem>().Select(_ => new Url(_.Url)).ToArray();
                            }
                        }
                        Status = $"Документа {_address} загружен. Найдено {uriToParse.Count()} ссылок";
                    }
                    catch (Exception ex)
                    {
                        new ExceptionViewer("Неверный формат файла.", ex).ShowDialog();
                    }
                    if (uriToParse != null && uriToParse.Any())
                    {
                        var i     = 0;
                        var links = 10;
                        _sitesFound = 0;
                        while (i <= uriToParse.Count() - 1)
                        {
                            try
                            {
                                if (_current == null || (_current != null && _current.IsCompleted))
                                {
                                    if (_current != null && _current.IsCanceled)
                                    {
                                        break;
                                    }
                                    var _list = uriToParse.Skip(i).Take(links).ToList();
                                    _current    = ProcessUris(_list, _cts.Token);
                                    _sitesFound = _sitesFound + _list.Count();
                                    Status      = $"Обработано ресурсов {_sitesFound} ";
                                    AutoSaveToExcel();
                                    i = i + links;
                                }
                            }
                            catch (OperationCanceledException)
                            {
                                _process.Message($"Задача отменена, страница {_page}");
                            }
                            catch (Exception ex)
                            {
                                Logger.Error(ex);
                            }
                        }
                    }

                    #endregion Process Files
                }
                else if (_address.IsCatalog())
                {
                    #region Process Catalog
                    while (_page <= _pages)
                    {
                        try
                        {
                            if (_current == null || (_current != null && _current.IsCompleted))
                            {
                                if (_current != null && _current.IsCanceled)
                                {
                                    break;
                                }
                                _process.Message(
                                    $"Загрузка страницы {((_page == 0) ? string.Empty : _page.ToString())}...");
                                var url = Path.Combine(_address, (_page == 0) ? string.Empty :
                                                       string.Format(_address.GetPageTemplate(), _page)).CreateUrl();
                                _process.Message($"Ссылка {url}...");
                                _current = LoadAsync(url, _cts.Token);
                                _page++;
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            _process.Message($"Задача отменена, страница {_page}");
                        }
                        catch (AggregateException ex)
                        {
                            foreach (var e in ex.InnerExceptions)
                            {
                                if (e is CaptchaRequestException)
                                {
                                    var message = "Запрос подтверждения на использование данных в автоматическом режиме";
                                    _process.Message(message);
                                    Status = message;
                                    Logger.Error(ex);
                                    _current.Wait(new TimeSpan(0, 10, 0));
                                }
                                else
                                {
                                    Logger.Error(ex);
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            Logger.Error(ex);
                        }
                    }
                    #endregion Process Catalog
                }
                else
                {
                    ProcessResource();
                }
                Reset();
            }
        }