예제 #1
0
        private void launchChrome_Click(object sender, RoutedEventArgs e)
        {
            launchChrome.IsEnabled = false;

            Thread chromeThread = new Thread(() =>
            {
                string pathToChrome = "";
                pathToChrome        = GetPathToChrome();

                if (pathToChrome == "")
                {
                    System.Windows.MessageBox.Show("Unable to locate Chrome!", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }


                Process proc            = new Process();
                proc.StartInfo.FileName = pathToChrome;

                proc.StartInfo.Arguments = "https://www.brightedge.com/secure/login/ --new-window --remote-debugging-port=9222 --user-data-dir=C:\\Temp";
                proc.Start();

                var chromeDriverService = ChromeDriverService.CreateDefaultService();
                chromeDriverService.HideCommandPromptWindow = true;
                //return new ChromeDriver(chromeDriverService, new ChromeOptions());

                ChromeOptions options   = new ChromeOptions();
                options.DebuggerAddress = "127.0.0.1:9222";


                options.AddArgument("--start-maximized");
                options.AddArguments("--disable-gpu");
                options.AddArguments("--disable-extensions");

                Driver = new ChromeDriver(chromeDriverService, options);
                //Driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
                PuppetMaster.Driver = Driver;
                PuppetMaster.Window = this;

                PuppetMaster.LoginUser(user);

                this.Dispatcher.Invoke(() =>
                {
                    start.IsEnabled = true;
                });

                while (proc.HasExited == false)
                {
                    if ((DateTime.Now.Second % 5) == 0)
                    { // Show a tick every five seconds.
                      //Console.Write(".");
                    }
                    System.Threading.Thread.Sleep(1000);
                }

                // After process exits
                try
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        launchChrome.IsEnabled = true;
                        start.IsEnabled        = false;
                    });
                }
                catch (Exception ex)
                { }
            });

            chromeThread.Start();
        }
예제 #2
0
        private void StartPuppetProcess(string selectedPath)
        {
            this.Dispatcher.Invoke(() =>
            {
                start.IsEnabled       = false;
                StopProcess           = false;
                stopProcess.IsEnabled = true;

                status.Text            = "";
                SpinnerText.Visibility = Visibility.Visible;
            });

            DirectoryInfo dirInfo = new DirectoryInfo(selectedPath);

            FileInfo[] files = null;
            files = dirInfo.GetFiles();
            int batchesProcessed = 0;



            foreach (FileInfo f in files)
            {
                string fileToProcess = f.FullName;

                if (f.Name.StartsWith("~$"))
                {
                    continue;
                }

                UpdateStatus($"{DateTime.Now} | Processing file {f.Name}");
                SpreadsheetHelper.MatchedSheets.Clear();
                byte[] byteArray;
                try
                {
                    byteArray = File.ReadAllBytes(fileToProcess);
                }
                catch (Exception e)
                {
                    UpdateStatus($"{DateTime.Now} | Error reading file: {f.Name}");
                    continue;
                }

                using (MemoryStream stream = new MemoryStream())
                {
                    stream.Write(byteArray, 0, (int)byteArray.Length);

                    // Open the document for editing
                    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(stream, true))
                    {
                        WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
                        SpreadsheetHelper.workbookPart = workbookPart;

                        var mainSheetPart = SpreadsheetHelper.GetWorksheetPart(workbookPart, "REPLACE");

                        if (mainSheetPart != null)
                        {
                            var mainSheetData = SpreadsheetHelper.GetMainSheetData(mainSheetPart);
                            List <KeywordResultValue> keywordStats = new List <KeywordResultValue>();

                            // Process all Pages
                            foreach (var pageItem in mainSheetData.Pages)
                            {
                                // Only for testing and should be commented out **********************
                                //if (pageItem != "Dining")
                                //    continue;

                                UpdateStatus($"{DateTime.Now} | Processing {pageItem} keywords in file: {f.Name}");

                                //Console.WriteLine(item);
                                var keywordListData = SpreadsheetHelper.GetKeywordsFromSheet(pageItem);
                                var keywordList     = keywordListData.Item1;
                                var keywordCount    = keywordListData.Item2;

                                UpdateStatus($"{DateTime.Now} | {pageItem}: keyword count: {keywordCount}");

                                PuppetMaster.RetryUntilSuccessOrTimeout(() => {
                                    List <KeywordResultValue> keywordPageStats = new List <KeywordResultValue>();

                                    // Process a 1000 keywords at a time from each page
                                    foreach (var keywordListItem in keywordList)
                                    {
                                        if (StopProcess)
                                        {
                                            return(true);
                                        }

                                        DateTime processStartTime = DateTime.Now;

                                        var result = PuppetMaster.RunProcess(keywordListItem, mainSheetData.Country);

                                        if (result == false)
                                        {
                                            return(false);
                                        }


                                        // Process downloaded file
                                        IEnumerable <string> downloadedFiles = new List <string>();

                                        var diffInSeconds = (processStartTime - DateTime.Now).TotalSeconds;

                                        while (diffInSeconds <= 60 && downloadedFiles.Count() == 0)
                                        {
                                            downloadedFiles = Directory.GetFiles(DownloadsFolder)
                                                              .Where(x => new FileInfo(x).CreationTime > processStartTime && x.EndsWith(".csv"));

                                            Thread.Sleep(3000);
                                        }

                                        if (downloadedFiles.Count() > 0)
                                        {
                                            try
                                            {
                                                // Process the file
                                                var downloadedFile = downloadedFiles.ElementAt(0);
                                                Console.WriteLine(downloadedFile);
                                                FilesToDelete.Add(downloadedFile);

                                                List <KeywordResultValue> keywordStats1000 = File.ReadAllLines(downloadedFile)
                                                                                             .Skip(1).Select(v => KeywordResultValue.FromCsv(v))
                                                                                             .Where(v => v != null).ToList();

                                                keywordPageStats.AddRange(keywordStats1000);
                                            }
                                            catch (Exception ex)
                                            {
                                                return(false);
                                            }
                                        }
                                        else
                                        {
                                            return(false);
                                        }
                                    }

                                    //if (keywordPageStats.Count() == 0)
                                    //    return false;

                                    keywordPageStats = keywordPageStats.OrderByDescending(k => k.Volume).ToList();

                                    UpdateStatus($"{DateTime.Now} | {pageItem}: volume stats found in csv: {keywordPageStats.Count}");

                                    keywordStats.AddRange(keywordPageStats);

                                    DeleteDownloadedFiles();

                                    return(true);
                                }, TimeSpan.FromMinutes(8));

                                //break;
                            }

                            PuppetMaster.RemoveLocation(mainSheetData.Country);


                            SpreadsheetHelper.CreateResultSheet(keywordStats);
                            batchesProcessed++;
                        }
                    }

                    SaveAs(fileToProcess, stream);
                }

                if (StopProcess)
                {
                    break;
                }
            }

            PuppetMaster.DeleteAllQueries((int)Math.Ceiling((decimal)batchesProcessed / 10));


            this.Dispatcher.Invoke(() =>
            {
                start.IsEnabled        = true;
                SpinnerText.Visibility = Visibility.Collapsed;
                stopProcess.IsEnabled  = false;
            });
            UpdateStatus($"{DateTime.Now} | Process complete!");
            System.Windows.MessageBox.Show("Process complete!", "Complete", MessageBoxButton.OK, MessageBoxImage.Information);
        }