public static void getbndata(List<string> isbns) { // for each isbn, get info from bn.com using (WebClient client = new WebClient()) { foreach (string isbn in isbns) { bndata bndata = new bndata(); bndata.isbn13 = isbn; //create url for bn with incoming isbn string url = "http://www.barnesandnoble.com/s/" + isbn + "?store=book&keyword=" + isbn; //add user agent client.Headers.Add("user-agent", "User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31"); string html = client.DownloadString(url); HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(html); //parse through the html HtmlAgilityPack.HtmlNode ratingnode = doc.DocumentNode.SelectSingleNode("//span[@class='starDisplay']"); try { HtmlAgilityPack.HtmlNode starnode = ratingnode.SelectSingleNode("//span/span[1]"); string ratingstring = starnode.GetAttributeValue("title", "attribute not found"); // if rating is found, string will be "Average rating of 4.33333 out of 5 stars" // exception cases: "attribute not found", or "No rating data available yet" if (ratingstring != "attribute not found" && ratingstring != "No rating data available yet") { string rPattern = "(\\d+(?:\\.\\d*)?|\\.\\d+)"; Match rMatch = Regex.Match(ratingstring, rPattern); string rating = rMatch.Value; bndata.rating = decimal.Parse(rating) * .02M; } } catch { } // get Product Details node, searching for Age Range HtmlAgilityPack.HtmlNodeCollection detailsnode = doc.DocumentNode.SelectNodes("//div[@class='product-details box']/ul/li"); try { foreach (HtmlAgilityPack.HtmlNode detailnode in detailsnode) { //check through details nodes searching for <span>Age range: </span> //the text node which follows is the age range string agerangestring = "no age range found"; string tPattern = "Age range"; Match tMatch = Regex.Match(detailnode.InnerHtml, tPattern); if (tMatch.Success) { agerangestring = detailnode.InnerHtml; string arPattern = "(\\d+)"; MatchCollection arMatches = Regex.Matches(agerangestring, arPattern); if (arMatches.Count > 0) { bndata.agelow = int.Parse(arMatches[0].Value); } if (arMatches.Count > 1) { bndata.agehigh = int.Parse(arMatches[1].Value); } } } } catch { } loadbndata(bndata); } } }
public static void loadbndata(bndata bndata) { // receive bndata, load to database using (LexileTitlesEntities lr = new LexileTitlesEntities()) { List<BarnesAndNoble> existing = new List<BarnesAndNoble>(from b in lr.BarnesAndNobles where b.Isbn13 == bndata.isbn13 select b); if (existing.Count == 0) { // create new BarnesAndNoble BarnesAndNoble bn = new BarnesAndNoble(); bn.Isbn13 = bndata.isbn13; bn.AgeHigh = bndata.agehigh; bn.AgeLow = bndata.agelow; bn.Rating = bndata.rating; lr.BarnesAndNobles.Add(bn); lr.SaveChanges(); } else { // update fields on existing BarnesAndNoble existing[0].AgeHigh = bndata.agehigh; existing[0].AgeLow = bndata.agelow; existing[0].Rating = bndata.rating; lr.SaveChanges(); } } }