Skip to content

Fix and improve Geocaching.search_rect() #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 12, 2023
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
2 changes: 1 addition & 1 deletion pycaching/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ def _from_api_record(cls, geocaching, record):
name=record["name"],
type=Type.from_number(record["geocacheType"]),
status=Status(record["cacheStatus"]),
found=record["userFound"],
found="userFound" in record,
size=Size.from_number(record["containerType"]),
difficulty=record["difficulty"],
terrain=record["terrain"],
Expand Down
16 changes: 12 additions & 4 deletions pycaching/geocaching.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ def search_rect(
*,
per_query: int = 200,
sort_by: Union[str, SortOrder] = SortOrder.date_last_visited,
reverse: bool = False,
limit: int = float("inf"),
origin: Optional[Point] = None,
wait_sleep: bool = True
):
Expand All @@ -400,22 +402,28 @@ def search_rect(
:param rect: Search area.
:param int per_query: Number of caches requested in single query.
:param sort_by: Order cached by given criterion.
:param reverse: Reverse sort order.
:param limit: Maximum number of caches to return.
:param origin: Origin point for search by distance.
:param wait_sleep: In case of rate limits exceeding, wait appropriate time if set True,
otherwise just yield None.
"""
if not isinstance(sort_by, SortOrder):
sort_by = SortOrder(sort_by)

if limit <= 0:
return

take_amount = min(limit, per_query)
params = {
"box": "{},{},{},{}".format(
rect.corners[0].latitude,
rect.corners[0].longitude,
rect.corners[1].latitude,
rect.corners[1].longitude,
),
"take": per_query,
"asc": "true",
"take": take_amount,
"asc": str(not reverse).lower(),
"skip": 0,
"sort": sort_by.value,
}
Expand All @@ -425,7 +433,7 @@ def search_rect(
params["origin"] = "{},{}".format(origin.latitude, origin.longitude)

total, offset = None, 0
while (total is None) or (offset < total):
while (offset < limit) and ((total is None) or (offset < total)):
params["skip"] = offset

try:
Expand All @@ -441,7 +449,7 @@ def search_rect(
yield Cache._from_api_record(self, record)

total = resp["total"]
offset += per_query
offset += take_amount

def geocode(self, location):
"""Return a :class:`.Point` object from geocoded location.
Expand Down
Loading