public static IEnumerable <MobileApiResult> SearchByName(SearchByNameRequest searchByNameRequest) { var result = new List <MobileApiResult>(); if (!searchByNameRequest.IsValid) { throw searchByNameRequest.Exception; } var client = new RestClient(BaseUrl); var request = new RestRequest(SearchByNameUrl, Method.POST) .AddParameter("memln", searchByNameRequest.LastName.Substring(0, Math.Min(searchByNameRequest.LastName.Length, 20))) .AddParameter("mode", "Search"); if (!string.IsNullOrWhiteSpace(searchByNameRequest.FirstName)) { request.AddParameter("memfn", searchByNameRequest.FirstName.Substring(0, Math.Min(searchByNameRequest.FirstName.Length, 20))); } if (!string.IsNullOrWhiteSpace(searchByNameRequest.State)) { request.AddParameter("memstate", searchByNameRequest.State); } var response = client.Execute(request); if (response.ResponseStatus == ResponseStatus.Completed) { var htmlContent = response.Content; var htmlDocument = new HtmlAgilityPack.HtmlDocument(); htmlDocument.LoadHtml(htmlContent); // second table always exists var rows = htmlDocument.DocumentNode.SelectNodes("//table")[1].SelectNodes("tr"); result = (from row in rows select row.SelectNodes("td") into cells where cells != null && (cells.Count == 3 || cells.Count == 4) let infocell = cells[2].InnerText.Trim('\n').Split('\n').Select(val => val.Trim()).ToArray() let comments = cells.Count == 4 ? cells[3].InnerText : string.Empty let rating = ParseRating(infocell[2]) let membershipExpirationDate = ParseExpirationDate(infocell[1]) select new MobileApiResult { UscfId = ParseMemberNumber(cells[0].InnerText.Trim('\n')), FullName = cells[1].InnerText.Trim('\n'), StateOrCountry = infocell[0], MembershipExpirationDate = membershipExpirationDate, MembershipStatus = ParseMembershipStatus(membershipExpirationDate, comments), RegularRating = rating, Comments = comments }).Where(item => item.UscfId > 0).ToList(); } return(result); }
public void Should_Search_UsChess_Site() { var searchByNameRequest = new SearchByNameRequest() { LastName = "ivanov" }; var resultApi = new Api().SearchByName(searchByNameRequest); //var resultDesktop = DesktopApi.SearchByName(searchByNameRequest); var ss = resultApi; }
// the logic here should be like this: // Go to the mobile api and run search eigther by the name/state, or by id. // if the name has more than 2 parts, then use desktop search using the id in question. // Desktop site returns the names in the form last name, first name, so it is easy to reconstruct the original first and last name public IEnumerable <UscfSearchRecord> SearchByName(SearchByNameRequest searchByNameRequest) { var mobileApiResult = MobileApi.SearchByName(searchByNameRequest).ToList(); return(ProcessSearchResults(mobileApiResult)); }