Esempio n. 1
0
 private void jobToRun_GrabComplete(object sender, GrabCompleteEventArgs e)
 {
     BackgroundWorker saveWorker = new BackgroundWorker();
     saveWorker.DoWork += (s, dwe) =>
     {
         if (_config.LogToDatabase) {
             var db = new SqLiteDal(_config.DatabasePath);
             db.EnsureExists();
                 db.AddJobHistory(new[] { e.Job });
         }
         if (_config.LogToFile) {
             string filePath= CsvGrabber.Core.Utils.DeserializeList(_config.FilePath).FirstOrDefault();
             if (string.IsNullOrWhiteSpace(filePath))
                 filePath = Lime49.Utils.GetApplicationPath();
             string fileName = Path.GetExtension(filePath);
             var logDirectory = string.IsNullOrWhiteSpace(fileName)? filePath: Path.GetDirectoryName(filePath);
             if (!Directory.Exists(logDirectory))
                 Directory.CreateDirectory(logDirectory);
             switch (e.Job.ScheduledGrab.GrabMode) {
                 case Constants.GrabModes.Regex:
                     string logFilePath = System.IO.Path.Combine(logDirectory, string.Format("{0}-{1:yyyy-MM-ddTHHmm}.csv", Utils.SanitizeFileName(e.Job.ScheduledGrab.Name), DateTime.Now));
                     CSVExporter exporter = new CSVExporter(logFilePath, _config.AppendLogFile) { IncludeRawResponse = _config.LogRawResponse, TrimExtraWhitespace = _config.TrimExtraWhitespace };
                     exporter.Save(e.Job.Response);
                     break;
                 case Constants.GrabModes.Scrape:
                     //todo: test-untested
                     string dumpPath = System.IO.Path.Combine(logDirectory, string.Format("{0}-{1:yyyy-MM-ddTHHmm}.csv", Utils.SanitizeFileName(e.Job.ScheduledGrab.Name), DateTime.Now));
                     File.WriteAllBytes(dumpPath, e.Job.Response.BinaryResponse);
                     break;
             }
         }
     };
     saveWorker.RunWorkerCompleted += (Fs, rwe) =>
     {
         if (rwe.Error != null) {
             Logger.Log(string.Format("Failed saving output for job #{0} - {1}: {2}", e.Job.ScheduledGrab.GrabID, e.Job.ScheduledGrab.Name, rwe.Error.Message));
             GrabFailed(sender, e);
         }
         _handles[e.Job.WaitIndex].Set();
         if (e.Job.WaitIndex == _handles.Length - 1)
         {
             Dispatcher.Invoke(new Action(() =>
                                              {
                                                  IsBusy = false;
                                              }));
         }
     };
     saveWorker.RunWorkerAsync();
     if (e.Job.ScheduledGrab.GrabSchedule == Constants.GrabSchedules.OneTime)
         Jobs.Remove(e.Job);
     if (GrabComplete != null)
         GrabComplete(sender, e);
 }
Esempio n. 2
0
        /// <summary>
        /// Loads saved settings when the Window loads.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            try {
                //string serializedSavedUrls = Settings.Default.UrlList;
                //IEnumerable<GrabbableUrl> savedUrls = CsvGrabber.Core.Utils.DeserializeUrls(serializedSavedUrls);
                //ScheduledGrabs = new ObservableCollection<ScheduledGrab>(savedUrls);
                WPFConfiguration.LoadConfiguration();
                _config = WPFConfiguration.GetConfig();
                var db = new SqLiteDal();
                if (!db.EnsureExists()) {
                    DialogBox.ShowAlert(this, string.Format("The SQLite Database {0} could not be created", db.FileName), "Error");
                    Close();
                }
                RefreshScheduledGrabs();
            } catch (Exception ex) {
                Console.WriteLine("Error loading saved URL list: " + ex.Message);
            }

            try {
                string serializedFilenames = Settings.Default.FilePath;
                IEnumerable<string> items = CsvGrabber.Core.Utils.DeserializeList(serializedFilenames);
                if (items.Any()) {
                    foreach (string item in items) {
                        cboFileName.Items.Add(item);
                    }
                    cboFileName.SelectedIndex = 0;
                } else {
                    cboFileName.Text = System.IO.Path.Combine(Lime49.Utils.GetApplicationPath(), string.Format("grabbeddata{0:yyyy-MM-ddTHHmm}.csv", DateTime.Now));
                }
            } catch (Exception ex) {
                Console.WriteLine("Error loading saved expression list: " + ex.Message);
                Settings.Default.FilePath = CsvGrabber.Core.Utils.SerializeList(new string[0]);
            }

            cboDbPath.Text = Settings.Default.LogDbPath;
            /*string lastFilename = Settings.Default.FilePath;
            if (string.IsNullOrWhiteSpace(lastFilename)) {
                Settings.Default.FilePath = System.IO.Path.Combine(Lime49.Utils.GetApplicationPath(), string.Format("grabbeddata{0:yyyy-MM-ddTHHmm}.csv", DateTime.Now));
            }
            txtFilePath.Text = Settings.Default.FilePath;*/
            chkLogDatabase.IsChecked = Settings.Default.LogToDatabase;
            chkLogFile.IsChecked = Settings.Default.LogToFile;
            chkTrimWhitespace.IsChecked = Settings.Default.TrimExtraWhitespace;
            Settings.Default.Save();
        }