Scraper Source: dm_scraper

Read-only source view.

dm_scraper.py · 10625 bytes
  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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
from __future__ import annotations

import logging
import threading
import time
from typing import Any, List, Dict, Optional, Mapping, Set, Tuple, Callable
from scrapers.base_scraper import BaseScraper, ScrapeRunResult
from scrapers.records import calculate_discount, parse_package, parse_price
from scrapers.settings_schema import ScraperSetting
from scrapers.common import PRODUCT_FIELDNAMES, ProxyPool, ResponseCache, ThreadLocalSessionFactory
from scrapers.exceptions import StructuralError, ScraperAbortError
from scrapers.options import RuntimeOptions

logger = logging.getLogger(__name__)

# Top-level category IDs discovered from live site (2026-05).
# Each search query is capped at 1 000 accessible products by the API.
# Using multiple category buckets maximises unique product coverage.
CATEGORY_IDS = ["020000", "030000", "040000", "050000", "060000", "070000"]
SEARCH_URL = "https://product-search.services.dmtech.com/hu/search/static"
BASE_URL = "https://www.dm.hu"


def _parse_packaging(name: str) -> tuple[str, str]:
    return parse_package(name)


def _parse_price(tile: dict) -> tuple[str, str, str]:
    tracking = tile.get("trackingData", {})
    price_block = tile.get("price", {}).get("price", {})
    current = price_block.get("current", {})
    previous = price_block.get("previous", {})

    price, _ = parse_price(current.get("value", tracking.get("price")))
    old_price, _ = parse_price(previous.get("value", ""))
    return price, old_price, calculate_discount(price, old_price)


class Scraper(BaseScraper):
    execution_mode = "api"
    settings_schema = BaseScraper.settings_schema + (
        ScraperSetting(
            name="page_size",
            type="int",
            default=100,
            label="Page Size",
            description="Number of products per API page request. Higher values reduce round-trips but may trigger rate limits.",
            min=1,
            max=500,
        ),
        ScraperSetting(
            name="user_agent",
            type="str",
            default="",
            label="User Agent",
            description="Override the HTTP User-Agent header. Leave blank to use the scraper default.",
        ),
    )
    store_definitions = (
        {
            "retailer_code": "dm-hu",
            "name": "dm Hungary",
            "country_code": "HU",
            "base_url": "https://www.dm.hu",
            "scraper_module": "scrapers.platform.dm_scraper",
            "scraper_mapping": {"execution_mode": "api", "output_schema": "product"},
        },
    )

    def __init__(
        self,
        workers: int = 4,
        proxy_list: str | None = None,
        retries: int = 3,
        timeout: int = 20,
        max_proxy_failures: int = 3,
        page_size: int = 100,
        request_delay: float = 0.5,
    ):
        super().__init__()
        self.workers = workers
        self.retries = retries
        self.timeout = timeout
        self.page_size = page_size
        self.request_delay = request_delay
        self.proxy_validation_url = "https://www.dm.hu/"
        self.headers = {
            "Accept": "application/json",
            "Referer": "https://www.dm.hu/",
            "Origin": "https://www.dm.hu",
        }
        if hasattr(self, "configure_http_transport"):
            self.configure_http_transport(
                headers=self.headers,
                proxy_list=proxy_list,
                max_proxy_failures=max_proxy_failures,
            )
        else:
            self.cache = ResponseCache(ttl=43200)
            self.proxy_pool = ProxyPool(proxy_list, max_failures=max_proxy_failures)
            self.http_proxies_enabled = True
            self.sessions = ThreadLocalSessionFactory(self.headers)
            self._request_rate_lock = threading.Lock()
            self._last_request_at = 0.0
        self.fieldnames = PRODUCT_FIELDNAMES

    def _fetch_page(self, category_id: str, page: int) -> dict | None:
        params = {
            "allCategories.id": category_id,
            "pageSize": self.page_size,
            "currentPage": page,
            "searchType": "editorial-search",
            "sort": "editorial_relevance",
            "type": "search-static",
        }
        self.throttle_requests(self.request_delay)

        data = self.fetch_json_via_backend(
            SEARCH_URL,
            params=params,
            purpose=f"dm {category_id} p{page}",
            retries=self.retries,
            timeout=self.timeout,
            headers=self.headers,
            retry_sleep=lambda attempt: time.sleep(2 ** attempt if attempt == 1 else 1),
        )
        if data is None:
            logger.debug("DM request failed: category=%s page=%d", category_id, page)
            return None

        try:
            if "products" not in data and "totalPages" not in data:
                raise StructuralError(f"DM API response missing core fields: {list(data.keys())}")
            return data
        except (ValueError, KeyError) as exc:
            if isinstance(exc, StructuralError):
                raise
            logger.debug("DM response parse issue: category=%s page=%d err=%s", category_id, page, exc)
            return None

    def parse_product(self, raw: dict) -> dict | None:
        try:
            from scrapers.records import product_record, parse_package, parse_price, calculate_discount

            dan = str(raw.get("dan", ""))
            if not dan:
                return None

            gtin_int = raw.get("gtin")
            ean = f"{gtin_int:013d}" if gtin_int else ""

            brand = raw.get("brandName", "").strip()
            title = raw.get("title", "").strip()
            name = f"{brand} {title}".strip() if brand else title

            tile = raw.get("tileData", {})
            tracking = tile.get("trackingData", {})

            self_path = tile.get("self", "")
            source_url = f"{BASE_URL}{self_path}" if self_path else ""

            images = tile.get("images", [])
            image_url = images[0].get("tileSrc", "") if images else ""

            categories = tracking.get("categories", [])
            category = " > ".join(categories) if categories else ""

            # Use shared price parser
            current_num = tracking.get("price")
            price_val = str(int(current_num)) if current_num is not None else ""
            
            price_block = tile.get("price", {}).get("price", {})
            previous = price_block.get("previous", {})
            old_price_val = ""
            if previous:
                old_price_val, _ = parse_price(previous.get("value", ""))

            discount_percent = calculate_discount(price_val, old_price_val)

            pkg_value, pkg_unit = parse_package(title)

            return product_record(
                source_product_id=dan,
                name=name,
                price=price_val,
                old_price=old_price_val,
                currency="HUF",
                discount_percent=discount_percent,
                image_url=image_url,
                category=category,
                packaging_value=pkg_value,
                packaging_unit=pkg_unit,
                source_url=source_url,
                ean_code=ean,
                availability="1",
                raw=raw,
            )
        except Exception as exc:
            logger.debug("DM product parse skipped: dan=%s err=%s", raw.get("dan"), exc)
            return None

    def scrape_category(self, category_id: str) -> list[dict]:
        products: dict[str, dict] = {}

        first = self._fetch_page(category_id, 0)
        if not first:
            logger.error("Failed to fetch first page of category %s", category_id)
            return []

        total_pages = min(first.get("totalPages", 1), 10)  # API caps at page 999; we cap at 10
        logger.info("Category %s: %s products, %d pages", category_id, first.get("count", "?"), total_pages)

        for raw in first.get("products", []):
            parsed = self.parse_product(raw)
            if parsed and parsed["source_product_id"] not in products:
                products[parsed["source_product_id"]] = parsed

        for page in range(1, total_pages):
            data = self._fetch_page(category_id, page)
            if not data:
                break
            for raw in data.get("products", []):
                parsed = self.parse_product(raw)
                if parsed and parsed["source_product_id"] not in products:
                    products[parsed["source_product_id"]] = parsed

            if (page + 1) % 5 == 0:
                logger.debug("  %s: page %d/%d, %d unique so far", category_id, page + 1, total_pages, len(products))

        return list(products.values())

    def run(self, target: Any = None, context: dict[str, Any] | None = None) -> ScrapeRunResult:
        opts = RuntimeOptions.from_run_args(target, context)
        records = self.collect_products(
            category_ids=opts.extra.get("category_ids"),
            limit_categories=opts.limit_categories,
            category_filter=opts.category_filter,
        )
        return ScrapeRunResult(
            records=records,
            raw_records=records,
            accepted_records=records,
            rejected_records=[],
            telemetry=self.telemetry,
        )

    def collect_products(
        self,
        category_ids: list[str] | None = None,
        limit_categories: int | None = None,
        category_filter: str | None = None,
    ) -> list[dict[str, Any]]:
        cats = category_ids or CATEGORY_IDS
        if category_filter:
            cats = [c for c in cats if category_filter in c]
        if limit_categories:
            cats = cats[:limit_categories]
            
        if not cats:
            raise ScraperAbortError(
                "dm: no categories to scrape after filtering",
                error_category="validation",
            )
            
        self.log_startup_status(logger, f"dm Hungary scraper started — {len(cats)} categories")

        category_results = self.parallel_map(
            self.scrape_category,
            cats,
            label="dm categories",
            flatten=False,
            unit="categories"
        )

        all_by_dan: dict[str, dict] = {}
        for products in category_results:
            for p in products:
                pid = p["source_product_id"]
                if pid not in all_by_dan:
                    all_by_dan[pid] = p

        unique = list(all_by_dan.values())
        self.log_summary(logger, "dm complete")
        return unique


DmScraper = Scraper