public ActionResult <SuccessResponse> GetCoinInfoScraper() { ActionResult results = null; var webClient = new WebClient(); var html = webClient.DownloadString("https://coinmarketcap.com/"); var parser = new HtmlParser(); var document = parser.ParseDocument(html); var table = document.QuerySelector(".table"); var rows = table.QuerySelectorAll("tr").Skip(1); foreach (var row in rows) { var coinInfoModel = new CoinInfoModel(); coinInfoModel.Name = row.QuerySelector("td:nth-child(2)").TextContent; coinInfoModel.MarketCap = row.QuerySelector("td:nth-child(3)").TextContent; coinInfoModel.Price = row.QuerySelector("td:nth-child(4)").TextContent; coinInfoModel.Volume = row.QuerySelector("td:nth-child(5)").TextContent; coinInfoModel.CirculatingSupply = row.QuerySelector("td:nth-child(6)").TextContent; coinInfoModel.Change = row.QuerySelector("td:nth-child(7)").TextContent; int addCoinInfo = _coinInfoScraperService.InsertCoinInfoScraper(coinInfoModel); } return(Ok(results)); }
public int InsertCoinInfoScraper(CoinInfoModel model) { int id = 0; using (SqlConnection con = new SqlConnection(connString)) { SqlCommand command = new SqlCommand("dbo.CoinInfoScraper_Insert", con); command.CommandType = CommandType.StoredProcedure; con.Open(); command.Parameters.Add(new SqlParameter("@Name", model.Name)); command.Parameters.Add(new SqlParameter("@MarketCap", model.MarketCap)); command.Parameters.Add(new SqlParameter("@Price", model.Price)); command.Parameters.Add(new SqlParameter("@Volume", model.Volume)); command.Parameters.Add(new SqlParameter("@CirculatingSupply", model.CirculatingSupply)); command.Parameters.Add(new SqlParameter("@Change", model.Change)); SqlParameter outputParameter = new SqlParameter(); outputParameter.ParameterName = "@Id"; outputParameter.SqlDbType = SqlDbType.Int; outputParameter.Direction = ParameterDirection.Output; command.Parameters.Add(outputParameter); command.ExecuteNonQuery(); con.Close(); id = (int)outputParameter.Value; } return(id); }