|
| 1 | +"""Reconstruct deep links to an individual pet's adoption page. |
| 2 | +
|
| 3 | +The RescueGroups API gives us an org's *landing* page (e.g. |
| 4 | +``https://sterlingshelter.org/``), not a link to the specific animal. Some orgs |
| 5 | +embed the RescueGroups "web toolkit" (``toolkit.rescuegroups.org/j/3/.../toolkit.js``), |
| 6 | +which renders a single animal when the page URL carries a hash fragment of the |
| 7 | +form:: |
| 8 | +
|
| 9 | + <pet-finder page>#action_0=pet&animalID_0=<animal id> |
| 10 | +
|
| 11 | +``animalID_0`` is the RescueGroups animal id -- the exact value the API returns |
| 12 | +as ``animal["id"]`` and we store as ``AdoptablePet.pet_id`` -- so we can rebuild |
| 13 | +the deep link without scraping. (``petIndex_0`` only drives next/prev nav within |
| 14 | +a result list and is not needed to load a specific animal.) |
| 15 | +
|
| 16 | +We only reconstruct for orgs we have verified use the toolkit; every other org |
| 17 | +falls back to whatever URL the API provided. |
| 18 | +""" |
| 19 | + |
| 20 | +from typing import Iterable |
| 21 | +from urllib.parse import urlparse |
| 22 | + |
| 23 | +# Domain -> (template, id_key). Each org's pet page is reachable from one of the |
| 24 | +# ids we get from the API: |
| 25 | +# * "pet_id" -- the RescueGroups numeric animal id (toolkit shelters). |
| 26 | +# * "rescue_id_lower" -- the shelter's own animal id (RescueGroups "rescueId"), |
| 27 | +# lowercased (MSPCA's /pets/a######/ urls). |
| 28 | +# The template uses a single ``{id}`` placeholder filled with that id. |
| 29 | +# |
| 30 | +# Sterling & SmallDog embed the RescueGroups toolkit v3; the trailing |
| 31 | +# ``petIndex_0=-1`` is the toolkit's "standalone pet, not part of a browsed list" |
| 32 | +# sentinel -- without it the widget can show the full list instead of the animal. |
| 33 | +PET_FINDER_TEMPLATES: dict[str, tuple[str, str]] = { |
| 34 | + "sterlingshelter.org": ( |
| 35 | + "https://sterlingshelter.org/pet-finder/#action_0=pet&animalID_0={id}&petIndex_0=-1", |
| 36 | + "pet_id", |
| 37 | + ), |
| 38 | + "smalldogrescuene.org": ( |
| 39 | + "https://www.smalldogrescuene.org/adoptable-dogs/#action_0=pet&animalID_0={id}&petIndex_0=-1", |
| 40 | + "pet_id", |
| 41 | + ), |
| 42 | + "mspca.org": ( |
| 43 | + "https://www.mspca.org/pets/{id}/", |
| 44 | + "rescue_id_lower", |
| 45 | + ), |
| 46 | +} |
| 47 | + |
| 48 | + |
| 49 | +def _domain_of(url: str | None) -> str | None: |
| 50 | + """Return the lowercased host of ``url`` without a leading ``www.``.""" |
| 51 | + if not url: |
| 52 | + return None |
| 53 | + netloc = urlparse(url.strip()).netloc.lower() |
| 54 | + if not netloc: |
| 55 | + return None |
| 56 | + # Drop any user:pass@ and :port, then a leading www. |
| 57 | + netloc = netloc.rsplit("@", 1)[-1].split(":", 1)[0] |
| 58 | + return netloc[4:] if netloc.startswith("www.") else netloc |
| 59 | + |
| 60 | + |
| 61 | +def _template_for_domain(domain: str | None) -> tuple[str, str] | None: |
| 62 | + if not domain: |
| 63 | + return None |
| 64 | + # Exact match or any subdomain of a known org (e.g. adopt.sterlingshelter.org). |
| 65 | + for known, entry in PET_FINDER_TEMPLATES.items(): |
| 66 | + if domain == known or domain.endswith("." + known): |
| 67 | + return entry |
| 68 | + return None |
| 69 | + |
| 70 | + |
| 71 | +def is_supported_org(url: str | None) -> bool: |
| 72 | + """True if ``url``'s domain is a shelter we have a deep-link template for.""" |
| 73 | + return _template_for_domain(_domain_of(url)) is not None |
| 74 | + |
| 75 | + |
| 76 | +def reconstruct_adoption_url( |
| 77 | + candidate_urls: Iterable[str | None], |
| 78 | + pet_id: str | None, |
| 79 | + rescue_id: str | None = None, |
| 80 | +) -> str | None: |
| 81 | + """Build a deep link to a specific pet, or ``None`` if we can't. |
| 82 | +
|
| 83 | + Args: |
| 84 | + candidate_urls: URLs from the API that might reveal the org's domain |
| 85 | + (adoption URL, org adoption URL, org website, ...). Checked in order; |
| 86 | + the first whose domain matches a known org wins. |
| 87 | + pet_id: The RescueGroups numeric animal id (``AdoptablePet.pet_id``). |
| 88 | + rescue_id: The shelter's own animal id (``AdoptablePet.rescue_id`` / |
| 89 | + RescueGroups "rescueId"), used by orgs like MSPCA. |
| 90 | +
|
| 91 | + Returns: |
| 92 | + A reconstructed deep link, or ``None`` when no candidate domain is known |
| 93 | + or the id that org's template needs is missing. |
| 94 | + """ |
| 95 | + ids = { |
| 96 | + "pet_id": pet_id or None, |
| 97 | + "rescue_id_lower": rescue_id.lower() if rescue_id else None, |
| 98 | + } |
| 99 | + for url in candidate_urls: |
| 100 | + entry = _template_for_domain(_domain_of(url)) |
| 101 | + if not entry: |
| 102 | + continue |
| 103 | + template, id_key = entry |
| 104 | + id_value = ids.get(id_key) |
| 105 | + if id_value: |
| 106 | + return template.format(id=id_value) |
| 107 | + # Domain matched but we lack the id it needs -> fall back (try next url). |
| 108 | + return None |
0 commit comments