From b2202dfa23065968b9a00e22aded91876a718c2f Mon Sep 17 00:00:00 2001 From: Akhil Kodali <50161+akhil@users.noreply.github.com> Date: Sat, 10 Aug 2019 16:18:55 -0400 Subject: [PATCH] added negative list --- EmailAddressExtracter.csproj | 2 +- Form1.Designer.cs | 44 +++++++++++++- Form1.cs | 112 +++++++++++++++++++++++++---------- 3 files changed, 123 insertions(+), 35 deletions(-) diff --git a/EmailAddressExtracter.csproj b/EmailAddressExtracter.csproj index 994436e..b311478 100644 --- a/EmailAddressExtracter.csproj +++ b/EmailAddressExtracter.csproj @@ -11,6 +11,7 @@ v4.0 512 true + false publish\ true Disk @@ -23,7 +24,6 @@ true 0 1.0.0.%2a - false false true diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 6125100..9084389 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -34,6 +34,9 @@ private void InitializeComponent() this.emailListView = new System.Windows.Forms.ListView(); this.downloadEmailAddressButton = new System.Windows.Forms.Button(); this.emailCountLabel = new System.Windows.Forms.Label(); + this.badEmailsButton = new System.Windows.Forms.Button(); + this.badEmailsSelection = new System.Windows.Forms.TextBox(); + this.badEmailListView = new System.Windows.Forms.ListView(); this.SuspendLayout(); // // chooseFileButton @@ -51,7 +54,7 @@ private void InitializeComponent() this.fileSelectionBox.Location = new System.Drawing.Point(294, 12); this.fileSelectionBox.Multiline = true; this.fileSelectionBox.Name = "fileSelectionBox"; - this.fileSelectionBox.Size = new System.Drawing.Size(389, 103); + this.fileSelectionBox.Size = new System.Drawing.Size(270, 103); this.fileSelectionBox.TabIndex = 1; // // extractEmailButton @@ -70,14 +73,14 @@ private void InitializeComponent() this.emailListView.HideSelection = false; this.emailListView.Location = new System.Drawing.Point(98, 256); this.emailListView.Name = "emailListView"; - this.emailListView.Size = new System.Drawing.Size(585, 335); + this.emailListView.Size = new System.Drawing.Size(466, 335); this.emailListView.TabIndex = 3; this.emailListView.UseCompatibleStateImageBehavior = false; // // downloadEmailAddressButton // this.downloadEmailAddressButton.Enabled = false; - this.downloadEmailAddressButton.Location = new System.Drawing.Point(431, 169); + this.downloadEmailAddressButton.Location = new System.Drawing.Point(312, 161); this.downloadEmailAddressButton.Name = "downloadEmailAddressButton"; this.downloadEmailAddressButton.Size = new System.Drawing.Size(252, 56); this.downloadEmailAddressButton.TabIndex = 4; @@ -94,11 +97,43 @@ private void InitializeComponent() this.emailCountLabel.TabIndex = 5; this.emailCountLabel.Text = "Email Count"; // + // badEmailsButton + // + this.badEmailsButton.Location = new System.Drawing.Point(921, 132); + this.badEmailsButton.Name = "badEmailsButton"; + this.badEmailsButton.Size = new System.Drawing.Size(140, 40); + this.badEmailsButton.TabIndex = 6; + this.badEmailsButton.Text = "Bad Emails"; + this.badEmailsButton.UseVisualStyleBackColor = true; + this.badEmailsButton.Click += new System.EventHandler(this.BadEmailsButton_Click); + // + // badEmailsSelection + // + this.badEmailsSelection.Location = new System.Drawing.Point(857, 14); + this.badEmailsSelection.Multiline = true; + this.badEmailsSelection.Name = "badEmailsSelection"; + this.badEmailsSelection.Size = new System.Drawing.Size(304, 101); + this.badEmailsSelection.TabIndex = 7; + // + // badEmailListView + // + this.badEmailListView.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Nonclickable; + this.badEmailListView.HideSelection = false; + this.badEmailListView.Location = new System.Drawing.Point(817, 256); + this.badEmailListView.Name = "badEmailListView"; + this.badEmailListView.Size = new System.Drawing.Size(344, 335); + this.badEmailListView.TabIndex = 8; + this.badEmailListView.UseCompatibleStateImageBehavior = false; + this.badEmailListView.View = System.Windows.Forms.View.List; + // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1198, 744); + this.Controls.Add(this.badEmailListView); + this.Controls.Add(this.badEmailsSelection); + this.Controls.Add(this.badEmailsButton); this.Controls.Add(this.emailCountLabel); this.Controls.Add(this.downloadEmailAddressButton); this.Controls.Add(this.emailListView); @@ -119,6 +154,9 @@ private void InitializeComponent() private System.Windows.Forms.ListView emailListView; private System.Windows.Forms.Button downloadEmailAddressButton; private System.Windows.Forms.Label emailCountLabel; + private System.Windows.Forms.Button badEmailsButton; + private System.Windows.Forms.TextBox badEmailsSelection; + private System.Windows.Forms.ListView badEmailListView; } } diff --git a/Form1.cs b/Form1.cs index 7e0e194..c184283 100644 --- a/Form1.cs +++ b/Form1.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Text; using System.Windows.Forms; +using static System.Windows.Forms.ListView; namespace EmailAddressExtracter { @@ -31,49 +32,76 @@ private void ChooseFileButton_Click(object sender, EventArgs e) } } - ProcessStartInfo startInfo = new ProcessStartInfo(); - - private void ExtractEmailAddressButton_Click(object sender, EventArgs e) + private ProcessStartInfo grepStartInfo(string fileName) { - MessageBoxButtons buttons = MessageBoxButtons.OK; - if (!ofd.CheckFileExists) - { - MessageBox.Show("Invalid file", "test", buttons); - return; - } + ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "grep.exe"; startInfo.RedirectStandardOutput = true; startInfo.UseShellExecute = false; startInfo.CreateNoWindow = true; startInfo.Arguments = "-a -o -P \"(?[\\w.]+)\\@(?\\w+\\.\\w+)(\\.\\w+)?\" " - + '"' + ofd.FileName +'"'; - - Console.WriteLine("Grep arguments: " + startInfo.Arguments); + + '"' + fileName + '"'; + return startInfo; + } + private string[] getTextfromProcess(ProcessStartInfo startInfo) + { + string[] text = null; using (Process process = Process.Start(startInfo)) { using (StreamReader reader = process.StandardOutput) { - string result = reader.ReadToEnd(); - emailListView.View = View.Details; - emailListView.GridLines = true; - emailListView.Columns.Add("Email", -2, HorizontalAlignment.Left); - string[] emails = result.Split('\n'); - Console.WriteLine("emails Count" + emails.Length.ToString()); - foreach(string x in emails) - { - emailListView.Items.Add(x); - } - if(emails.Length > 0) - { - emailCountLabel.Text = "Email Count: " + emails.Length; - downloadEmailAddressButton.Enabled = true; - } + text = reader.ReadToEnd().Split('\n').Where(x => x.Trim().Length > 0).ToArray(); } } + return text; } + private void populateEmailView(ListView listView, string[] emails) + { + listView.Clear(); + listView.View = View.Details; + listView.GridLines = true; + listView.Columns.Add("Email", -2, HorizontalAlignment.Left); - SaveFileDialog sfd = new SaveFileDialog(); + foreach (string x in emails) + { + listView.Items.Add(x); + } + } + private void ExtractEmailAddressButton_Click(object sender, EventArgs e) + { + if (!ofd.CheckFileExists) + { + MessageBox.Show("Invalid file: " + ofd.FileName); + return; + } + ProcessStartInfo startInfo = grepStartInfo(ofd.FileName); + Console.WriteLine("Grep arguments: " + startInfo.Arguments); + string[] emails = getTextfromProcess(startInfo); + Console.WriteLine("extract email Count" + emails.Length.ToString()); + + populateEmailView(emailListView, emails); + + if (emails.Length > 0) + { + emailCountLabel.Text = "Email Count: " + emails.Length; + downloadEmailAddressButton.Enabled = true; + } + else + { + emailCountLabel.Text = "No Emails found"; + } + } + SaveFileDialog sfd = new SaveFileDialog(); + private List cleanEmails(ListView emailListView, ListView badEmailListView) + { + List lv = emailListView.Items.Cast().ToList(); + var emails = from email in emailListView.Items.Cast() + select email.Text.Trim(); + var badEmails = from email in badEmailListView.Items.Cast() + select email.Text.Trim(); + return emails.Except(badEmails).ToList(); + } private void DownloadEmailAddressButton_Click(object sender, EventArgs e) { sfd.Title = "Save Emails"; @@ -82,14 +110,36 @@ private void DownloadEmailAddressButton_Click(object sender, EventArgs e) { using (Stream myStream = sfd.OpenFile()) { - using(StreamWriter sw = new StreamWriter(myStream)) - foreach (ListViewItem item in emailListView.Items) + using (StreamWriter sw = new StreamWriter(myStream)) { - sw.WriteLine(item.Text.Trim()); + List emails = cleanEmails(emailListView, badEmailListView); + emails.ForEach(x => sw.WriteLine(x)); + Console.WriteLine("Downloading emails: " + emails.Count); } MessageBox.Show("Download Complete"); } } } + + OpenFileDialog badEmailsOpenFileDialog = new OpenFileDialog(); + private void BadEmailsButton_Click(object sender, EventArgs e) + { + if (badEmailsOpenFileDialog.ShowDialog() != DialogResult.OK) + { + return; + } + badEmailsSelection.Text = ofd.FileName; + + if (!badEmailsOpenFileDialog.CheckFileExists) + { + MessageBox.Show("Invalid file: " + badEmailsOpenFileDialog.FileName); + return; + } + + ProcessStartInfo startInfo = grepStartInfo(badEmailsOpenFileDialog.FileName); + Console.WriteLine("Bad Emails Grep arguments: " + startInfo.Arguments); + string[] badEmails = getTextfromProcess(startInfo); + populateEmailView(badEmailListView, badEmails); + } } }