Example #1
0
        public static KeywordResultValue FromCsv(string csvLine)
        {
            string sep = ",";

            KeywordResultValue kValues = null;

            string[] values = csvLine.Split(sep.ToCharArray());

            if (Convert.ToDecimal(values[1]) == 0)
            {
                return(kValues);
            }


            kValues = new KeywordResultValue();

            kValues.Keyword = values[0];
            kValues.Volume  = Convert.ToDecimal(values[1]);

            return(kValues);
        }
Example #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);
        }