public async Task <IActionResult> PutContextAsync([FromBody] Models.Url url) { try { if (context.Url.Find(url.Index) is Models.Url model) { model.Record = url.Record; model.Json = url.Json; } else if (context.Theme.Find(url.Index) is Models.Theme theme) { theme.Url = url; } else { return(NoContent()); } return(Ok(await context.SaveChangesAsync())); } catch (Exception ex) { Console.WriteLine($"{GetType()}\n{url.Index}\n{ex.Message}\n{url.Record}\n{nameof(this.PutContextAsync)}"); } return(BadRequest()); }
public async Task <UrlResponse> GetUrlAsync(string url) { var _lstUrl = new List <Url>(); try { var _doc = await ProcesaProductoPorURLAsync(url); if (!_doc.Header.Correcto) { await new LogController(Configuration).InsertLog(new LogErrores() { URL = url, Observaciones = "Error al ProcesaProductoPorURLAsync: " + _doc.Header.Observación }); throw new Exception(_doc.Header.Observación); } var _url = new Models.Url() { URL = url, CantPaginas = _doc.FullObject.State.SearchItemList.PagesTotal }; //TODO insertar solo si existe... await InsertUrl(_url); _lstUrl.Add(_url); return(new UrlResponse() { Header = new Header() { Correcto = true, FechaProceso = DateTime.Now, Observación = "URL procesadas correctamente", }, Urls = _lstUrl }); } catch (Exception ex) { return(new UrlResponse() { Header = new Header() { Correcto = false, FechaProceso = DateTime.Now, Observación = ex.Message } }); } }
public async Task <UrlResponse> GetUrlsAsync() { var _lstUrl = new List <Url>(); try { var _htmlDocument = await ObtenerHtmlDocumentAsync("https://www.falabella.com/falabella-cl"); var _menu = _htmlDocument.DocumentNode.SelectNodes(string.Format("//li[@class='fb-masthead__primary-links__item']")); foreach (var item in _menu) { //Por cada menú, recorro las categorías var cat = new HtmlAgilityPack.HtmlDocument(); cat.LoadHtml(item.InnerHtml); var _cat = cat.DocumentNode.SelectNodes(string.Format("//li[@class='fb-masthead__child-links__item']")); foreach (var item2 in _cat) { //Ahora que estoy en la categoría, obtengo el link de "Todos" var _link = new HtmlAgilityPack.HtmlDocument(); _link.LoadHtml(item2.InnerHtml); var link = _link.DocumentNode.SelectNodes(string.Format("//li[@class='fb-masthead__grandchild-links__item']")); var url = link.LastOrDefault().SelectNodes("a")[0].Attributes["href"].Value; url = BASE_URL + url + "?isPLP=1"; //No sé por que no viene en el htmlDocument... //La inserto en la BD //TODO --> Debo validar si la URL ya existe. En caso contrario la agrego: //Necesito saber la cantidad de páginas de la url... var _doc = await ProcesaProductoPorURLAsync(url); if (!_doc.Header.Correcto) { await new LogController(Configuration).InsertLog(new LogErrores() { URL = url, Observaciones = "Error al ProcesaProductoPorURLAsync: " + _doc.Header.Observación }); continue; } var _url = new Models.Url() { URL = url, CantPaginas = _doc.FullObject.State.SearchItemList.PagesTotal }; //TODO insertar solo si existe await InsertUrl(_url); _lstUrl.Add(_url); } } return(new UrlResponse() { Header = new Header() { Correcto = true, FechaProceso = DateTime.Now, Observación = "URL's procesadas correctamente", }, Urls = _lstUrl }); } catch (Exception ex) { return(new UrlResponse() { Header = new Header() { Correcto = false, FechaProceso = DateTime.Now, Observación = ex.Message } }); } }
public ActionResult Index(String Url) { if (string.IsNullOrEmpty(Url)) { return(HttpNotFound()); } string ClientHash = string.Empty; Db.Client client = null; int? clientid = null; if (!string.IsNullOrEmpty(Request.QueryString["cid"])) { ClientHash = Request.QueryString["cid"].ToString(); } if (!string.IsNullOrEmpty(ClientHash)) { client = (from item in db.Clients where item.Hash == ClientHash select item).FirstOrDefault(); } if (client != null) { clientid = client.ClientId; } //check the database for the url Db.Url CheckUrl = db.GetUrlByValue(Url, clientid).FirstOrDefault(); //(from item in db.Urls where item.Value.Equals(Url) select item).FirstOrDefault(); bool UpdateUrl = false; if (CheckUrl != null) { UpdateUrl = CheckUrl.DateUpdated <= DateTime.Now.AddDays(-7); } if (CheckUrl == null || UpdateUrl) { if (!UpdateUrl) { CheckUrl = new Db.Url(); } //get the site..& parse the content Utility.UrlInfo UrlInfo = Utility.ParseUrl.getUrlInfo(Url); CheckUrl.Value = UrlInfo.URL; CheckUrl.Title = UrlInfo.Title; CheckUrl.Description = UrlInfo.Description; CheckUrl.DateUpdated = DateTime.Now; //save the content if (!UpdateUrl) { CheckUrl.ViewCount = 1; CheckUrl.DateAdded = DateTime.Now; Db.UrlView FirstView = new Db.UrlView(); FirstView.DateAdded = DateTime.Now; FirstView.ClientId = clientid; CheckUrl.UrlViews.Add(FirstView); db.Urls.AddObject(CheckUrl); } db.SaveChanges(); } Models.Url output = new Models.Url(); output.Id = CheckUrl.UrlId; output.Value = CheckUrl.Value; output.Title = HttpUtility.HtmlDecode(CheckUrl.Title); //wellicht deze regel alleen bij sharen van linkedin? output.Description = HttpUtility.HtmlDecode(CheckUrl.Description); //System.Threading.Thread.Sleep(1000); //output the correct return(Json(output, JsonRequestBehavior.AllowGet)); }