Ejemplo n.º 1
0
        private async Task SaveHomesForSale(HomeSearchResults searchResults)
        {
            // Delete unseen results
            List <HomeForSale> existingHomesForSale = DB.HomesForSale.ToList();

            existingHomesForSale.ForEach(h =>
            {
                if (!searchResults.HomesForSale.Any(hh => hh.AddressFull == h.AddressFull))
                {
                    DB.Entry(h).State = Microsoft.EntityFrameworkCore.EntityState.Deleted;
                }
            });

            // Add Current Results
            searchResults.HomesForSale.ForEach((HomeForSale homeForSale) =>
            {
                HomeForSale existingDBHome = DB.HomesForSale.Where(h => h.AddressFull == homeForSale.AddressFull).FirstOrDefault();
                if (existingDBHome == null)
                {
                    DB.Add(homeForSale);
                }
                else
                {
                    existingDBHome.PriceInDollars = homeForSale.PriceInDollars;
                    existingDBHome.CoverImageURL  = homeForSale.CoverImageURL;
                }
            });

            await DB.SaveChangesAsync();
        }
Ejemplo n.º 2
0
        public async Task <HomeForSale> IgnoreHome(int homeID)
        {
            HomeForSale homeForSale = DB.HomesForSale.Where(h => h.Id == homeID).FirstOrDefault();

            if (homeForSale == null)
            {
                return(null);
            }

            homeForSale.Status = -1;
            await DB.SaveChangesAsync();

            return(homeForSale);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            HomeForSale = await _context.HomeForSale
                          .Include(h => h.Lot).SingleOrDefaultAsync(m => m.HomeForSaleId == id);

            if (HomeForSale == null)
            {
                return(NotFound());
            }
            return(Page());
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            HomeForSale = await _context.HomeForSale.FindAsync(id);

            if (HomeForSale != null)
            {
                _context.HomeForSale.Remove(HomeForSale);
                await _context.SaveChangesAsync();
            }

            return(RedirectToPage("./Index"));
        }
Ejemplo n.º 5
0
        public async Task <string> IgnoreHomeAndZip(int homeID)
        {
            HomeForSale homeForSale = DB.HomesForSale.Where(h => h.Id == homeID).FirstOrDefault();

            if (homeForSale == null)
            {
                return(null);
            }

            IgnoredZip existingIgnoreRule = DB.IgnoredZips.Where(z => z.ZipCode == homeForSale.ZipCode).FirstOrDefault();

            if (existingIgnoreRule != null)
            {
                return(null);
            }

            await DB.IgnoredZips.AddAsync(new IgnoredZip { ZipCode = homeForSale.ZipCode });

            await DB.SaveChangesAsync();

            return(homeForSale.ZipCode);
        }
Ejemplo n.º 6
0
        private static HomeForSale ProcessHomeNode(INode homeNode)
        {
            // init home and get broker
            HomeForSale home = new HomeForSale
            {
                Broker          = homeNode.ChildNodes.QuerySelectorAll("span[data-label=\"pc-brokered\"]").First()?.TextContent,
                NewConstruction = homeNode.ChildNodes.QuerySelectorAll("span[data-label=\"pc-new-construction\"]").Length > 0
            };

            // get price in dollars
            var priceElement = homeNode.ChildNodes.QuerySelectorAll("span[data-label=\"pc-price\"]").FirstOrDefault();

            if (priceElement != null)
            {
                Regex  notNumbers     = new Regex(@"[\D]");
                string priceProcessed = notNumbers.Replace(priceElement.TextContent, string.Empty);
                if (int.TryParse(priceProcessed, out int priceInt))
                {
                    home.PriceInDollars = priceInt;
                }
            }

            // get beds & baths count
            home.BedCount  = GetBedBathCount(homeNode, "pc-meta-beds");
            home.BathCount = GetBedBathCount(homeNode, "pc-meta-baths");

            var addressNode = homeNode.ChildNodes.QuerySelectorAll("div[data-label=\"pc-address\"]").FirstOrDefault();

            if (addressNode != null)
            {
                home.AddressFull = addressNode.TextContent;
                var addressSections = addressNode.TextContent.Split(',');
                if (addressSections.Length >= 1)
                {
                    home.StreetName = addressSections[0];
                }

                if (addressSections.Length >= 2)
                {
                    home.City = addressSections[1].Trim();
                }

                if (addressSections.Length >= 3)
                {
                    string cityAndZip = addressSections[2].Trim();
                    Regex  notNumbers = new Regex(@"[\D]");
                    home.ZipCode = notNumbers.Replace(cityAndZip, string.Empty);

                    Regex notCapLetters = new Regex(@"[^A-Z]");
                    home.State = notCapLetters.Replace(cityAndZip, string.Empty);
                }
            }

            // get cover image url
            var firstPictureNode = homeNode.ChildNodes.QuerySelectorAll("picture").FirstOrDefault();

            if (firstPictureNode != null)
            {
                var    firstPictureImg = firstPictureNode.QuerySelectorAll("img").FirstOrDefault();
                string imgSource       = firstPictureImg.Attributes.Where(a => a.Name == "data-src").FirstOrDefault()?.Value;
                home.CoverImageURL = imgSource;
            }

            // get detail link
            var aRelOpeners = homeNode.ChildNodes.QuerySelectorAll("a[rel=\"noopener\"]");

            if (aRelOpeners.Length > 0)
            {
                var firstLink = aRelOpeners.First();
                if (firstLink.HasAttribute("href"))
                {
                    home.DetailPageURL = firstLink.Attributes["href"].Value;
                    if (!home.DetailPageURL.StartsWith("https://realtor"))
                    {
                        home.DetailPageURL = $"https://realtor.com{home.DetailPageURL}";
                    }
                }
            }

            return(home);
        }