// working with only sweetwater.com for now private PriceListing AssembleListingFromData(string url, string htmlData) { string titlePattern = @"<title>(.*?)</title>"; string pricePattern = @"<meta itemprop=""price"" content=\""(\d+).(\d+)\"" />"; var title = Regex.Match(htmlData, titlePattern).ToString(); var price = Regex.Match(htmlData, pricePattern).ToString(); if (string.IsNullOrEmpty(title)) { throw new System.Exception("Title not found"); } if (string.IsNullOrEmpty(price)) { throw new System.Exception("Price not found"); } title = title.Substring(title.IndexOf('>') + 1); title = title.Substring(0, title.IndexOf('<')); price = Regex.Match(price, @"(\d+).(\d+)").ToString(); PriceListing pl = new PriceListing(); pl.Url = url; pl.ItemTitle = title; pl.ItemPrice = price; return(pl); }
public async Task <IActionResult> PutPriceListing(long id, PriceListing priceListing) { if (id != priceListing.Id) { return(BadRequest()); } var priceListingDb = await _context.PriceListings.FindAsync(id); if (priceListingDb == null) { return(NotFound()); } priceListingDb.Url = priceListing.Url; priceListingDb.ItemPrice = priceListing.ItemPrice; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) when(!PriceListingExists(id)) { return(NotFound()); } return(NoContent()); }
public async Task <ActionResult <PriceListing> > PostPriceListing(PriceListing priceListing) { string htmlData = null; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(priceListing.Url); request.UserAgent = @"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); if (response.StatusCode == HttpStatusCode.OK) { Stream recieveStream = response.GetResponseStream(); StreamReader readStream = null; if (string.IsNullOrWhiteSpace(response.CharacterSet)) { readStream = new StreamReader(recieveStream); } else { readStream = new StreamReader(recieveStream, Encoding.GetEncoding(response.CharacterSet)); } htmlData = readStream.ReadToEnd(); response.Close(); readStream.Close(); } if (string.IsNullOrWhiteSpace(htmlData)) { throw new System.Exception("Error: Could not connect to web page"); } PriceListing pl = AssembleListingFromData(priceListing.Url, htmlData); _context.PriceListings.Add(pl); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetPriceListing), new { id = pl.Id }, pl)); }