Scraper Source: pelenka_scraper

Read-only source view.

pelenka_scraper.py · 17468 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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import json
import logging
import threading
import time
import hashlib
from bs4 import BeautifulSoup
from urllib.parse import urljoin
from typing import Any, List, Dict, Optional, Mapping, Set, Tuple, Callable
from scrapers.base_scraper import BaseScraper, ScrapeRunResult
from scrapers.common import PRODUCT_FIELDNAMES, ProxyPool, ResponseCache, ThreadLocalSessionFactory
from scrapers.exceptions import ScraperAbortError
from scrapers.options import RuntimeOptions

logger = logging.getLogger(__name__)


class Scraper(BaseScraper):
    execution_mode = "api"
    store_definitions = (
        {
            "retailer_code": "pelenka-hu",
            "name": "Pelenka Hungary",
            "country_code": "HU",
            "base_url": "https://www.pelenka.hu",
            "scraper_module": "scrapers.platform.pelenka_scraper",
            "scraper_mapping": {
                "execution_mode": "api",
                "output_schema": "product",
            },
        },
    )

    def __init__(
        self,
        workers=10,
        proxy_list=None,
        retries=2,
        timeout=20,
        max_proxy_failures=3,
    ):
        super().__init__()
        self.workers = workers
        self.retries = retries
        self.timeout = timeout
        self.max_proxy_failures = max_proxy_failures

        self.base_url = "https://www.pelenka.hu"
        self.proxy_validation_url = self.base_url

        self.headers = {
            "Accept": "application/json, text/javascript, */*; q=0.01",
            "X-Requested-With": "XMLHttpRequest",
        }
        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 _process_discovery_url(self, url, level):
        # Discovery only needs 2 retries to keep it fast
        resp = self.fetch_response_via_backend(
            url,
            purpose=f"pelenka discovery l{level}",
            retries=2,
            timeout=self.timeout,
            headers=self.headers,
            retry_sleep=lambda attempt: (time.sleep((2 ** (attempt - 1)) + (0.1 * (attempt - 1)))),
        )
        status = getattr(resp, "status_code", getattr(resp, "status", None)) if resp else None
        err = None if resp and status == 200 else ("request_failed" if resp is None else f"HTTP {status}")
        if not resp:
            return [], []

        found_cats = []
        next_visit = []

        try:
            soup = BeautifulSoup(resp.text, "html.parser")
            links = soup.select("a")
            for a in links:
                href = str(a.get("href", ""))
                if not href:
                    continue
                if href.startswith("/"):
                    href = urljoin(self.base_url, href)

                clean_url = href.split("?")[0].split("#")[0].rstrip("/")

                if "pelenka.hu/" in clean_url and not any(
                    x in clean_url
                    for x in [
                        "blog",
                        "shop_",
                        "mobilapp",
                        "login",
                        "reg",
                        "cart",
                        "help",
                        "contact",
                        "magunkrol",
                        "ugyfelszolgalat",
                    ]
                ):
                    if clean_url != self.base_url and clean_url != self.base_url + "/":
                        if not any(
                            clean_url.endswith(ext)
                            for ext in [".php", ".jpg", ".png", ".webp", ".pdf"]
                        ):
                            found_cats.append(clean_url)
                            if level < 1:
                                next_visit.append(clean_url)
        except Exception as exc:
            logger.debug("Discovery parse failed for %s level %s: %s", url, level, exc)

        return found_cats, next_visit

    def get_categories(self, filter_str=None):
        logger.info("Fetching categories (parallel recursive discovery)...")
        to_visit = [self.base_url]
        visited = set()
        cat_urls = set()

        for level in range(2):
            logger.info(
                "Discovery Level %d: Processing %d URLs using %d workers...",
                level, len(to_visit), self.workers,
            )

            level_next_visit = set()
            urls_to_process = [u for u in to_visit if u not in visited]
            found_data = self.parallel_map(
                lambda u: self._process_discovery_url(u, level),
                urls_to_process,
                label=f"Pelenka Level {level} discovery",
                unit="discovery_urls"
            )

            for found_cats, next_v in found_data:
                for c in found_cats:
                    cat_urls.add(c)
                for n in next_v:
                    level_next_visit.add(n)
            
            for u in urls_to_process:
                visited.add(u)

            to_visit = sorted(list(level_next_visit))
            if not to_visit:
                break

        logger.info("Discovery complete. Found %d total categories.", len(cat_urls))
        if filter_str:
            cat_urls = {u for u in cat_urls if filter_str.lower() in u.lower()}
            logger.info("%d categories remaining after filter.", len(cat_urls))

        return sorted(list(cat_urls))

    def parse_product(self, product_div, category_name):
        try:
            from scrapers.records import product_record, parse_package, parse_price, calculate_discount

            sku = (product_div.get("data-sku") or "").strip()
            name = product_div.get("data-name", "").strip()

            url_el = product_div.select_one(".product__name-link")
            url = url_el.get("href") if url_el else ""
            if url and not url.startswith("http"):
                url = urljoin(self.base_url, url)

            if not sku:
                if not url:
                    return None
                sku = f"url:{hashlib.sha1(url.encode('utf-8')).hexdigest()}"

            img_el = product_div.select_one(".product__img")
            image = img_el.get("src") if img_el else ""

            prices_div = product_div.select_one(".product__prices")
            price_val = ""
            old_price_val = ""
            currency = "HUF"

            if prices_div:
                if "has-price-sale" in prices_div.get("class", []):
                    sale_price_el = prices_div.select_one(".product__price-sale .price-gross")
                    base_price_el = prices_div.select_one(".product__price-base .price-gross")
                    price_val, currency = parse_price(sale_price_el.text if sale_price_el else "")
                    old_price_val, _ = parse_price(base_price_el.text if base_price_el else "")
                else:
                    base_price_el = prices_div.select_one(".product__price-base .price-gross")
                    price_val, currency = parse_price(base_price_el.text if base_price_el else "")

            discount_el = product_div.select_one(".product__badge-sale") or product_div.select_one(".first-percent")
            discount_percent = calculate_discount(price_val, old_price_val)
            if not discount_percent and discount_el:
                discount_percent = discount_el.text.strip().replace("-", "").replace("%", "")

            packaging_value, packaging_unit = parse_package(name)

            unit_text_el = product_div.select_one(".product__price-unit .price-currency")
            if unit_text_el and not packaging_unit:
                ut_clean = unit_text_el.text.strip().replace('/', '').replace('Ft', '').strip()
                _, packaging_unit = parse_package(f"1 {ut_clean}")

            return product_record(
                source_product_id=sku,
                name=name,
                price=price_val,
                old_price=old_price_val,
                currency=currency,
                discount_percent=discount_percent,
                image_url=image,
                category=category_name,
                packaging_value=packaging_value,
                packaging_unit=packaging_unit,
                source_url=url,
                ean_code="",  # Not easily available in listing
                availability="1",
                raw=str(product_div)[:5000],
            )
        except Exception as e:
            logger.debug("Product parse skipped: %s", e)
            return None

    def scrape_category(self, cat_url):
        category_name = (
            cat_url.split("/")[-1].replace("_", " ").replace("-", " ").title()
        )
        logger.info("Scraping category: %s", category_name)

        page = 1
        all_products = []
        seen_skus = set()

        while True:
            params = {
                "action": "cat_art_list",
                "ajax": "1",
                "page": page,
                "infinite_scroll": "1",
                "ajax_nodesign": "1",
            }
            resp = self.fetch_response_via_backend(
                cat_url,
                params=params,
                purpose=f"pelenka category {category_name} p{page}",
                retries=self.retries,
                timeout=self.timeout,
                headers=self.headers,
                retry_sleep=lambda attempt: (time.sleep((2 ** (attempt - 1)) + (0.1 * (attempt - 1)))),
            )
            status = getattr(resp, "status_code", getattr(resp, "status", None)) if resp else None
            err = None if resp and status == 200 else ("request_failed" if resp is None else f"HTTP {status}")
            if not resp:
                if page == 1:
                    logger.error("Failed to fetch first page of %s: %s", category_name, err)
                break

            try:
                # Check if response looks like JSON
                if not resp.text.strip().startswith("{"):
                    if page == 1:
                        logger.debug("%s: non-JSON response on page 1, skipping category", category_name)
                    break

                data = resp.json()
                html = data.get("product_list_content", "")
                if not html or "<div" not in html:
                    break

                soup = BeautifulSoup(html, "html.parser")
                product_divs = soup.select(".js-product")
                if not product_divs:
                    break

                new_on_page = 0
                for div in product_divs:
                    p = self.parse_product(div, category_name)
                    if p:
                        if p["source_product_id"] not in seen_skus:
                            all_products.append(p)
                            seen_skus.add(p["source_product_id"])
                            new_on_page += 1

                if new_on_page == 0:
                    logger.info("%s: No new items on page %d. Finishing category.", category_name, page)
                    break

                logger.info("%s: Page %d done (%d new items)", category_name, page, new_on_page)
                page += 1

            except Exception as e:
                logger.error("Error processing page %d of %s: %s", page, category_name, e)
                break

        return all_products

    def get_ean_from_url(self, url):
        if not url:
            return ""
        logger.debug("Checking EAN for: %s", url)
        resp = self.fetch_response_via_backend(
            url,
            purpose="pelenka ean detail",
            retries=self.retries,
            timeout=self.timeout,
            headers=self.headers,
            retry_sleep=lambda attempt: (time.sleep((2 ** (attempt - 1)) + (0.1 * (attempt - 1)))),
        )
        status = getattr(resp, "status_code", getattr(resp, "status", None)) if resp else None
        err = None if resp and status == 200 else ("request_failed" if resp is None else f"HTTP {status}")
        if not resp:
            logger.debug("EAN detail fetch failed: %s (%s)", url, err)
            return ""

        try:
            soup = BeautifulSoup(resp.text, "html.parser")
            scripts = soup.select('script[type="application/ld+json"]')
            if not scripts:
                logger.debug("No JSON-LD found on %s", url)

            for script in scripts:
                try:
                    data = json.loads(script.string if script.string else script.text)
                    items = data if isinstance(data, list) else [data]
                    for item in items:
                        if item.get("@type") == "Product":
                            ean = (
                                item.get("gtin13")
                                or item.get("productId", "").replace("ean:", "")
                                or ""
                            )
                            if not ean and "additionalProperty" in item:
                                for prop in item["additionalProperty"]:
                                    if prop.get("name") == "EAN":
                                        ean = prop.get("value")
                                        break
                            if ean:
                                ean_clean = (
                                    str(ean)
                                    .replace("&amp;nbsp;", "")
                                    .replace("&nbsp;", "")
                                    .strip()
                                )
                                logger.debug("Found EAN %s for %s", ean_clean, url)
                                return ean_clean
                except Exception as je:
                    logger.debug("JSON-LD parse error on %s: %s", url, je)
                    continue
        except Exception as e:
            logger.debug("EAN extraction parse error on %s: %s", url, e)

        logger.debug("No EAN found for %s", url)
        return ""

    def run(self, target: Any = None, context: Dict[str, Any] | None = None) -> ScrapeRunResult:
        opts = RuntimeOptions.from_run_args(target, context)
        records = self.collect_products(
            limit_categories=opts.limit_categories,
            category_filter=opts.category_filter,
        )
        # Handle the case where collect_products returns ScrapeRunResult or List
        if isinstance(records, ScrapeRunResult):
            return records
            
        return ScrapeRunResult(
            records=records,
            raw_records=records,
            accepted_records=records,
            rejected_records=[],
            telemetry=self.telemetry,
        )

    def collect_products(self, limit_categories: int | None = None, category_filter: str | None = None) -> List[Dict[str, Any]] | ScrapeRunResult:
        self.log_startup_status(logger, "Starting Pelenka crawl")

        categories = self.get_categories(filter_str=category_filter)
        if not categories:
            raise ScraperAbortError(
                "Pelenka: category discovery returned no results — "
                "possible network failure or site structure change",
                error_category="network",
            )

        if limit_categories:
            categories = categories[:limit_categories]

        logger.info("Stage 1: Finding products in %d categories", len(categories))
        
        category_results = self.parallel_map(
            self.scrape_category, 
            categories, 
            label="Pelenka Stage 1",
            flatten=False,
            unit="categories"
        )

        all_found_products = {}  # source_product_id -> product_dict
        for products in category_results:
            for p in products:
                pid = p["source_product_id"]
                if pid not in all_found_products:
                    all_found_products[pid] = p
                else:
                    old_cat = all_found_products[pid]["category"]
                    if p["category"] not in old_cat:
                        all_found_products[pid]["category"] = f"{old_cat}, {p['category']}"

        unique_products = list(all_found_products.values())
        logger.info("Stage 1 complete. Found %d unique products.", len(unique_products))

        if not unique_products:
            return []

        logger.info("Stage 2: Extracting EAN codes from %d product pages", len(unique_products))
        
        # In Stage 2, we update the products in place
        def extract_ean_worker(p):
            if p.get("source_url"):
                p["ean_code"] = self.get_ean_from_url(p["source_url"])
            return p

        unique_products = self.parallel_map(
            extract_ean_worker,
            unique_products,
            label="Pelenka Stage 2",
            unit="products"
        )

        self.log_summary(logger, "Crawl complete")
        return unique_products


PelenkaScraper = Scraper