Skip to content

Commit

Permalink
Lint Test
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyBloomer committed May 2, 2023
1 parent 3dd6ca9 commit 09a9ba8
Show file tree
Hide file tree
Showing 6 changed files with 20,809 additions and 4,241 deletions.
124 changes: 63 additions & 61 deletions daftlistings/daft.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@

class Daft:
_ENDPOINT = "https://search-gateway.dsch.ie/v1/listings"
_HEADER = {
"Content-Type": "application/json",
"brand": "daft",
"platform": "web"
}
_HEADER = {"Content-Type": "application/json", "brand": "daft", "platform": "web"}
_PAGE_SZ = 50
_PAGE_0 = {"from": "0", "pagesize": str(_PAGE_SZ)}

Expand All @@ -39,19 +35,15 @@ def _set_range_to(self, name: str, to: str):
if r["name"] == name:
r["to"] = to
return
self._ranges.append({"name": name,
"from": "0",
"to": to})
self._ranges.append({"name": name, "from": "0", "to": to})

def _set_range_from(self, name: str, _from: str):
if self._ranges:
for r in self._ranges:
if r["name"] == name:
r["from"] = _from
return
self._ranges.append({"name": name,
"from": _from,
"to": str(10e8)})
self._ranges.append({"name": name, "from": _from, "to": str(10e8)})

def _add_filter(self, name: str, value: Union[str, bool]):
if self._filters:
Expand All @@ -60,8 +52,7 @@ def _add_filter(self, name: str, value: Union[str, bool]):
if value not in f["values"]:
f["values"].append(value)
return
self._filters.append({"name": name,
"values": [value]})
self._filters.append({"name": name, "values": [value]})

def _add_and_filter(self, name: str, value: str):
if self._andFilters:
Expand All @@ -70,8 +61,7 @@ def _add_and_filter(self, name: str, value: str):
if value not in f["values"]:
f["values"].append(value)
return
self._andFilters.append({"name": name,
"values": [value]})
self._andFilters.append({"name": name, "values": [value]})

def _add_sort_filter(self, sort_filter: str):
self._sort_filter = sort_filter
Expand All @@ -82,8 +72,7 @@ def _add_geo_filter(self, id: str):
if id not in ids:
self._geoFilter["storedShapeIds"].append(id)
return
self._geoFilter = {"storedShapeIds": [id],
"geoSearchType": "STORED_SHAPES"}
self._geoFilter = {"storedShapeIds": [id], "geoSearchType": "STORED_SHAPES"}

def set_search_type(self, search_type: SearchType):
if not isinstance(search_type, SearchType):
Expand All @@ -110,21 +99,25 @@ def set_max_baths(self, max_baths: int):
def set_min_price(self, min_price: int):
if not self._section:
raise ValueError("Must set search_type before price.")
if self._section in (SearchType.RESIDENTIAL_RENT.value,
SearchType.COMMERCIAL_RENT.value,
SearchType.SHARING.value,
SearchType.STUDENT_ACCOMMODATION.value):
if self._section in (
SearchType.RESIDENTIAL_RENT.value,
SearchType.COMMERCIAL_RENT.value,
SearchType.SHARING.value,
SearchType.STUDENT_ACCOMMODATION.value,
):
self._set_range_from("rentalPrice", str(min_price))
else:
self._set_range_from("salePrice", str(min_price))

def set_max_price(self, max_price: int):
if not self._section:
raise ValueError("Must set search_type before price.")
if self._section in (SearchType.RESIDENTIAL_RENT.value,
SearchType.COMMERCIAL_RENT.value,
SearchType.SHARING.value,
SearchType.STUDENT_ACCOMMODATION.value):
if self._section in (
SearchType.RESIDENTIAL_RENT.value,
SearchType.COMMERCIAL_RENT.value,
SearchType.SHARING.value,
SearchType.STUDENT_ACCOMMODATION.value,
):
self._set_range_to("rentalPrice", str(max_price))
else:
self._set_range_to("salePrice", str(max_price))
Expand Down Expand Up @@ -173,7 +166,11 @@ def set_max_ber(self, ber: Ber):
raise TypeError("Argument must be enums.Ber.")
self._set_range_to("ber", str(ber.value))

def set_location(self, location: Union[Location, str, List[Union[Location, str]]], distance: Distance = Distance.KM0):
def set_location(
self,
location: Union[Location, str, List[Union[Location, str]]],
distance: Distance = Distance.KM0,
):
if isinstance(location, Location):
self._add_geo_filter(location.value["id"] + distance.value)
elif isinstance(location, str):
Expand All @@ -187,25 +184,34 @@ def set_location(self, location: Union[Location, str, List[Union[Location, str]]
best_match = self._get_best_match(area)
self._add_geo_filter(best_match.value["id"] + distance.value)
else:
raise TypeError("List values must be of type location.Location or string.")
raise TypeError(
"List values must be of type location.Location or string."
)

else:
raise TypeError("Argument must be location.Location, list, or string.")

def set_facility(self, facility: Facility):
if self._section == None:
raise ValueError('SearchType must be set before Facility')
raise ValueError("SearchType must be set before Facility")
else:
if isinstance(facility, Facility):
if self._section in [s.value for s in facility.valid_types]:
self._add_and_filter('facilities', facility.value)
self._add_and_filter("facilities", facility.value)
else:
search_type = [(name,member) for name, member in SearchType.__members__.items() if member.value == self._section][0]
compatible_facilities = [f.name for f in Facility if search_type[1] in f.valid_types]
raise ValueError(f"Facility {facility.name} incompatible with SearchType {search_type[0]}\nThe following facilities are compatible with this SearchType:\n{compatible_facilities}")
search_type = [
(name, member)
for name, member in SearchType.__members__.items()
if member.value == self._section
][0]
compatible_facilities = [
f.name for f in Facility if search_type[1] in f.valid_types
]
raise ValueError(
f"Facility {facility.name} incompatible with SearchType {search_type[0]}\nThe following facilities are compatible with this SearchType:\n{compatible_facilities}"
)
else:
raise TypeError("Argument must be of type Facility")


def set_sort_type(self, sort_type: SortType):
if isinstance(sort_type, SortType):
Expand All @@ -219,9 +225,9 @@ def _get_best_match(location: str) -> Location:
search_term = regex.sub(" ", location)
best_score, best_match = 0, None
for loc in Location:
sm = SequenceMatcher(None,
search_term,
regex.sub(" ", loc.value['displayValue']))
sm = SequenceMatcher(
None, search_term, regex.sub(" ", loc.value["displayValue"])
)
if sm.ratio() > best_score:
best_score, best_match = sm.ratio(), loc
return best_match
Expand All @@ -246,52 +252,48 @@ def _make_payload(self) -> Dict:
def search(self, max_pages: Optional[int] = None) -> List[Listing]:
print("Searching...")
_payload = self._make_payload()
r = requests.post(self._ENDPOINT,
headers=self._HEADER,
json=_payload)
r = requests.post(self._ENDPOINT, headers=self._HEADER, json=_payload)
listings = r.json()["listings"]
results_count = r.json()["paging"]["totalResults"]
total_pages = ceil(results_count / self._PAGE_SZ)
limit = min(max_pages, total_pages) if max_pages else total_pages

for page in range(1, limit):
_payload["paging"]["from"] = page * self._PAGE_SZ
r = requests.post(self._ENDPOINT,
headers=self._HEADER,
json=_payload)
r = requests.post(self._ENDPOINT, headers=self._HEADER, json=_payload)
listings = listings + r.json()["listings"]

# expand out grouped listings as individual listings, commercial searches do not give the necessary information to do this

expanded_listings = []
for l in listings:
# the information contained in the key 'prs' for most searches is instead contained in 'newHome' for newHome type searches
if 'newHome' in l['listing'].keys():
if 'subUnits' in l['listing']['newHome'].keys():
l['listing']['prs'] = l['listing'].pop('newHome')
if "newHome" in l["listing"].keys():
if "subUnits" in l["listing"]["newHome"].keys():
l["listing"]["prs"] = l["listing"].pop("newHome")
try:
num_subUnits = len(l['listing']['prs']['subUnits'])
num_subUnits = len(l["listing"]["prs"]["subUnits"])
for i in range(num_subUnits):
copy = deepcopy(l)
for key in copy['listing']['prs']['subUnits'][i].keys():
copy['listing'][key] = copy['listing']['prs']['subUnits'][i][key]

# studios do not have a 'numBedrooms' so set it separately
if copy['listing']['propertyType'] == 'Studio':
copy['listing']['numBedrooms'] = '1 bed'
copy = deepcopy(l)
for key in copy["listing"]["prs"]["subUnits"][i].keys():
copy["listing"][key] = copy["listing"]["prs"]["subUnits"][i][
key
]

# studios do not have a 'numBedrooms' so set it separately
if copy["listing"]["propertyType"] == "Studio":
copy["listing"]["numBedrooms"] = "1 bed"
expanded_listings.append(copy)
except:
except:
# above only sets studio 'numBedrooms' for grouped listings, do ungrouped here
if 'propertyType' in l['listing'].keys():
if l['listing']['propertyType'] == 'Studio':
l['listing']['numBedrooms'] = '1 bed'
expanded_listings.append(l)
if "propertyType" in l["listing"].keys():
if l["listing"]["propertyType"] == "Studio":
l["listing"]["numBedrooms"] = "1 bed"
expanded_listings.append(l)

listings = expanded_listings

self._total_results = len(listings)
print(f"Search complete. Found {self.total_results} listings.")

return [Listing(l) for l in listings]

return [Listing(l) for l in listings]
Loading

0 comments on commit 09a9ba8

Please sign in to comment.