public static string GetEmail(List <string> emailList) { string emailToReturn = ""; Dictionary <string, SalesForce.EMAIL_WEIGHT> emailWeights = new Dictionary <string, SalesForce.EMAIL_WEIGHT>(); foreach (string email in emailList) { string hostDomain = email.Split('@')[1]; string domain = hostDomain.Split('.')[0]; if (emailWeights.ContainsKey(domain)) { continue; } else { emailWeights.Add(domain, SalesForce.GetWeight(domain.ToUpper())); } } var myList = emailWeights.ToList(); myList.Sort((pair2, pair1) => pair1.Value.CompareTo(pair2.Value)); emailToReturn = myList[0].Key; return(emailToReturn); }
private static Dictionary <string, EmailRecord> getNameValueByElementType( HtmlAgilityPack.HtmlDocument source, SalesForce salesForce ) { Dictionary <string, EmailRecord> output = new Dictionary <string, EmailRecord>(); var document = source.DocumentNode; foreach (KeyValuePair <string, bool> emailItem in salesForce.emailHeaderIdentities) { if (emailItem.Value) { HtmlAgilityPack.HtmlNode editNode = source.GetElementbyId(emailItem.Key); if (editNode.Attributes.ToList().Count(x => x.Name == "value") >= 1) { HtmlAgilityPack.HtmlAttribute attribute = editNode.Attributes["value"]; EmailRecord record = new EmailRecord(); record.emailAddress = attribute.Value; output.Add(emailItem.Key, record); } } } return(output); }
/// <summary> /// /// </summary> /// <param name="source"></param> /// <param name="advanced"></param> /// <returns></returns> private static void getNameValueByElementTypeVoid( HtmlAgilityPack.HtmlDocument source, SalesForce salesForce ) { var document = source.DocumentNode; HtmlAgilityPack.HtmlNode editNode = document.QuerySelector(salesForce.elementOne); HtmlAgilityPack.HtmlAttribute attribute = editNode.Attributes["href"]; salesForce.endpointEditSearchFieldTemplate = attribute.Value; }
private void button2_Click(object sender, EventArgs e) { // track enter and exit this.breakOutExited = false; if (lblExcelFileStatus.BackColor != Color.DarkGreen || lblConfirmedWorksheet.BackColor != Color.DarkGreen) { MessageBox.Show("Donna, you forgot a step make sure everything is green!", "Hey Donna... :-)"); return; } KeyInput input = new KeyInput(); ApplicationSettings settings = new ApplicationSettings(); // set csv output string csvOutput = settings.FILE_EXPORT_PATH; // remove files CleanUp(settings.WEB_EXPORT_PATH); try { // on new run, remove output file if (File.Exists(csvOutput)) { File.Copy(csvOutput, csvOutput.Replace(".csv", "_" + DateTime.Now.ToFileTime() + ".csv")); File.Delete(csvOutput); } } catch (Exception) { MessageBox.Show("Donna, did you close the Output.csv file? Close it, then press OK.", "Hey Donna... :-)"); File.Copy(csvOutput, csvOutput.Replace(".csv", "_" + DateTime.Now.ToFileTime() + ".csv")); File.Delete(csvOutput); } // exit if the file goes away if (!File.Exists(this.excelFileLocation)) { return; } List <string[]> idNumbers = uwExcel.createFromExcel(this.excelFileLocation, "A", "O", txtWorksheetName.Text); // excel row number from original int rowIndexNo = 1; int startFromIndex = 0; int endIndexRow = 0; // startFrom if (settings.START_AT_ROW > 0) { startFromIndex = settings.START_AT_ROW; } // startFrom if (settings.END_AT_ROW > 0) { endIndexRow = settings.END_AT_ROW; } List <SalesForce> salesForcePersons = new List <SalesForce>(); foreach (string[] person in idNumbers) { SalesForce salesForce = new SalesForce(SalesForce.TREE_TYPE.PERSON_SEARCH); // get default values salesForce.idNumber = person[0]; salesForce.oldEmail = person[14]; if (startFromIndex > 0) { if (rowIndexNo - 1 <= startFromIndex) { rowIndexNo++; continue; } } // skip if missing id number if (String.IsNullOrEmpty(person[0])) { rowIndexNo++; continue; } Process chrome = Process.Start(settings.DEFAULT_BROWSER_EXE, salesForce.GetNewEndpoint(person[0])); Thread.Sleep(settings.DEFAULT_SLEEP_INTERVAL); foreach (var browserInstruction in input.InstructionsBrowser) { SendKeys.SendWait(browserInstruction); Thread.Sleep(settings.DEFAULT_SLEEP_INTERVAL); } var html = Clipboard.GetText(); loadHtmlGetElementsBySelectorSalesForceGetEditLink(html, salesForce); // if no edit link returned skip //if (salesForce.endpointEditSearchFieldTemplate == "") //{ // rowIndexNo++; // continue; //} salesForce.rowNo = rowIndexNo; salesForcePersons.Add(salesForce); rowIndexNo++; if (endIndexRow > 0) { if (rowIndexNo - 1 >= endIndexRow) { rowIndexNo++; // break out as it is the end. break; } } } List <SalesForce> updatedSalesforceList = new List <SalesForce>(); foreach (SalesForce salesForceElement in salesForcePersons) { Process chromeEdit = Process.Start(settings.DEFAULT_BROWSER_EXE, salesForceElement.GetEditEndpoint()); Thread.Sleep(settings.DEFAULT_SLEEP_INTERVAL); foreach (var browserInstruction in input.InstructionsBrowser) { SendKeys.SendWait(browserInstruction); Thread.Sleep(settings.DEFAULT_SLEEP_INTERVAL); } var htmlEdit = Clipboard.GetText(); Dictionary <string, EmailRecord> emailDictionary = loadHtmlGetElementsBySelector(htmlEdit, salesForceElement); List <string> emailList = new List <string>(); // if no emails returned skip if (emailDictionary.Count > 0) { foreach (KeyValuePair <string, EmailRecord> emailRecord in emailDictionary) { EmailRecord record = emailRecord.Value; emailList.Add(record.emailAddress); } } // copy the object SalesForce salesForceNew = salesForceElement.DeepCopy(); // assign the new email list salesForceNew.emailRecordList = emailList; // make new list updatedSalesforceList.Add(salesForceNew); } foreach (SalesForce updatedSalesForceItem in updatedSalesforceList) { string emailToWrite = ""; bool hasEmailChanged = false; if (updatedSalesForceItem.emailRecordList.Count > 0) { // weigh out emails string domain = GetEmail(updatedSalesForceItem.emailRecordList); // find the email to write emailToWrite = updatedSalesForceItem.emailRecordList.Find(p => p.Contains(domain)); if (updatedSalesForceItem.oldEmail != emailToWrite || updatedSalesForceItem.oldEmail == "" && emailToWrite != "") { hasEmailChanged = true; } } // This text is always added, making the file longer over time // if it is not deleted. using (StreamWriter sw = File.AppendText(csvOutput)) { sw.WriteLine("{0},{1},{2},{3},{4}", updatedSalesForceItem.idNumber, hasEmailChanged, emailToWrite, updatedSalesForceItem.oldEmail, updatedSalesForceItem.rowNo); } } }
/// <summary> /// /// </summary> /// <param name="html"></param> /// <param name="advanced"></param> /// <returns></returns> public static void loadHtmlGetElementsBySelectorSalesForceGetEditLink(string html, SalesForce salesForce) { var source = new HtmlAgilityPack.HtmlDocument(); source.LoadHtml(html); getNameValueByElementTypeVoid(source, salesForce); }
public static Dictionary <string, EmailRecord> loadHtmlGetElementsBySelector(string html, SalesForce salesForce) { var source = new HtmlAgilityPack.HtmlDocument(); source.LoadHtml(html); return(getNameValueByElementType(source, salesForce)); }