File size: 9,367 Bytes
83e8cee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
from __future__ import annotations

import json
import os
import re
import urllib.parse
from typing import Any, Dict, Optional

import requests
from smolagents.tools import Tool


SCRAPE_DO_SIGNUP_URL = (
    "https://dashboard.scrape.do/sign-up?utm_source=huggingface&utm_medium=amazon-scraper"
)
API_BASE = "https://api.scrape.do/plugin/amazon"

# geocode -> human label, shown in the UI dropdown.
MARKETPLACES: Dict[str, str] = {
    "us": "United States (amazon.com)",
    "gb": "United Kingdom (amazon.co.uk)",
    "de": "Germany (amazon.de)",
    "fr": "France (amazon.fr)",
    "it": "Italy (amazon.it)",
    "es": "Spain (amazon.es)",
    "ca": "Canada (amazon.ca)",
    "jp": "Japan (amazon.co.jp)",
    "in": "India (amazon.in)",
    "au": "Australia (amazon.com.au)",
    "br": "Brazil (amazon.com.br)",
    "mx": "Mexico (amazon.com.mx)",
    "nl": "Netherlands (amazon.nl)",
    "pl": "Poland (amazon.pl)",
    "se": "Sweden (amazon.se)",
    "tr": "Turkey (amazon.com.tr)",
    "ae": "UAE (amazon.ae)",
    "sa": "Saudi Arabia (amazon.sa)",
    "sg": "Singapore (amazon.sg)",
    "eg": "Egypt (amazon.eg)",
    "be": "Belgium (amazon.com.be)",
}

# Marketplaces where Amazon localises delivery by postal code. Everything else
# takes a country-level location instead, via countryName.
ZIP_LEVEL_MARKETS = {"us", "gb", "uk", "de", "fr", "it", "es", "ca", "jp", "in"}

COUNTRY_NAMES: Dict[str, str] = {
    "tr": "Turkey",
    "br": "Brazil",
    "au": "Australia",
    "mx": "Mexico",
    "nl": "Netherlands",
    "pl": "Poland",
    "se": "Sweden",
    "ae": "United Arab Emirates",
    "sa": "Saudi Arabia",
    "sg": "Singapore",
    "eg": "Egypt",
    "be": "Belgium",
}

_ASIN_PATTERN = re.compile(r"(?:/dp/|/gp/product/|/gp/aw/d/|/product/|[?&]asin=)([A-Z0-9]{10})", re.I)
_BARE_ASIN = re.compile(r"^[A-Z0-9]{10}$", re.I)


def extract_asin(value: str) -> Optional[str]:
    """Accept a full Amazon URL or a bare ASIN and return the ASIN."""
    if not value:
        return None
    value = value.strip()

    if _BARE_ASIN.match(value):
        return value.upper()

    match = _ASIN_PATTERN.search(value)
    if match:
        return match.group(1).upper()

    return None


class ScrapeDoAmazonTool(Tool):
    name = "amazon_product_scraper"
    description = """
    Scrape structured product data from any Amazon marketplace: product detail
    pages (title, brand, price, rating, images, rankings, technical specs),
    all seller offers for an ASIN, or keyword search results with organic and
    sponsored positions. Returns clean JSON, no HTML parsing required.
    Powered by Scrape.do's Amazon Scraper API, which handles AWS WAF, proxy
    rotation, sessions and CAPTCHAs automatically across 21 marketplaces.
    Use this when you need Amazon pricing, catalogue or competitive data.
    """
    output_type = "string"

    def __init__(self, api_token: Optional[str] = None) -> None:
        # Token can be passed explicitly (e.g. by a Gradio UI) or read from the
        # SCRAPE_DO_API_TOKEN environment variable for programmatic use.
        self._api_token = api_token
        self.inputs = {
            "mode": {
                "type": "string",
                "description": (
                    "What to scrape: 'product' for a product detail page, "
                    "'offers' for every seller offer on an ASIN, or 'search' "
                    "for keyword/category search results."
                ),
            },
            "query": {
                "type": "string",
                "description": (
                    "An Amazon product URL or ASIN for 'product' and 'offers'. "
                    "A search keyword for 'search'."
                ),
            },
            "geocode": {
                "type": "string",
                "description": (
                    "Marketplace country code: 'us', 'gb', 'de', 'jp', 'tr'… "
                    "Defaults to 'us'."
                ),
                "nullable": True,
            },
            "zipcode": {
                "type": "string",
                "description": (
                    "Postal code for ZIP-level marketplaces (us, gb, de, fr, it, "
                    "es, ca, jp, in) to localise price, stock and delivery. "
                    "Ignored elsewhere, where a country-level location is used."
                ),
                "nullable": True,
            },
            "page": {
                "type": "integer",
                "description": "Search results page number. Only used when mode='search'.",
                "nullable": True,
            },
            "language": {
                "type": "string",
                "description": (
                    "ISO 639-1 language code in uppercase, e.g. 'EN', 'DE', 'FR'. "
                    "Must be supported by the target marketplace."
                ),
                "nullable": True,
            },
        }
        super().__init__()

    # ------------------------------------------------------------------ token

    def _resolve_token(self) -> Optional[str]:
        if self._api_token:
            return self._api_token.strip()
        env_token = os.getenv("SCRAPE_DO_API_TOKEN")
        return env_token.strip() if env_token else None

    # ---------------------------------------------------------------- request

    def request(
        self,
        mode: str,
        query: str,
        geocode: str = "us",
        zipcode: Optional[str] = None,
        page: Optional[int] = None,
        language: Optional[str] = None,
    ) -> Dict[str, Any]:
        """Call the Amazon Scraper API and return a parsed dict."""
        token = self._resolve_token()
        if not token:
            return {
                "status": "error",
                "errorMessage": "Missing Scrape.do API token.",
                "hint": f"Get a free token at {SCRAPE_DO_SIGNUP_URL}",
            }

        if not query or not query.strip():
            return {"status": "error", "errorMessage": "No ASIN, URL or keyword provided."}

        geocode = (geocode or "us").strip().lower()
        params: Dict[str, Any] = {"token": token, "geocode": geocode}

        if mode in ("product", "offers"):
            asin = extract_asin(query)
            if not asin:
                return {
                    "status": "error",
                    "errorMessage": (
                        "Could not read an ASIN from that input. Paste a full "
                        "Amazon product URL (…/dp/B0BLRJ4R8F) or the 10-character "
                        "ASIN on its own."
                    ),
                }
            params["asin"] = asin
            endpoint = f"{API_BASE}/pdp" if mode == "product" else f"{API_BASE}/offer-listing"
        elif mode == "search":
            params["keyword"] = query.strip()
            params["page"] = int(page or 1)
            endpoint = f"{API_BASE}/search"
        else:
            return {"status": "error", "errorMessage": f"Unknown mode: {mode}"}

        # Location: ZIP for ZIP-level marketplaces, country name for the rest.
        if geocode in ZIP_LEVEL_MARKETS:
            if zipcode and zipcode.strip():
                params["zipcode"] = zipcode.strip()
        elif geocode in COUNTRY_NAMES:
            # Passing the marketplace's own country is a no-op, so only send it
            # when the caller explicitly asked for a different delivery country.
            if zipcode and zipcode.strip():
                params["countryName"] = zipcode.strip()

        if language and language.strip():
            params["language"] = language.strip().upper()

        url = endpoint + "?" + urllib.parse.urlencode(params, quote_via=urllib.parse.quote)

        try:
            response = requests.get(url, timeout=90)
        except requests.exceptions.RequestException as exc:
            return {"status": "error", "errorMessage": f"Request failed: {exc}"}

        try:
            data = response.json()
        except ValueError:
            return {
                "status": "error",
                "errorMessage": f"Unexpected non-JSON response (HTTP {response.status_code}).",
                "details": response.text[:500],
            }

        if response.status_code == 401 or "token" in str(data.get("error", "")).lower():
            data.setdefault(
                "hint", f"Check your token or create one at {SCRAPE_DO_SIGNUP_URL}"
            )

        if isinstance(data, dict) and "status" not in data:
            data["status"] = "success" if response.ok else "error"
        if isinstance(data, dict) and not response.ok and not data.get("errorMessage"):
            data["errorMessage"] = data.get("message") or f"HTTP {response.status_code}"

        return data

    # ---------------------------------------------------------------- forward

    def forward(
        self,
        mode: str,
        query: str,
        geocode: Optional[str] = None,
        zipcode: Optional[str] = None,
        page: Optional[int] = None,
        language: Optional[str] = None,
    ) -> str:
        result = self.request(
            mode=mode,
            query=query,
            geocode=geocode or "us",
            zipcode=zipcode,
            page=page,
            language=language,
        )
        return json.dumps(result, ensure_ascii=False, indent=2)