Scraper Source: rossmann

Read-only source view.

rossmann.py · 10097 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
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 product_record, parse_package, parse_price, calculate_discount
from scrapers.settings_schema import ScraperSetting
from scrapers.common import PRODUCT_FIELDNAMES, ProxyPool, ThreadLocalSessionFactory
from scrapers.exceptions import StructuralError, ScraperAbortError
from scrapers.options import RuntimeOptions

logger = logging.getLogger(__name__)

# Identified top-level categories from live site
# These slugs are used in the GraphQL query 'product_category_path'
CATEGORY_SLUGS = [
    "akcios-termekek",
    "arcapolas",
    "haj",
    "baba",
    "haztartas",
    "szepsegapolas",
    "dekorkozmetika",
    "egeszseg",
    "parfum",
    "szajapolas",
    "elelmiszer",
    "erotika",
    "allat",
    "oltozkodes",
]

GRAPHQL_URL = "https://api.rossmann.hu/graphql"
BASE_URL = "https://shop.rossmann.hu"

LIST_PRODUCTS_QUERY = """
query listProductsByCategory($product_category_path: String, $filters: [ProductFilter!], $aggregations: [ProductListFilterField!], $sortBy: ProductListSort, $first: Int!, $page: Int) {
  listProductsByCategory(
    product_category_path: $product_category_path
    filters: $filters
    aggregations: $aggregations
    sortBy: $sortBy
    first: $first
    page: $page
  ) {
    paginatorInfo {
      ...PaginatorInfo
    }
    data {
      ...ProductSlim
    }
  }
}

fragment PaginatorInfo on PaginatorInfo {
  count
  currentPage
  firstItem
  hasMorePages
  lastItem
  lastPage
  perPage
  total
}

fragment ProductSlim on Product {
  id
  lfdnr
  slug
  name
  ean
  category_path_main {
    id
    slug
    name
  }
  brand {
    id
    name
  }
  in_stock
  images {
    name
    url_list
  }
  price
  price_original
  price_original_unit
  price_unit
  unit_base
  magento_id
  is_adult
  children {
    id
  }
  price_rplus
  price_rplus_unit
  price_rossmano
  price_rossmano_unit
  deposit_fee
  cart_qty_max
  margin_stop
  promotion_id
}
"""

class Scraper(BaseScraper):
    execution_mode = "api"
    settings_schema = BaseScraper.settings_schema + (
        ScraperSetting(
            name="page_size",
            type="int",
            default=24,
            label="Page Size",
            description="Number of products per API page request.",
            min=1,
            max=100,
        ),
    )
    store_definitions = (
        {
            "retailer_code": "rossmann-hu",
            "name": "Rossmann Hungary",
            "country_code": "HU",
            "base_url": "https://shop.rossmann.hu",
            "scraper_module": "scrapers.platform.rossmann",
            "scraper_mapping": {"execution_mode": "api", "output_schema": "product"},
        },
    )

    def __init__(
        self,
        workers: int = 4,
        proxy_list: str | None = None,
        retries: int = 3,
        timeout: int = 30,
        max_proxy_failures: int = 3,
        page_size: int = 24,
        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://shop.rossmann.hu/"
        self.headers = {
            "Content-Type": "application/json",
            "Accept": "*/*",
            "Referer": "https://shop.rossmann.hu/",
            "Origin": "https://shop.rossmann.hu",
        }
        
        # Initialize transport
        self.configure_http_transport(
            headers=self.headers,
            proxy_list=proxy_list,
            max_proxy_failures=max_proxy_failures,
        )
        self.fieldnames = PRODUCT_FIELDNAMES

    def _fetch_page(self, category_path: str, page: int) -> dict | None:
        payload = {
            "query": LIST_PRODUCTS_QUERY,
            "variables": {
                "product_category_path": category_path,
                "first": self.page_size,
                "page": page,
                "filters": []
            }
        }
        self.throttle_requests(self.request_delay)

        data = self.fetch_json_via_backend(
            GRAPHQL_URL,
            method="POST",
            json=payload,
            purpose=f"rossmann {category_path} p{page}",
            retries=self.retries,
            timeout=self.timeout,
            headers=self.headers,
            use_cache=True,
        )
        
        if data is None:
            return None

        try:
            res = data.get("data", {}).get("listProductsByCategory")
            if res is None:
                errors = data.get("errors")
                if errors:
                    logger.debug("Rossmann GraphQL errors: %s", errors)
                return None
            return res
        except Exception as exc:
            logger.debug("Rossmann response parse issue: %s", exc)
            return None

    def parse_product(self, raw: dict) -> dict | None:
        try:
            source_id = str(raw.get("id") or raw.get("lfdnr") or "")
            if not source_id:
                return None

            name = raw.get("name", "").strip()
            slug = raw.get("slug", "")
            source_url = f"{BASE_URL}/termek/{slug}" if slug else ""

            images = raw.get("images", [])
            image_url = images[0].get("url_list", "") if images else ""

            category_objs = raw.get("category_path_main", [])
            category = " > ".join([c.get("name", "") for c in category_objs]) if category_objs else ""

            price_raw = raw.get("price")
            price_val, _ = parse_price(str(price_raw) if price_raw is not None else "")
            
            old_price_raw = raw.get("price_original")
            old_price_val, _ = parse_price(str(old_price_raw) if old_price_raw is not None else "")
            
            discount_percent = calculate_discount(price_val, old_price_val)

            ean_list = raw.get("ean", [])
            ean = ean_list[0] if ean_list else ""

            pkg_value, pkg_unit = parse_package(name)

            return product_record(
                source_product_id=source_id,
                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" if raw.get("in_stock") else "0",
                raw=raw,
            )
        except Exception as exc:
            logger.debug("Rossmann product parse skipped: %s", exc)
            return None

    def scrape_category(self, category_path: str) -> list[dict]:
        products: dict[str, dict] = {}
        
        page = 1
        has_more = True
        
        while has_more:
            data = self._fetch_page(category_path, page)
            if not data:
                break
                
            items = data.get("data", [])
            if items:
                logger.info("  %s: page %d, first item: %s", category_path, page, items[0].get("name"))
            for raw in items:
                parsed = self.parse_product(raw)
                if parsed and parsed["source_product_id"] not in products:
                    products[parsed["source_product_id"]] = parsed
            
            paginator = data.get("paginatorInfo", {})
            has_more = paginator.get("hasMorePages", False)
            total = paginator.get("total", 0)
            
            if page == 1:
                logger.info("Category %s: %d total products", category_path, total)
            
            logger.info("  %s: page %d, %d items, has_more=%s", category_path, page, len(items), has_more)
            
            page += 1
            if page > 200: # Safety cap
                break
                
        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_SLUGS
        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(
                "Rossmann: no categories to scrape after filtering",
                error_category="validation",
            )
            
        self.log_startup_status(logger, f"Rossmann Hungary scraper started — {len(cats)} categories")

        category_results = self.parallel_map(
            self.scrape_category,
            cats,
            label="Rossmann categories",
            flatten=True,
            unit="categories"
        )

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

        unique = list(all_by_id.values())
        self.log_summary(logger, "Rossmann complete", total=len(unique))
        return unique

RossmannScraper = Scraper