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);
        }
Ejemplo n.º 2
0
        protected async Task <bool> SearchExecute(AdvertSearch advertSearch, bool addLoad = false)
        {
            Debug.Write("SearchExecute");
            Activity     = true;
            ErrorMessage = String.Empty;

            _lastAdvertSearch = advertSearch;
            OnPropertyChanged("CurrentCategory");
            OnPropertyChanged("FilterButtonColor");

            if (advertSearch == null)
            {
                Debug.Write("SearchExecute => advertSearch == null");
                return(false);
            }

            if (addLoad)
            {
                FotterActivity = true;
                Activity       = false;
            }

            else
            {
                Activity       = true;
                FotterActivity = false;
                AdvertShortList.Clear();
            }

            var lastAddAdvert = await _rzeszowiakRepository.GetAdvertListAsync(advertSearch);

            if (!lastAddAdvert.Correct)
            {
                if (!addLoad)
                {
                    ErrorMessage = "Błąd podczas ładowania strony.\nSprawdź połączenie internetowe i spróbuj ponownie.";
                }
            }
            else
            {
                _lastAdvertSearchResult = lastAddAdvert;

                var adverId = new List <int>();
                foreach (var item in AdvertShortList)
                {
                    adverId.Add(item.AdverIDinRzeszowiak);
                }

                foreach (var item in lastAddAdvert.AdvertShortsList)
                {
                    if (adverId.IndexOf(item.AdverIDinRzeszowiak) != -1)
                    {
                        continue;
                    }
                    AdvertShortList.Add(item);
                    await Task.Delay(50);
                }

                if (AdvertShortList.Count == 0 && !addLoad)
                {
                    ErrorMessage = "Nic nie znaleziono.\nZmień kryteria wyszukiwania.";
                }
            }
            Activity       = false;
            FotterActivity = false;
            return(true);
        }