public async Task <IEnumerable <LinkedinSkill> > GetLinkedinSkills() { LinkedinSkill linkedinSkill = null; List <LinkedinSkill> linkedinSkills = new List <LinkedinSkill>(); string comandtext = "select * from Monitoring.dbo.LinkedinSkill"; using (SqlConnection sqlConnection = new SqlConnection(connectionString)) { await sqlConnection.OpenAsync(); SqlCommand sqlCommand = new SqlCommand(comandtext, sqlConnection); using (SqlDataReader reader = sqlCommand.ExecuteReader()) { while (reader.Read()) { linkedinSkill = new LinkedinSkill { Id = (int)reader["id"], Name = reader["Name"] as string }; linkedinSkills.Add(linkedinSkill); } } return(linkedinSkills); } }
/// <summary> /// Get profile model from HTML content /// </summary> /// <param name="content">HTML content</param> /// <returns>LinkedinProfile</returns> public LinkedinProfile GetProfile(string content) { var document = new HtmlDocument(); var linkedinProfile = new LinkedinProfile(); try { document.LoadHtml(content); var fullName = document.DocumentNode.SelectSingleNode(".//h1[starts-with(@class,'pv-top-card-section__name')]")?.InnerText; var specialty = document.DocumentNode.SelectSingleNode(".//h2[starts-with(@class,'pv-top-card-section__headline')]")?.InnerText; var location = document.DocumentNode.SelectSingleNode(".//h3[starts-with(@class,'pv-top-card-section__location')]")?.InnerText; var company = document.DocumentNode.SelectSingleNode(".//span[@class='lt-line-clamp__line lt-line-clamp__line--last']")?.InnerText; var education = document.DocumentNode.SelectSingleNode(".//span[starts-with(@class,'pv-top-card-v2-section__entity-name pv-top-card-v2-section__school-name')]")?.InnerText; var connectionCountStr = document.DocumentNode.SelectSingleNode(".//span[starts-with(@class,'pv-top-card-v2-section__entity-name pv-top-card-v2-section__connections')]")?.InnerText; var imageUrlArray = document.DocumentNode.SelectSingleNode(".//div[starts-with(@class,'pv-top-card-section__photo presence-entity__image')]")?.GetAttributeValue("style", "")?.Split(new[] { "url("" }, StringSplitOptions.RemoveEmptyEntries); var imageUrl = imageUrlArray?.Count() == 0 || imageUrlArray == null ? null : imageUrlArray?.ElementAt(1)?.TrimEnd(';', ')', ';'); linkedinProfile.FullName = StringBeauty(fullName); linkedinProfile.Specialty = StringBeauty(specialty); linkedinProfile.Location = StringBeauty(location); linkedinProfile.Company = StringBeauty(company); linkedinProfile.Education = StringBeauty(education); int.TryParse(StringBeauty(connectionCountStr?.Replace("connections", "")?.Replace("+", "")), out var conCountInt); linkedinProfile.ConnectionCount = conCountInt; linkedinProfile.ImageUrl = HtmlDecode(imageUrl); } catch (Exception e) { Logger.Error(e, MethodBase.GetCurrentMethod().Name); } var experienceSection = document.DocumentNode.SelectSingleNode(".//section[@id='experience-section']"); if (experienceSection != null) { var companyCollection = experienceSection.SelectNodes(".//div[starts-with(@class,'pv-entity__summary-info')]"); if (companyCollection != null) { linkedinProfile.LinkedinExperiences = new List <LinkedinExperience>(); foreach (var experince in companyCollection) { try { var linkedinExperience = new LinkedinExperience(); var title = experince.SelectSingleNode(".//h3")?.InnerText?.Replace("Title", ""); var companyName = experince.SelectSingleNode(".//span[@class='pv-entity__secondary-title']")?.InnerText; var range = experince.SelectSingleNode(".//h4[starts-with(@class,'pv-entity__date-range')]")?.SelectNodes(".//span")?.Last()?.InnerText; var locationCompany = experince.SelectSingleNode(".//h4[starts-with(@class,'pv-entity__location')]")?.SelectNodes(".//span")?.Last()?.InnerText; linkedinExperience.Title = StringBeauty(title); linkedinExperience.Company = StringBeauty(companyName); linkedinExperience.Time = StringBeauty(range); linkedinExperience.Location = StringBeauty(locationCompany); linkedinProfile.LinkedinExperiences.Add(linkedinExperience); } catch (Exception e) { Logger.Error(e, MethodBase.GetCurrentMethod().Name); } } } } var educationSection = document.DocumentNode.SelectSingleNode(".//section[@id='education-section']"); if (educationSection != null) { var educationCollection = educationSection.SelectNodes(".//div[starts-with(@class,'pv-entity__summary-info')]"); if (educationCollection != null) { linkedinProfile.LinkedinEducations = new List <LinkedinEducation>(); foreach (var education in educationCollection) { try { var linkedinEducation = new LinkedinEducation(); var university = education.SelectSingleNode(".//h3")?.InnerText; linkedinEducation.Name = HtmlDecode(StringBeauty(university)); var titleCol = education.SelectNodes(".//span[starts-with(@class,'pv-entity__comma-item')]")?.Select(x => x.InnerText); if (titleCol != null) { var title = StringBeauty(string.Join(" of ", titleCol)); linkedinEducation.Title = title; } var range = education.SelectSingleNode(".//p[starts-with(@class,'pv-entity__dates')]")?.SelectNodes(".//time")?.Select(x => x?.InnerText); if (range != null) { var dateRange = string.Join(" - ", range); linkedinEducation.Time = StringBeauty(dateRange); } linkedinProfile.LinkedinEducations.Add(linkedinEducation); } catch (Exception e) { Logger.Error(e, MethodBase.GetCurrentMethod().Name); } } } } var skillSection = document.DocumentNode.SelectSingleNode(".//section[starts-with(@class,'pv-profile-section pv-skill-categories-section')]"); if (skillSection != null) { var skillCollection = skillSection.SelectNodes(".//div[@class='pv-skill-category-entity__skill-wrapper tooltip-container']"); if (skillCollection != null) { linkedinProfile.LinkedinSkills = new List <LinkedinSkill>(); foreach (var skillItem in skillCollection) { var linkedinSkill = new LinkedinSkill(); try { var skillName = skillItem.SelectSingleNode(".//p[starts-with(@class,'pv-skill-category-entity__name')]")?.InnerText; var uproveCount = skillItem.SelectSingleNode(".//span[starts-with(@class,'pv-skill-category-entity__endorsement-count')]")?.InnerText; linkedinSkill.Name = StringBeauty(skillName); int.TryParse(StringBeauty(uproveCount), out var uproveInt); linkedinSkill.EndorsedCount = uproveInt; linkedinProfile.LinkedinSkills.Add(linkedinSkill); } catch (Exception e) { Logger.Error(e, MethodBase.GetCurrentMethod().Name); } } } } var interestsSection = document.DocumentNode.SelectSingleNode(".//section[starts-with(@class,'pv-profile-section pv-interests-section')]"); if (interestsSection != null) { var interestsCollection = interestsSection.SelectNodes(".//div[@class='pv-entity__summary-info ember-view']"); if (interestsCollection != null) { linkedinProfile.LinkedinInterests = new List <LinkedinInterest>(); foreach (var interesItem in interestsCollection) { try { var linkedinInterest = new LinkedinInterest(); var name = interesItem.SelectSingleNode(".//span[@class='pv-entity__summary-title-text']")?.InnerText; var followersCount = interesItem.SelectSingleNode(".//p[starts-with(@class,'pv-entity__follower-count')]")?.InnerText?.Replace("followers", "")?.Replace(",", ""); int.TryParse(StringBeauty(followersCount), out var followersInt); linkedinInterest.Name = StringBeauty(name); linkedinInterest.FollowersCount = followersInt; linkedinProfile.LinkedinInterests.Add(linkedinInterest); } catch (Exception e) { Logger.Error(e, MethodBase.GetCurrentMethod().Name); } } } } var accomplishmentsSection = document.DocumentNode.SelectSingleNode(".//section[starts-with(@class,'pv-profile-section pv-accomplishments-section')]"); if (accomplishmentsSection != null) { var accomplishmentsCollection = accomplishmentsSection.SelectNodes(".//li[@class='pv-accomplishments-block__summary-list-item']"); if (accomplishmentsCollection != null) { linkedinProfile.LinkedinLanguages = new List <LinkedinLanguage>(); foreach (var accomplishmentItem in accomplishmentsCollection) { try { var linkedinInterest = new LinkedinLanguage { Name = StringBeauty(accomplishmentItem.InnerText) }; linkedinProfile.LinkedinLanguages.Add(linkedinInterest); } catch (Exception e) { Logger.Error(e, MethodBase.GetCurrentMethod().Name); } } } } try { var documentInfo = new HtmlDocument(); var findElementByXPath = _driver.FindElementByXPath(".//a[@data-control-name='contact_see_more']"); findElementByXPath.Click(); Thread.Sleep(700); documentInfo.LoadHtml(_driver.PageSource); var phone = documentInfo.DocumentNode.SelectSingleNode(".//section[@class='pv-contact-info__contact-type ci-phone']")?.SelectSingleNode(".//span[@class='Sans-15px-black-85%']")?.InnerText; var email = documentInfo.DocumentNode.SelectSingleNode(".//section[@class='pv-contact-info__contact-type ci-email']")?.SelectSingleNode(".//a[@class='pv-contact-info__contact-link Sans-15px-black-85%']")?.GetAttributeValue("href", "")?.Replace("mailto:", ""); var birthday = documentInfo.DocumentNode.SelectSingleNode(".//section[@class='pv-contact-info__contact-type ci-birthday']")?.SelectSingleNode(".//a[@class='pv-contact-info__contact-item Sans-15px-black-85%']")?.InnerText; var website = documentInfo.DocumentNode.SelectSingleNode(".//section[@class='pv-contact-info__contact-type ci-websites']")?.SelectSingleNode(".//a[starts-with(@class,'pv-contact-info__contact-link')]")?.GetAttributeValue("href", ""); //var userName = documentInfo.DocumentNode.SelectSingleNode(".//h1[@id='pv-contact-info']")?.InnerText; linkedinProfile.Email = StringBeauty(email); linkedinProfile.Phone = StringBeauty(phone); linkedinProfile.Birthday = StringBeauty(birthday); linkedinProfile.Website = StringBeauty(website); //linkedinProfile.Username = StringBeauty(userName); Thread.Sleep(250); var findElementByXPathClose = _driver.FindElementByXPath(".//button[@class='artdeco-dismiss']"); findElementByXPathClose?.Click(); Thread.Sleep(250); } catch (Exception e) { Logger.Error(e, MethodBase.GetCurrentMethod().Name); } try { var connectButton = _driver.FindElementByXPath(".//button[starts-with(@class,'pv-s-profile-actions pv-s-profile-actions--connect')]"); connectButton?.Click(); Thread.Sleep(1000); var sendNowButton = _driver.FindElementByXPath(".//button[starts-with(@class,'button-primary-large')]"); sendNowButton?.Click(); //pv-s-profile-actions pv-s-profile-actions--connect } catch (Exception e) { Logger.Error(e, MethodBase.GetCurrentMethod().Name); Console.WriteLine(e); } return(linkedinProfile); }