Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,26 @@ for listing in listings:
print()
```

Search for apartments for rent in Dublin with a custom User Agent.

```python
from daftlistings import Daft, Location, SearchType, PropertyType

daft = Daft()
daft.set_headers({"User-Agent": "CustomUserAgent/1.0"})
daft.set_location(Location.DUBLIN)
daft.set_search_type(SearchType.RESIDENTIAL_RENT)
daft.set_property_type(PropertyType.APARTMENT)

listings = daft.search()

for listing in listings:
print(listing.title)
print(listing.price)
print(listing.daft_link)
print()
```

## Running Tests

The Python unittest module contains its own test discovery function, which you can run from the command line:
Expand Down
23 changes: 15 additions & 8 deletions daftlistings/daft.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@

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

Expand All @@ -29,6 +23,19 @@ def __init__(self):
self._geoFilter = dict()
self._sort_filter = dict()
self._total_results = 0
self._headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36",
"Content-Type": "application/json",
"brand": "daft",
"platform": "web",
}

def set_headers(self, headers: Dict[str, str]):
"""
Merge or replace headers.
Example: d.set_headers({"User-Agent": "UserAgent/1.0", "X-Example": "Example"})
"""
self._headers.update(headers)

@property
def total_results(self):
Expand Down Expand Up @@ -257,15 +264,15 @@ 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._headers, 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._headers, 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
Expand Down
Loading