Wire produce-box switching as a select entity
The boxes are not in the Algolia catalogue and their category pages look empty
because the grid calls every option 'Georgia Box' — each option's real name is
only in its own select-basket popup, so listing reads them there.
POST /s/submit/select-basket carries a scope the add-on endpoints do not:
popup-toggle is 'do' for the next delivery or 'so' for the standing order. The
select uses 'do', because a mistaken permanent change is the worse one to undo.
Also makes the popup regexes whitespace-tolerant. The site writes both
openPopup("x","y") and openPopup("x", "y"), and the strict form silently
matched nothing on the basket pages.
This commit is contained in:
@@ -43,6 +43,16 @@ SUBMIT_RESTORE = "/s/submit/restore-delivery"
|
||||
SUBMIT_DONATE = "/s/submit/donate-basket"
|
||||
SUBMIT_SUBSCRIBE = "/s/submit/item-frequency"
|
||||
SUBMIT_HOLD = "/s/submit/pause-range-add"
|
||||
SUBMIT_BASKET = "/s/submit/select-basket"
|
||||
BASKET_TYPES = "/p/shop/basket-types"
|
||||
BASKET_GROUPS = (
|
||||
"georgia-grown-baskets",
|
||||
"mixed-fruit-and-veggie-baskets",
|
||||
"fruit-basket",
|
||||
)
|
||||
# The two submit buttons set this before posting: "do" = this delivery only,
|
||||
# "so" = the standing order, i.e. every future box.
|
||||
SCOPE_ONCE, SCOPE_STANDING = "do", "so"
|
||||
AJAX_ORDER_MANAGE = "/p/Ajax/order-manage/{mode}/{hash}/-/false/{ts}"
|
||||
|
||||
# id='FrequencyID' but name='popup-toggle' — the id is a decoy, the POST field
|
||||
@@ -206,7 +216,7 @@ class FreshHarvestActions:
|
||||
submit and this raises rather than inventing one.
|
||||
"""
|
||||
page = await self._client.async_fetch(DASHBOARD_ORDERS)
|
||||
tokens = re.findall(r'openPopup\("pause-delivery","([^"]+)"', page)
|
||||
tokens = re.findall(r'openPopup\("pause-delivery",\s*"([^"]+)"', page)
|
||||
if not tokens:
|
||||
raise FreshHarvestActionError("no skippable delivery on this account")
|
||||
|
||||
@@ -267,7 +277,7 @@ class FreshHarvestActions:
|
||||
raises when there is nothing to restore.
|
||||
"""
|
||||
page = await self._client.async_fetch(DASHBOARD_ORDERS)
|
||||
tokens = re.findall(r'openPopup\("restore-delivery","([^"]+)"', page)
|
||||
tokens = re.findall(r'openPopup\("restore-delivery",\s*"([^"]+)"', page)
|
||||
if not tokens:
|
||||
raise FreshHarvestActionError("no skipped delivery to restore")
|
||||
|
||||
@@ -299,7 +309,7 @@ class FreshHarvestActions:
|
||||
async def async_donate(self, dry_run: bool = True) -> ActionResult:
|
||||
"""Donate the upcoming box. One-way — there is no undo in the UI."""
|
||||
page = await self._client.async_fetch(DASHBOARD_ORDERS)
|
||||
m = re.search(r'openPopup\("donate-delivery","([^"]+)"', page)
|
||||
m = re.search(r'openPopup\("donate-delivery",\s*"([^"]+)"', page)
|
||||
if not m:
|
||||
raise FreshHarvestActionError("no donatable delivery")
|
||||
popup = await self._client.async_fetch(
|
||||
@@ -441,3 +451,116 @@ class FreshHarvestActions:
|
||||
|
||||
async def async_list_vacation_holds(self) -> list[VacationHold]:
|
||||
return parse_vacation_holds(await self._client.async_fetch(DASHBOARD_PAUSE))
|
||||
|
||||
|
||||
@dataclass
|
||||
class Basket:
|
||||
"""A produce box you can switch to."""
|
||||
|
||||
item_id: str
|
||||
name: str
|
||||
is_current: bool = False
|
||||
token: str = ""
|
||||
|
||||
|
||||
class BasketMixin:
|
||||
"""Produce-box switching, mixed into FreshHarvestActions below.
|
||||
|
||||
A box is not an add-on: you swap which box arrives, you do not add or
|
||||
remove the produce inside it. The switch also has a scope the add-on
|
||||
endpoints do not — one delivery, or every future one.
|
||||
"""
|
||||
|
||||
async def async_list_baskets(self) -> list[Basket]:
|
||||
"""Every box you could switch TO.
|
||||
|
||||
Each option's real name lives in its own popup rather than the grid
|
||||
(the grid calls them all "Georgia Box"), so this reads them there.
|
||||
|
||||
The box you are already on is deliberately absent: the site offers no
|
||||
"switch to this" control for it, so there is no popup and no name. Its
|
||||
id shows up as every popup's `ReplaceItemID`, which is what
|
||||
`current_basket_id` returns; its NAME comes from the subscription list.
|
||||
"""
|
||||
baskets: list[Basket] = []
|
||||
seen: set[str] = set()
|
||||
for group in BASKET_GROUPS:
|
||||
page = await self._client.async_fetch(f"{BASKET_TYPES}/{group}")
|
||||
tokens = dict.fromkeys(
|
||||
re.findall(r'openPopup\("select-basket",\s*"([^"]+)"', page)
|
||||
)
|
||||
for token in tokens:
|
||||
popup = await self._client.async_fetch(
|
||||
f"/x/popup/select-basket/{token}", is_page=False
|
||||
)
|
||||
soup = BeautifulSoup(popup, "html.parser")
|
||||
form = _find_form(soup, SUBMIT_BASKET)
|
||||
if form is None:
|
||||
continue # a catch-all page, not a real popup
|
||||
fields = _hidden_fields(form)
|
||||
item_id = fields.get("ItemID", "")
|
||||
if not item_id or item_id in seen:
|
||||
continue
|
||||
seen.add(item_id)
|
||||
heading = soup.select_one("h4, h5, h6")
|
||||
baskets.append(
|
||||
Basket(
|
||||
item_id=item_id,
|
||||
name=(heading.get_text(" ", strip=True) if heading else item_id),
|
||||
is_current=False, # see the docstring: never offered
|
||||
token=token,
|
||||
)
|
||||
)
|
||||
return baskets
|
||||
|
||||
async def async_current_basket_id(self) -> str | None:
|
||||
"""The id of the box currently subscribed, read off any switch popup."""
|
||||
for group in BASKET_GROUPS:
|
||||
page = await self._client.async_fetch(f"{BASKET_TYPES}/{group}")
|
||||
for token in dict.fromkeys(
|
||||
re.findall(r'openPopup\("select-basket",\s*"([^"]+)"', page)
|
||||
):
|
||||
popup = await self._client.async_fetch(
|
||||
f"/x/popup/select-basket/{token}", is_page=False
|
||||
)
|
||||
form = _find_form(BeautifulSoup(popup, "html.parser"), SUBMIT_BASKET)
|
||||
if form is not None:
|
||||
return _hidden_fields(form).get("ReplaceItemID")
|
||||
return None
|
||||
|
||||
async def async_change_basket(
|
||||
self, name_or_id: str, all_future: bool = False, dry_run: bool = True
|
||||
) -> ActionResult:
|
||||
"""Switch to a different produce box.
|
||||
|
||||
`all_future=False` changes only the next delivery; True changes the
|
||||
standing order. Defaulting to the one-off is deliberate — a mistaken
|
||||
permanent change is the more annoying of the two to undo.
|
||||
"""
|
||||
wanted = str(name_or_id).strip().lower()
|
||||
for basket in await self.async_list_baskets():
|
||||
if wanted not in (basket.item_id.lower(), basket.name.lower()):
|
||||
continue
|
||||
popup = await self._client.async_fetch(
|
||||
f"/x/popup/select-basket/{basket.token}", is_page=False
|
||||
)
|
||||
form = _find_form(BeautifulSoup(popup, "html.parser"), SUBMIT_BASKET)
|
||||
if form is None:
|
||||
raise FreshHarvestActionError("basket form disappeared mid-flight")
|
||||
payload = _hidden_fields(form)
|
||||
payload["popup-toggle"] = SCOPE_STANDING if all_future else SCOPE_ONCE
|
||||
scope = "all future orders" if all_future else "the next delivery only"
|
||||
result = ActionResult(
|
||||
action="change_basket", ok=True, target=basket.name,
|
||||
submitted=payload, dry_run=dry_run,
|
||||
detail=f"switch to {basket.name} for {scope}",
|
||||
)
|
||||
if not dry_run:
|
||||
await self._post(SUBMIT_BASKET, payload)
|
||||
return result
|
||||
|
||||
raise FreshHarvestActionError(f"no box matches {name_or_id!r}")
|
||||
|
||||
|
||||
class FreshHarvestActions(FreshHarvestActions, BasketMixin): # noqa: F811
|
||||
"""Actions plus basket switching."""
|
||||
|
||||
Reference in New Issue
Block a user