public async Task UpdateTitle(string GrupoId) { IEnumerable <Anuncio> anuncios = await GetByGroup(GrupoId); IEnumerable <FormUpdateAnuncio> dataAnuncios = GetData(anuncios.Select(a => a.Url).ToArray()); int len = anuncios.Count(); for (int i = 0; i < len; i++) { try { FormUpdateAnuncio d = dataAnuncios.ElementAt(i); string t = d.variables.title, c = d.variables.categoria; if (String.IsNullOrEmpty(t) && String.IsNullOrEmpty(anuncios.ElementAt(i).Titulo)) { anuncios.ElementAt(i).Titulo = "-- título no actualizado -- "; } else if (!String.IsNullOrEmpty(t)) { anuncios.ElementAt(i).Titulo = t; } if (!String.IsNullOrEmpty(c)) { anuncios.ElementAt(i).Categoria = c; } } catch (Exception ex) { _log.LogError(ex.ToExceptionString()); } } await repositoryAnuncio.SaveChangesAsync(); }
private IEnumerable <FormUpdateAnuncio> GetData(string[] links) { List <Task <string> > body = new List <Task <string> >(); foreach (string st in links) { body.Add(Requests.GetAsync(st)); } try { Task.WaitAll(body.ToArray()); } catch (Exception ex) { _log.LogError(ex.ToExceptionString()); } List <FormUpdateAnuncio> ans = new List <FormUpdateAnuncio>(); int len = links.Length; for (int i = 0; i < len; i++) { try { FormUpdateAnuncio formAnuncio = ParseFormAnuncio(body[i].Result); ans.Add(formAnuncio); } catch (Exception) { ans.Add(null); } } return(ans); }
public async Task <bool> IsListed(Anuncio anuncio) { try { string anuncioContent = await Requests.GetAsync(anuncio.Url); FormUpdateAnuncio formAnuncio = null; for (int i = 0; i < 5; i++) { try { formAnuncio = _anuncioService.ParseFormAnuncio(anuncioContent); break; } catch (Exception) { await Task.Delay(TimeSpan.FromSeconds(15)); continue; } } int cnt = 5; string url = $"{Requests.RevolicoBaseUrl}/{formAnuncio.variables.categoria}"; string urlPage2 = $"{url}pagina-2.html?"; string html; while (cnt > 0) { await Task.Delay(TimeSpan.FromSeconds(30)); html = await Requests.GetAsync(url); if (html.Contains(formAnuncio.variables.title)) { return(true); } html = await Requests.GetAsync(urlPage2); if (html.Contains(formAnuncio.variables.title)) { return(true); } await Task.Delay(TimeSpan.FromSeconds(15)); cnt--; } return(false); } catch (Exception ex) { _log.LogError(ex.ToExceptionString()); return(false); } }
public async Task <bool> DeleteFromRevolico(string url) { string htmlAnuncio = await Requests.GetAsync(url); FormUpdateAnuncio formAnuncio = ParseFormAnuncio(htmlAnuncio); FormDeleteAnuncio formDeleteAnuncio = new FormDeleteAnuncio(formAnuncio); return(await DeleteFromRevolico(formDeleteAnuncio)); }
public async Task <ReinsertResult> ReInsert(Anuncio anuncio, string Key2Captcha, string email) { ReinsertResult result; CaptchaAnswer captchaResponse; FormInsertAnuncio formInsertAnuncio; FormDeleteAnuncio formDeleteAnuncio; try { // Get Anuncio string htmlAnuncio = await Requests.GetAsync(anuncio.Url); GetException(htmlAnuncio, anuncio.Url, false); // Parse All Data FormUpdateAnuncio formAnuncio = ParseFormAnuncio(htmlAnuncio); formAnuncio.variables.email = email; anuncio.FormUpdateAnuncio = JsonConvert.SerializeObject(formAnuncio); //Solve Captcha captchaResponse = await ResolveCaptcha(Key2Captcha, Requests.RevolicoInserrUrl, htmlAnuncio); formAnuncio.variables.captchaResponse = captchaResponse.Answerv2; //formAnuncio.variables.botScore = captchaResponse.Answerv3; // Parse Insert and Delete Forms formInsertAnuncio = new FormInsertAnuncio(formAnuncio); formDeleteAnuncio = new FormDeleteAnuncio(formAnuncio); // Insert new Announce string answer = await InsertAnuncio(formInsertAnuncio); // Verify Insertion GetException(answer, anuncio.Url, true, captchaResponse); // Update new Anuncio URL InsertResult insertResult = ParseInsertResult(answer); anuncio.Url = $"{Requests.RevolicoModifyUrl}?key={insertResult.FullId}"; _log.LogWarning($"ReplaceInsert {anuncio.Id} {insertResult.FullId}"); // Delete from Revolico await DeleteFromRevolico(formDeleteAnuncio); result = new ReinsertResult(anuncio); } catch (Exception ex) { result = new ReinsertResult(anuncio, ex); } return(result); }
public FormUpdateAnuncio ParseFormAnuncio(string htmlAnuncio) { try { FormUpdateAnuncio formAnuncio = new FormUpdateAnuncio(); HtmlDocument doc = new HtmlDocument(); // Load the html from a string doc.LoadHtml(htmlAnuncio); var tmp = doc.DocumentNode.SelectSingleNode("//*[@name='price']"); int price; _ = int.TryParse(tmp.Attributes["value"].Value, out price); formAnuncio.variables.price = price; tmp = doc.DocumentNode.SelectSingleNode("//*[@name='title']"); formAnuncio.variables.title = HttpUtility.HtmlDecode(tmp.Attributes["value"].Value); tmp = doc.DocumentNode.SelectSingleNode("//textarea[@name='description']"); // Decode the encoded string. formAnuncio.variables.description = HttpUtility.HtmlDecode(tmp.InnerText); formAnuncio.variables.images = new string[0]; List <string> imagesId = new List <string>(); int lastPos = 0, posIni = 0, posEnd; posIni = htmlAnuncio.IndexOf("gcsKey", lastPos); while (posIni != -1) { posEnd = htmlAnuncio.IndexOf("urls", posIni); posIni += 9; posEnd -= 3; string id = htmlAnuncio.Substring(posIni, posEnd - posIni); imagesId.Add(id); lastPos = posEnd; posIni = htmlAnuncio.IndexOf("gcsKey", lastPos); } formAnuncio.variables.images = imagesId.ToArray(); tmp = doc.DocumentNode.SelectSingleNode("//*[@name='email']"); formAnuncio.variables.email = tmp.Attributes["value"].Value; tmp = doc.DocumentNode.SelectSingleNode("//*[@name='name']"); formAnuncio.variables.name = tmp.Attributes["value"].Value; tmp = doc.DocumentNode.SelectSingleNode("//*[@name='phone']"); formAnuncio.variables.phone = tmp.Attributes["value"].Value; tmp = doc.DocumentNode.SelectSingleNode("//*[@name='subcategory']"); formAnuncio.variables.subcategory = tmp.FirstChild.Attributes["value"].Value; formAnuncio.variables.contactInfo = "EMAIL_PHONE"; formAnuncio.variables.botScore = ""; formAnuncio.variables.categoria = GetCategoria(htmlAnuncio); int p1 = htmlAnuncio.IndexOf("pageProps") + "pageProps".Length + 2; int p2 = htmlAnuncio.IndexOf("apolloState") - 2; string substr = htmlAnuncio.Substring(p1, p2 - p1); dynamic prop = JObject.Parse(substr); formAnuncio.variables.token = prop.token; formAnuncio.variables.id = prop.id; return(formAnuncio); } catch (Exception ex) { throw new GeneralException(ex.Message + "\n" + ex.StackTrace + "\n --- \n" + htmlAnuncio, ""); } }