Exemplo n.º 1
0
        public static Postcode Parse(string postcode)
        {
            // This method is not so interested in the validity of the postcode,
            // it is more so here to split the postcode into the start and end
            // part for matching purposes.

            postcode = postcode.Trim().Replace(" ", "");

            if (postcode.Length < 2 || postcode.Length > 7)
            {
                throw new ArgumentException("Postcode is not valid");
            }

            var parsedPostcode = new Postcode();

            // First part only, AN ie M1
            if (postcode.Length == 2)
            {
                parsedPostcode.FirstPart = postcode;
                return(parsedPostcode);
            }

            parsedPostcode.FirstPart = postcode.Substring(0, postcode.Length - 3);
            if (parsedPostcode.FirstPart.Length < 2)
            {
                parsedPostcode.FirstPart = postcode;
            }
            else
            {
                parsedPostcode.LastPart = postcode.Substring(parsedPostcode.FirstPart.Length);
            }

            if (parsedPostcode.LastPart != null && parsedPostcode.LastPart.Length != 3)
            {
                throw new ArgumentException("Postcode is not valid");
            }

            return(parsedPostcode);
        }
Exemplo n.º 2
0
        private bool PropertyMatchesShortlist(RightmovePropertyListing rightmovePropertyListing, bool fullTest = true)
        {
            // The property might be excluded more than once if it is a featured property so we
            // need to account for this, hence why we don't use the _removedProperties.Add()
            // method and instead use the indexer

            if (_shortlist.MinimumPrice > 0 && rightmovePropertyListing.Price > 0 && rightmovePropertyListing.Price < _shortlist.MinimumPrice)
            {
                if (OptionsForm.DebugingEnabled)
                {
                    _removedProperties[rightmovePropertyListing.Link] = "Price too low: " + rightmovePropertyListing.PriceQualifier + " " + rightmovePropertyListing.Price;
                }
                return(false);
            }

            if (_shortlist.MaximumPrice > 0 && rightmovePropertyListing.Price > 0 && rightmovePropertyListing.Price > _shortlist.MaximumPrice)
            {
                if (OptionsForm.DebugingEnabled)
                {
                    _removedProperties[rightmovePropertyListing.Link] = "Price too high: " + rightmovePropertyListing.PriceQualifier + " " + rightmovePropertyListing.Price;
                }
                return(false);
            }

            if (_shortlist.MaximumPrice > 0 && rightmovePropertyListing.Price > 0 && rightmovePropertyListing.Price == _shortlist.MaximumPrice && rightmovePropertyListing.PriceQualifier == "Offers in Excess of")
            {
                if (OptionsForm.DebugingEnabled)
                {
                    _removedProperties[rightmovePropertyListing.Link] = "Price too high: Offers in Excess of " + rightmovePropertyListing.PriceQualifier + " " + rightmovePropertyListing.Price;
                }
                return(false);
            }

            if (_shortlist.ExcludePriceOnApplication && rightmovePropertyListing.PriceQualifier == "POA")
            {
                if (OptionsForm.DebugingEnabled)
                {
                    _removedProperties[rightmovePropertyListing.Link] = "POA excluded.";
                }
                return(false);
            }

            if (_shortlist.ExcludeOffersInvited && rightmovePropertyListing.PriceQualifier == "Offers Invited")
            {
                if (OptionsForm.DebugingEnabled)
                {
                    _removedProperties[rightmovePropertyListing.Link] = "Offers Invited excluded.";
                }
                return(false);
            }

            if (_shortlist.ExcludeTerms.Count() > 0)
            {
                string excludedTermFound = null;
                foreach (string term in _shortlist.ExcludeTerms)
                {
                    var regex = new Regex(@"\b" + term + @"\b", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    if (regex.IsMatch(rightmovePropertyListing.Title))
                    {
                        excludedTermFound = term;
                        break;
                    }
                    else if (regex.IsMatch(rightmovePropertyListing.Description))
                    {
                        excludedTermFound = term;
                        break;
                    }
                    else if (!string.IsNullOrEmpty(rightmovePropertyListing.PriceQualifier) && regex.IsMatch(rightmovePropertyListing.PriceQualifier))
                    {
                        excludedTermFound = term;
                        break;
                    }
                }
                if (!string.IsNullOrEmpty(excludedTermFound))
                {
                    if (OptionsForm.DebugingEnabled)
                    {
                        _removedProperties[rightmovePropertyListing.Link] = "Excluded term found: " + excludedTermFound;
                    }
                    return(false);
                }
            }

            if (fullTest)
            {
                // We only test for terms that must be included in a full test
                // as the search listings page doesn't include the full
                // description so we could potentially filter out matching
                // properties if we were to do this prior to having the full
                // description of the property.

                if (_shortlist.ExcludeTerms.Count() > 0)
                {
                    bool requiredTermFound = false;
                    foreach (string term in _shortlist.IncludeTerms)
                    {
                        var regex = new Regex(@"\b" + term + @"\b", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                        if (regex.IsMatch(rightmovePropertyListing.Title))
                        {
                            requiredTermFound = true;
                            break;
                        }
                        else if (regex.IsMatch(rightmovePropertyListing.Description))
                        {
                            requiredTermFound = true;
                            break;
                        }
                        else if (!string.IsNullOrEmpty(rightmovePropertyListing.PriceQualifier) && regex.IsMatch(rightmovePropertyListing.PriceQualifier))
                        {
                            requiredTermFound = true;
                            break;
                        }
                    }
                    if (!requiredTermFound)
                    {
                        if (OptionsForm.DebugingEnabled)
                        {
                            _removedProperties[rightmovePropertyListing.Link] = "One or more required terms were not found.";
                        }
                        return(false);
                    }
                }

                // Annoyingly, not all properties on Rightmove list when they were added so any with a DateTime.MinValue
                // are properties that were added on an unknown date.

                if (_shortlist.ExcludePostcodes.Count() > 0)
                {
                    var postcodeRegex = new Regex("propertyPostcode: \"([^\"]+)\",", RegexOptions.Compiled);
                    var postcodeMatch = postcodeRegex.Match(rightmovePropertyListing.Html);
                    if (postcodeMatch.Success)
                    {
                        var postcode = Postcode.Parse(postcodeMatch.Groups[1].Value);
                        foreach (string excludePostcode in _shortlist.ExcludePostcodes)
                        {
                            var excludedPostcode = Postcode.Parse(excludePostcode);
                            if (excludedPostcode.IsPartcialMatch(postcode))
                            {
                                return(false);
                            }
                        }
                    }
                }

                //TODO: finish filtering the listings....
                // AddedAfter, etc.
            }

            return(true);
        }