protected async Task ForceUpdate() { Debug.Write("ForeceUpdate"); var HttpResult = await GetWeb.GetWebPage("http://www.rzeszowiak.pl/kontakt/"); if (!HttpResult.Success) { Debug.Write("GetCategoryListAsync => !HttpResult.Success"); return; } if (HttpResult.BodyString.Length == 0) { Debug.Write("GetCategoryListAsync => responseString.Length == 0"); return; } UpdateCategoryList(HttpResult.BodyString); HttpResult.BodyString.Clear(); }
public async Task <bool> SendUserMessage(Advert advert, string message, string email) { Debug.Write("SendUserMessage"); if (advert == null || message?.Length == 0 || email.Length == 0) { Debug.Write("SendUserMessage: advert == null && message?.Length == 0"); return(false); } if (advert.EmailToken.Length != 10) { Debug.Write("SendUserMessage: advert.EmailToken.Length != 10"); return(false); } string Url = RZESZOWIAK_BASE_URL + $"wyslij-zapytanie-ze-strony/{advert.AdverIDinRzeszowiak}/"; Debug.Write(Url); var postData = new Dictionary <string, string> { ["token"] = advert.EmailToken, ["MAX_FILE_SIZE"] = "4000000", ["emailFK"] = email, ["wiadomoscFK"] = message, ["zgodaFormularzKontakt"] = "1", }; var HttpResult = await GetWeb.PostWebPage(Url, postData, advert.URLPath, null); if (HttpResult.IndexOf("{\"status\":\"ok\", \"opis\":\"Wiadomość została wysłana.\"}", 0, true) == -1) { Debug.Write("SendUserMessage => Confirm message not recive"); return(false); } return(true); }
public async Task <bool> DownloadImage(Advert advert, string ssid, int advertID, string advertURL, Cookie PHPSESSIDPcookie) { if (advert == null) { Debug.Write(this.GetType().Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " => advert == null"); return(false); } if ((advert.PhoneImageByteArray?.Length ?? 0) > 0) { Debug.Write(this.GetType().Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " => advert.PhoneImageByteArray exists"); ImageData = ImageSource.FromStream(() => new MemoryStream(advert.PhoneImageByteArray)); OnDownloadFinish?.Invoke(this, new EventArgs()); return(true); } if (ssid == null || advertID == 0 || advertURL.Length == 0 || PHPSESSIDPcookie == null) { Debug.Write(this.GetType().Name + "." + System.Reflection.MethodBase.GetCurrentMethod().Name + " => ssid == null || adrverID == 0 || advertURL.Length == 0"); return(false); } _session = new Guid().ToString(); var localSession = _session; var inputData = new Dictionary <string, string>() { { "oid", advertID.ToString() }, { "ssid", ssid }, }; var BodyResult = await GetWeb.PostWebPage("http://www.rzeszowiak.pl/telefon/", inputData, advertURL, new CookieCollection() { PHPSESSIDPcookie }); if (BodyResult.Length == 0) { Debug.Write("RzeszowiakImageContainer => DownloadImage => BodyResult.Length == 0"); return(false); } Base64ToImage(BodyResult.ToString().Replace("data:image/jpeg;base64,", ""), out ImageSource imageData, out byte[] byteArray); ImageData = imageData; advert.PhoneImageByteArray = byteArray; BodyResult.Clear(); if (localSession == _session) { OnDownloadFinish?.Invoke(this, new EventArgs()); } else { Debug.Write("RzeszowiakImageContainer => DownloadImage() => localSession != _session"); } return(true); }
public async Task <Advert> GetAdvertAsync(AdvertShort advertShort, Action <string> userNotify = null) { Debug.Write("GetAdvert(AdvertShort advertShort)"); if (advertShort == null) { throw new NullReferenceException("advertShort == null"); } var HttpResult = await GetWeb.GetWebPage(advertShort.URL); if (!HttpResult.Success) { Debug.Write("GetAdvertAsync => !HttpResult.Success"); return(null); } var BodyResult = HttpResult.BodyString; if (BodyResult.Length == 0) { Debug.Write("GetAdvertAsync => BodyResult.Length == 0"); return(null); } UpdateCategoryList(BodyResult); // update category String BodyResultString = BodyResult.ToString(); Match match = Regex.Match(BodyResultString, @"(<div class=""label"">kategoria :</div>\s*<div class=""value"">)(?'result'.+)(</div>)"); if (!match.Success) { Debug.Write("GetAdvertAsync => no category"); return(null); } string aCategory = match.Groups["result"].Value; match = Regex.Match(BodyResultString, @"(<div class=""label"">tytuł :</div>\s*<div class=""value"">)(?'result'.+)(?=</div>)"); if (!match.Success) { Debug.Write("GetAdvertAsync => no title"); return(null); } string aTitle = match.Groups["result"].Value; match = Regex.Match(BodyResultString, @"(<div class=""label"">data dodania :</div>\s*<div class=""value"">.*)(?'result'\d{4}-\d{2}-\d{2})(?=</div>)"); if (!match.Success) { Debug.Write("GetAdvertAsync => no expired date"); return(null); } string aDateExpiredString = match.Groups["result"].Value; if (DateTime.TryParse(aDateExpiredString, out DateTime aDateExpired)) { aDateExpired = aDateExpired.Add(advertShort.DateAddDateTime.TimeOfDay); } match = Regex.Match(BodyResultString, @"(<div class=""label"">wyświetleń :</div>\s*<div class=""value"">)(?'result'\d+)(?=.*</div>)"); if (!match.Success) { Debug.Write("GetAdvertAsync => no views"); return(null); } string aViews = match.Groups["result"].Value; if (!Int32.TryParse(aViews, out int aViewsInt)) { Debug.Write("GetAdvertAsync => !Int32.TryParse(aViews, out int aViewsInt)"); return(null); } match = Regex.Match(BodyResultString, @"(<div class=""label"">cena :</div>\s*<div class=""value"">)(?'result'\d+)(\sPLN</div>)"); if (!match.Success) { Debug.Write("GetAdvertAsync => no price"); return(null); } string aPrice = match.Groups["result"].Value; if (!Int32.TryParse(aPrice, out int aPriceInt)) { Debug.Write("GetAdvertAsync => !Int32.TryParse(aPrice, out int aPriceInt)"); return(null); } match = Regex.Match(BodyResultString, @"(<div class=""content"">)(?'result'.+)(?=</div>)"); if (!match.Success) { Debug.Write("GetAdvertAsync => no description"); return(null); } string aDesc = match.Groups["result"].Value.Replace("<br />", "\n\n").StripHTML().Replace("\n\n\n", "\n").Trim(); match = Regex.Match(BodyResultString, @"(id=""tokenFK"" value="")(?'result'[0-9a-f]+)(?="")"); if (!match.Success) { Debug.Write("GetAdvertAsync => no aEmailToken"); return(null); } string aEmailToken = match.Groups["result"].Value; string aSsid = String.Empty; match = Regex.Match(BodyResultString, @"(rel=""\d+\|)(?'result'[0-9a-f]+)(?="")"); if (match.Success) { aSsid = match.Groups["result"].Value; } //picture var pictureList = new List <string>(); match = Regex.Match(BodyResultString, @"(?s)(<div>Zdjęcia</div>.*?<div id=""photos""\s>.+?</div>)"); if (match.Success) { var matches = Regex.Matches(match.Value, @"(src=""(?'result'/img/ogl/[^""]+)"")"); foreach (Match m in matches) { var picUrl = RZESZOWIAK_BASE_URL + m.Groups["result"].Value.Replace("mini/o_", ""); pictureList.Add(picUrl); } } Cookie aCookie = null; foreach (var item in HttpResult.CookieList) { if (item.Name == "PHPSESSID") { aCookie = item; } } /* * BodyResult.CutFoward("ogloszeniebox-top"); * * string GetValue(StringBuilder bodyResult, string category) * { * BodyResult.CutFoward(category + "</div>"); * BodyResult.CutFoward("<div class=\"value\">"); * return BodyResult.ToString(0, BodyResult.IndexOf("</div>", 0, true)).Trim(); * } * * string aCategory = GetValue(BodyResult, "kategoria :"); * string aTitle = GetValue(BodyResult, "tytuł :").Trim(); * string aDateExpiredString = GetValue(BodyResult, "data dodania :").CutFoward(" - ").CutFoward(":"); * if(DateTime.TryParse(aDateExpiredString, out DateTime aDateExpired)) * aDateExpired = aDateExpired.Add(advertShort.DateAddDateTime.TimeOfDay); * * string aViews = GetValue(BodyResult, "wyświetleń :").Replace(" razy", ""); * if (!Int32.TryParse(aViews, out int aViewsInt)) * { * Debug.Write("GetAdvert(AdvertShort advertShort, Action<string> userNotify = null) => !Int32.TryParse(aViews, out int aViewsInt)"); * return null; * } * string aPrice = GetValue(BodyResult, "cena :").Replace(" PLN", ""); * if (!Int32.TryParse(aPrice, out int aPriceInt)) * { * Debug.Write("GetAdvert(AdvertShort advertShort, Action<string> userNotify = null) => !Int32.TryParse(aPrice, out int aPriceInt)"); * return null; * } * BodyResult.CutFoward("Treść ogłoszenia"); * BodyResult.CutFoward("<div class=\"content\">"); * string aDesc = BodyResult.ToString(0, BodyResult.IndexOf("</div>", 0, true)).Replace("<br />", "\n\n").StripHTML(); * aDesc = aDesc.Replace("\n\n\n", "\n").Trim(); * * // additionalData * var additionalData = new Dictionary<string, string>(); * if (BodyResult.IndexOf("<div>Dane dodatkowe</div>", 0, true) != -1) * { * BodyResult.CutFoward("<div>Dane dodatkowe</div>"); * StringBuilder addData = new StringBuilder(BodyResult.ToString(0, BodyResult.IndexOf("ogloszeniebox-bottom", 0, true))); * do * { * int posLab = addData.IndexOf("<div class=\"label\">", 0, true); * if (posLab == -1) break; * addData.CutFoward("<div class=\"label\">"); * string key = addData.ToString(0, addData.IndexOf("</div>", 0, true)); * addData.CutFoward("</div>"); * addData.CutFoward("\">"); * string value = addData.ToString(0, addData.IndexOf("</div>", 0, true)); * additionalData.Add(key, value); * } while (true); * } * // picture list * var pictureList = new List<string>(); * if (BodyResult.IndexOf("<div>Zdjęcia</div>", 0, true) != -1) * { * BodyResult.CutFoward("<div>Zdjęcia</div>"); * StringBuilder addData = new StringBuilder(BodyResult.ToString(0, BodyResult.IndexOf("ogloszeniebox-bottom", 0, true))); * do * { * int posLab = addData.IndexOf("<img src=\"/", 0, true); * if (posLab == -1) break; * addData.CutFoward("<img src=\"/"); * var picUrl = RZESZOWIAK_BASE_URL + addData.ToString(0, addData.IndexOf("\"", 0, true)).Replace("mini/o_", ""); * pictureList.Add(picUrl); * } while (true); * } * * // image sid * string aSsid = String.Empty; * if (BodyResult.IndexOf("rel=\"" + advertShort.AdverIDinRzeszowiak + "|", 0, true) != -1) * { * BodyResult.CutFoward("rel=\"" + advertShort.AdverIDinRzeszowiak + "|"); * aSsid = BodyResult.ToString(0, BodyResult.IndexOf("\"", 0, true)).Trim(); * } * Cookie aCookie = null; * foreach (var item in HttpResult.CookieList) * if (item.Name == "PHPSESSID") aCookie = item; * * //email token * string aEmailToken = String.Empty; * if (BodyResult.IndexOf("id=\"tokenFK\" value=\"", 0, true) != -1) * { * BodyResult.CutFoward("id=\"tokenFK\" value=\""); * aEmailToken = BodyResult.ToString(0, BodyResult.IndexOf("\"", 0, true)).Trim(); * } */ return(new Advert() { AdverIDinRzeszowiak = advertShort.AdverIDinRzeszowiak, Category = null, Title = aTitle, DateAddDateTime = advertShort.DateAddDateTime, ExpiredDatetime = aDateExpired, Views = aViewsInt, Price = aPriceInt, Highlighted = advertShort.Highlighted, DescriptionShort = advertShort.DescriptionShort, DescriptionHTML = aDesc, //AdditionalData = additionalData, ImageURLsList = pictureList, URLPath = advertShort.URLPath, PhoneSsid = aSsid, PhonePHPSSESION = aCookie, EmailToken = aEmailToken, VisitPageDate = DateTime.Now, ThumbnailUrl = advertShort.ThumbnailUrl, }); }
public async Task <AdvertSearchResult> GetAdvertListAsync(AdvertSearch searchParams, Action <string> userNotify = null) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams)"); if (searchParams == null) { throw new NullReferenceException("AdvertSearch is null"); } var resultList = new AdvertSearchResult() { Correct = false, ErrorMessage = String.Empty, Page = 1, AllPage = 1, AdvertShortsList = new List <AdvertShort>(), }; var urlRequest = searchParams.GetURL; if (urlRequest.Length == 0) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => urlRequest.Length == 0"); resultList.ErrorMessage = "Błędny URL"; return(resultList); } IEnumerable <AdvertShort> ProcessResponse(StringBuilder ResponseHTMLBody) { if (ResponseHTMLBody.Length == 0) { yield break; } int promoPos, normalPos; bool rowEven = false; do { promoPos = ResponseHTMLBody.IndexOf("promobox-title-left", 0, true); normalPos = ResponseHTMLBody.IndexOf("normalbox-title-left", 0, true); int nextPos = Math.Min(promoPos, normalPos); nextPos = (nextPos == -1 && promoPos > -1) ? promoPos : nextPos; nextPos = (nextPos == -1 && normalPos > -1) ? normalPos : nextPos; if (nextPos == -1) { yield break; } bool highlighted = (nextPos == promoPos) ? true : false; ResponseHTMLBody.Remove(0, nextPos); //url if (ResponseHTMLBody.IndexOf("<a href=\"/", 0, true) == -1) { Debug.Write("Task<AdvertSearchResult> GetAdvertListAsync => url not found"); yield break; } ResponseHTMLBody.CutFoward("<a href=\"/"); string aUrl = ResponseHTMLBody.ToString(0, ResponseHTMLBody.IndexOf("\">", 0, true)).Trim(); if (aUrl.Length == 0) { Debug.Write("Task<AdvertSearchResult> aUrl.Length == 0"); yield break; } //title ResponseHTMLBody.CutFoward("\">"); string aTitle = ResponseHTMLBody.ToString(0, ResponseHTMLBody.IndexOf("</a>", 0, true)).CutFoward(".").Trim(); if (aTitle.Length == 0) { Debug.Write("Task<AdvertSearchResult> aUrl.Length == 0"); yield break; } //price ResponseHTMLBody.CutFoward("cena: <strong>"); string aPrice = ResponseHTMLBody.ToString(0, ResponseHTMLBody.IndexOf("zł</strong>", 0, true)).Trim(); if (!Int32.TryParse(aPrice, out int aPriceInt)) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => !Int32.TryParse(aPrice, out int aPriceInt)"); aPriceInt = 0; } //id ResponseHTMLBody.CutFoward("id=\"zr"); string aId = ResponseHTMLBody.ToString(0, ResponseHTMLBody.IndexOf("\">", 0, true)).Trim(); if (!Int32.TryParse(aId, out int aIdInt)) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => !Int32.TryParse(aId, out int aIdInt))"); yield break; } //thumb ResponseHTMLBody.CutFoward("src=\""); string aThumb = ResponseHTMLBody.ToString(0, ResponseHTMLBody.IndexOf("\"", 0, true)).Trim(); //desc ResponseHTMLBody.CutFoward("window.location"); ResponseHTMLBody.CutFoward("\">"); string aDesc = ResponseHTMLBody.ToString(0, ResponseHTMLBody.IndexOf("</div>", 0, true)).StripHTML().Trim(); //add ResponseHTMLBody.CutFoward("Dodane : <b>"); string aAdd = ResponseHTMLBody.ToString(0, ResponseHTMLBody.IndexOf("</b>", 0, true)).Trim(); if (aAdd.Length == 0) { Debug.Write("Task<AdvertSearchResult> aAdd.Length == 0"); yield break; } yield return(new AdvertShort() { AdverIDinRzeszowiak = aIdInt, Title = aTitle, URLPath = aUrl, ThumbnailUrl = aThumb, DescriptionShort = aDesc, DateAddDateTime = GetDateTimeFromISR(aAdd), Price = aPriceInt, Category = searchParams.CategorySearch, Highlighted = highlighted, RowEven = rowEven }); rowEven = !rowEven; } while (promoPos != -1 || normalPos != -1); } var HttpResult = await GetWeb.GetWebPage(urlRequest); if (!HttpResult.Success) { Debug.Write("GetAdvertAsync => !HttpResult.Success"); return(resultList); } var responseString = HttpResult.BodyString; if (responseString.Length == 0) { resultList.ErrorMessage = "Sprawdź połączenie internetowe i spróbuj ponownie."; responseString.Clear(); return(resultList); } UpdateCategoryList(responseString); // update category foreach (var item in ProcessResponse(responseString)) { resultList.AdvertShortsList.Add(item); } if (responseString.IndexOf("<div id=\"oDnns\">Strona ", 0, true) != -1) { responseString.CutFoward("<div id=\"oDnns\">Strona "); string aPage = responseString.ToString(0, responseString.IndexOf("z", 0, true)).Trim(); if (!Int32.TryParse(aPage, out int aPageInt)) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => !Int32.TryParse(aPage, out int aPageInt)"); aPageInt = 0; } resultList.Page = aPageInt; responseString.CutFoward("z"); string aPageAll = responseString.ToString(0, responseString.IndexOf("</div>", 0, true)).Trim(); if (!Int32.TryParse(aPageAll, out int aPageAllInt)) { Debug.Write("GetAdvertListAsync(AdvertSearch searchParams) => !Int32.TryParse(aPageAll, out int aPageAllInt)"); aPageAllInt = 0; } resultList.AllPage = aPageAllInt; } responseString.Clear(); resultList.Correct = true; return(resultList); }
public async Task <Advert> GetAdvertAsync(AdvertShort advertShort, Action <string> userNotify = null) { Debug.Write("GetAdvert(AdvertShort advertShort)"); if (advertShort == null) { throw new NullReferenceException("advertShort == null"); } var HttpResult = await GetWeb.GetWebPage(advertShort.URL); if (!HttpResult.Success) { Debug.Write("GetAdvertAsync => !HttpResult.Success"); return(null); } var BodyResult = HttpResult.BodyString; if (BodyResult.Length == 0) { Debug.Write("GetAdvertAsync => BodyResult.Length == 0"); return(null); } UpdateCategoryList(BodyResult); // update category BodyResult.CutFoward("ogloszeniebox-top"); string GetValue(StringBuilder bodyResult, string category) { BodyResult.CutFoward(category + "</div>"); BodyResult.CutFoward("<div class=\"value\">"); return(BodyResult.ToString(0, BodyResult.IndexOf("</div>", 0, true)).Trim()); } string aCategory = GetValue(BodyResult, "kategoria :"); string aTitle = GetValue(BodyResult, "tytuł :").Trim(); string aDateExpiredString = GetValue(BodyResult, "data dodania :").CutFoward(" - ").CutFoward(":"); if (DateTime.TryParse(aDateExpiredString, out DateTime aDateExpired)) { aDateExpired = aDateExpired.Add(advertShort.DateAddDateTime.TimeOfDay); } string aViews = GetValue(BodyResult, "wyświetleń :").Replace(" razy", ""); if (!Int32.TryParse(aViews, out int aViewsInt)) { Debug.Write("GetAdvert(AdvertShort advertShort, Action<string> userNotify = null) => !Int32.TryParse(aViews, out int aViewsInt)"); return(null); } string aPrice = GetValue(BodyResult, "cena :").Replace(" PLN", ""); if (!Int32.TryParse(aPrice, out int aPriceInt)) { Debug.Write("GetAdvert(AdvertShort advertShort, Action<string> userNotify = null) => !Int32.TryParse(aPrice, out int aPriceInt)"); return(null); } BodyResult.CutFoward("Treść ogłoszenia"); BodyResult.CutFoward("<div class=\"content\">"); string aDesc = BodyResult.ToString(0, BodyResult.IndexOf("</div>", 0, true)).Replace("<br />", "\n\n").StripHTML(); aDesc = aDesc.Replace("\n\n\n", "\n").Trim(); var additionalData = new Dictionary <string, string>(); if (BodyResult.IndexOf("<div>Dane dodatkowe</div>", 0, true) != -1) { BodyResult.CutFoward("<div>Dane dodatkowe</div>"); StringBuilder addData = new StringBuilder(BodyResult.ToString(0, BodyResult.IndexOf("ogloszeniebox-bottom", 0, true))); do { int posLab = addData.IndexOf("<div class=\"label\">", 0, true); if (posLab == -1) { break; } addData.CutFoward("<div class=\"label\">"); string key = addData.ToString(0, addData.IndexOf("</div>", 0, true)); addData.CutFoward("</div>"); addData.CutFoward("\">"); string value = addData.ToString(0, addData.IndexOf("</div>", 0, true)); additionalData.Add(key, value); } while (true); } var pictureList = new List <string>(); if (BodyResult.IndexOf("<div>Zdjęcia</div>", 0, true) != -1) { BodyResult.CutFoward("<div>Zdjęcia</div>"); StringBuilder addData = new StringBuilder(BodyResult.ToString(0, BodyResult.IndexOf("ogloszeniebox-bottom", 0, true))); do { int posLab = addData.IndexOf("<img src=\"/", 0, true); if (posLab == -1) { break; } addData.CutFoward("<img src=\"/"); var picUrl = RZESZOWIAK_BASE_URL + addData.ToString(0, addData.IndexOf("\"", 0, true)).Replace("mini/o_", ""); pictureList.Add(picUrl); } while (true); } // image sid string aSsid = String.Empty; if (BodyResult.IndexOf("rel=\"" + advertShort.AdverIDinRzeszowiak + "|", 0, true) != -1) { BodyResult.CutFoward("rel=\"" + advertShort.AdverIDinRzeszowiak + "|"); aSsid = BodyResult.ToString(0, BodyResult.IndexOf("\"", 0, true)).Trim(); } Cookie aCookie = null; foreach (var item in HttpResult.CookieList) { if (item.Name == "PHPSESSID") { aCookie = item; } } //email token string aEmailToken = String.Empty; if (BodyResult.IndexOf("id=\"tokenFK\" value=\"", 0, true) != -1) { BodyResult.CutFoward("id=\"tokenFK\" value=\""); aEmailToken = BodyResult.ToString(0, BodyResult.IndexOf("\"", 0, true)).Trim(); } return(new Advert() { AdverIDinRzeszowiak = advertShort.AdverIDinRzeszowiak, Category = null, Title = aTitle, DateAddDateTime = advertShort.DateAddDateTime, ExpiredDatetime = aDateExpired, Views = aViewsInt, Price = aPriceInt, Highlighted = advertShort.Highlighted, DescriptionShort = advertShort.DescriptionShort, DescriptionHTML = aDesc, AdditionalData = additionalData, ImageURLsList = pictureList, URLPath = advertShort.URLPath, PhoneSsid = aSsid, PhonePHPSSESION = aCookie, EmailToken = aEmailToken, VisitPageDate = DateTime.Now, ThumbnailUrl = advertShort.ThumbnailUrl, }); }