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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
"""
Core scraping engine.
Handles static (Requests + BS4) and dynamic (Playwright) scraping.
Implements UA rotation, retry logic, robots.txt checking, pagination, and infinite scroll.
"""
from __future__ import annotations

import hashlib
import json
import logging
import re
import time
import urllib.robotparser
from dataclasses import dataclass, field
from typing import Any, Generator, Optional
from urllib.parse import urljoin, urlparse

import requests
from bs4 import BeautifulSoup
from fake_useragent import UserAgent
from tenacity import (
    retry,
    retry_if_exception_type,
    stop_after_attempt,
    wait_exponential,
)

logger = logging.getLogger(__name__)

# ---------------------------------------------------------------------------
# Data structures
# ---------------------------------------------------------------------------


@dataclass
class ScrapeRequest:
    """Fully-typed request configuration for the scraping engine."""

    url: str
    html_tag: Optional[str] = None
    css_selector: Optional[str] = None
    xpath_selector: Optional[str] = None
    attribute_name: Optional[str] = None
    extraction_type: str = "text"  # text | images | links | attributes | table | json_ld | full_html
    scrape_type: str = "static"  # static | dynamic
    follow_pagination: bool = False
    max_pages: int = 1
    infinite_scroll: bool = False
    scroll_count: int = 3
    download_images: bool = False
    custom_headers: dict = field(default_factory=dict)
    user_agent: Optional[str] = None
    delay_seconds: float = 1.0
    timeout_seconds: int = 30
    max_retries: int = 3
    check_robots_txt: bool = True
    deduplicate: bool = True


@dataclass
class ScrapedItem:
    """A single scraped datum."""

    content: str
    content_type: str
    page_url: str
    page_num: int
    item_index: int
    content_hash: str
    metadata: dict = field(default_factory=dict)


@dataclass
class ScrapeResponse:
    """Final response from the scraping engine."""

    items: list[ScrapedItem]
    pages_scraped: int
    error_count: int
    errors: list[str]
    duration_seconds: float


# ---------------------------------------------------------------------------
# User-Agent rotation
# ---------------------------------------------------------------------------

_ua_instance: Optional[UserAgent] = None


def _get_user_agent(preferred: Optional[str] = None) -> str:
    global _ua_instance
    if preferred:
        return preferred
    try:
        if _ua_instance is None:
            _ua_instance = UserAgent(fallback="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36")
        return _ua_instance.random
    except Exception:
        return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"


# ---------------------------------------------------------------------------
# robots.txt checker
# ---------------------------------------------------------------------------


def _is_allowed_by_robots(url: str, user_agent: str = "*") -> bool:
    """Check whether a URL is allowed by the target site's robots.txt."""
    try:
        parsed = urlparse(url)
        robots_url = f"{parsed.scheme}://{parsed.netloc}/robots.txt"
        rp = urllib.robotparser.RobotFileParser()
        rp.set_url(robots_url)
        rp.read()
        return rp.can_fetch(user_agent, url)
    except Exception as exc:
        logger.warning("robots.txt check failed for %s: %s", url, exc)
        return True  # Allow on error — be conservative


# ---------------------------------------------------------------------------
# Content hashing for deduplication
# ---------------------------------------------------------------------------


def _content_hash(content: str) -> str:
    return hashlib.sha256(content.encode("utf-8", errors="replace")).hexdigest()[:16]


# ---------------------------------------------------------------------------
# HTML Parsers
# ---------------------------------------------------------------------------


def _parse_html(html: str) -> BeautifulSoup:
    try:
        return BeautifulSoup(html, "lxml")
    except Exception:
        return BeautifulSoup(html, "html.parser")


def _extract_with_css(soup: BeautifulSoup, selector: str) -> list[Any]:
    return soup.select(selector)


def _extract_with_xpath(html: str, xpath: str) -> list[str]:
    """XPath extraction via lxml."""
    try:
        from lxml import etree

        tree = etree.fromstring(html.encode(), parser=etree.HTMLParser())
        results = tree.xpath(xpath)
        texts = []
        for r in results:
            if isinstance(r, str):
                texts.append(r.strip())
            elif hasattr(r, "text_content"):
                texts.append(r.text_content().strip())
            elif hasattr(r, "text"):
                texts.append((r.text or "").strip())
        return [t for t in texts if t]
    except Exception as exc:
        logger.warning("XPath extraction failed: %s", exc)
        return []


def _extract_json_ld(soup: BeautifulSoup) -> list[dict]:
    results = []
    for tag in soup.find_all("script", type="application/ld+json"):
        try:
            data = json.loads(tag.string or "")
            results.append(data)
        except (json.JSONDecodeError, TypeError):
            pass
    return results


def _extract_tables(soup: BeautifulSoup) -> list[list[list[str]]]:
    tables = []
    for table in soup.find_all("table"):
        rows = []
        for tr in table.find_all("tr"):
            cells = [td.get_text(strip=True) for td in tr.find_all(["th", "td"])]
            if cells:
                rows.append(cells)
        if rows:
            tables.append(rows)
    return tables


# ---------------------------------------------------------------------------
# Content extractors
# ---------------------------------------------------------------------------


def _extract_items(
    soup: BeautifulSoup,
    raw_html: str,
    req: ScrapeRequest,
    page_url: str,
    page_num: int,
) -> list[ScrapedItem]:
    """Route extraction to the appropriate extractor."""
    items: list[ScrapedItem] = []

    if req.extraction_type == "json_ld":
        for idx, data in enumerate(_extract_json_ld(soup)):
            content = json.dumps(data, ensure_ascii=False)
            items.append(
                ScrapedItem(
                    content=content,
                    content_type="json_ld",
                    page_url=page_url,
                    page_num=page_num,
                    item_index=idx,
                    content_hash=_content_hash(content),
                )
            )
        return items

    if req.extraction_type == "table":
        for t_idx, table in enumerate(_extract_tables(soup)):
            content = json.dumps(table, ensure_ascii=False)
            items.append(
                ScrapedItem(
                    content=content,
                    content_type="table",
                    page_url=page_url,
                    page_num=page_num,
                    item_index=t_idx,
                    content_hash=_content_hash(content),
                )
            )
        return items

    if req.extraction_type == "full_html":
        content = str(soup)
        items.append(
            ScrapedItem(
                content=content,
                content_type="html",
                page_url=page_url,
                page_num=page_num,
                item_index=0,
                content_hash=_content_hash(content),
            )
        )
        return items

    # --- Resolve elements via CSS or XPath or Tag ---
    elements = []

    if req.xpath_selector:
        texts = _extract_with_xpath(raw_html, req.xpath_selector)
        for idx, text in enumerate(texts):
            items.append(
                ScrapedItem(
                    content=text,
                    content_type="text",
                    page_url=page_url,
                    page_num=page_num,
                    item_index=idx,
                    content_hash=_content_hash(text),
                )
            )
        return items

    if req.css_selector:
        elements = _extract_with_css(soup, req.css_selector)
    elif req.html_tag:
        elements = soup.find_all(req.html_tag)
    else:
        elements = soup.find_all(True)  # All elements

    for idx, el in enumerate(elements):
        if req.extraction_type == "text":
            content = el.get_text(separator=" ", strip=True)
        elif req.extraction_type == "links":
            href = el.get("href", "") if el.name == "a" else ""
            if not href:
                link_el = el.find("a")
                href = link_el.get("href", "") if link_el else ""
            if href:
                content = urljoin(page_url, href)
            else:
                continue
        elif req.extraction_type == "images":
            src = el.get("src", "") if el.name == "img" else ""
            if not src:
                img_el = el.find("img")
                src = img_el.get("src", "") if img_el else ""
            if src:
                content = urljoin(page_url, src)
            else:
                continue
        elif req.extraction_type == "attributes":
            if req.attribute_name:
                content = el.get(req.attribute_name, "")
            else:
                content = json.dumps(dict(el.attrs), ensure_ascii=False)
        else:
            content = el.get_text(separator=" ", strip=True)

        content = content.strip()
        if not content:
            continue

        items.append(
            ScrapedItem(
                content=content,
                content_type=req.extraction_type,
                page_url=page_url,
                page_num=page_num,
                item_index=idx,
                content_hash=_content_hash(content),
            )
        )

    return items


# ---------------------------------------------------------------------------
# Static scraper (Requests + BeautifulSoup)
# ---------------------------------------------------------------------------


class StaticScraper:
    """HTTP scraper using Requests with retry, UA rotation, and timeout handling."""

    def __init__(self, req: ScrapeRequest) -> None:
        self.req = req
        self.session = requests.Session()
        self._configure_session()

    def _configure_session(self) -> None:
        ua = _get_user_agent(self.req.user_agent)
        self.session.headers.update(
            {
                "User-Agent": ua,
                "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
                "Accept-Language": "en-US,en;q=0.5",
                "Connection": "keep-alive",
                "Upgrade-Insecure-Requests": "1",
            }
        )
        if self.req.custom_headers:
            self.session.headers.update(self.req.custom_headers)

    @retry(
        retry=retry_if_exception_type((requests.ConnectionError, requests.Timeout)),
        stop=stop_after_attempt(3),
        wait=wait_exponential(multiplier=1, min=1, max=10),
        reraise=True,
    )
    def _fetch(self, url: str) -> requests.Response:
        response = self.session.get(url, timeout=self.req.timeout_seconds, allow_redirects=True)
        response.raise_for_status()
        return response

    def scrape(self) -> Generator[tuple[str, str, int], None, None]:
        """Yields (html, page_url, page_num) for each page."""
        url = self.req.url
        page_num = 1

        while url and page_num <= self.req.max_pages:
            try:
                # Rotate UA per request
                self.session.headers["User-Agent"] = _get_user_agent(self.req.user_agent)
                response = self._fetch(url)
                html = response.text
                yield html, response.url, page_num

                if not self.req.follow_pagination or page_num >= self.req.max_pages:
                    break

                # Find next page link
                soup = _parse_html(html)
                next_url = _find_next_page(soup, response.url)
                if not next_url or next_url == url:
                    break

                url = next_url
                page_num += 1
                time.sleep(self.req.delay_seconds)

            except requests.HTTPError as exc:
                logger.error("HTTP error fetching %s: %s", url, exc)
                raise
            except Exception as exc:
                logger.error("Error fetching %s: %s", url, exc)
                raise

    def close(self) -> None:
        self.session.close()


def _find_next_page(soup: BeautifulSoup, current_url: str) -> Optional[str]:
    """Heuristic: find next pagination link."""
    patterns = [
        "a[rel='next']",
        "a.next",
        "a.pagination-next",
        "li.next a",
        "a[aria-label='Next']",
        ".next-page a",
        "#next a",
    ]
    for sel in patterns:
        el = soup.select_one(sel)
        if el and el.get("href"):
            return urljoin(current_url, el["href"])

    # Fallback: look for links with text "next"
    for a in soup.find_all("a", href=True):
        text = a.get_text(strip=True).lower()
        if text in ("next", "next »", "»", "›", "next page"):
            return urljoin(current_url, a["href"])

    return None


# ---------------------------------------------------------------------------
# Dynamic scraper (Playwright)
# ---------------------------------------------------------------------------


class DynamicScraper:
    """Playwright-based scraper for JS-rendered content with infinite scroll support."""

    def scrape(self, req: ScrapeRequest) -> Generator[tuple[str, str, int], None, None]:
        try:
            from playwright.sync_api import sync_playwright, TimeoutError as PWTimeout
        except ImportError:
            raise RuntimeError("Playwright not installed. Run: playwright install chromium")

        ua = _get_user_agent(req.user_agent)

        with sync_playwright() as p:
            browser = p.chromium.launch(headless=True, args=["--no-sandbox", "--disable-dev-shm-usage"])
            context = browser.new_context(
                user_agent=ua,
                viewport={"width": 1920, "height": 1080},
                extra_http_headers=req.custom_headers or {},
            )
            page = context.new_page()
            page.set_default_timeout(req.timeout_seconds * 1000)

            url = req.url
            page_num = 1

            while url and page_num <= req.max_pages:
                try:
                    page.goto(url, wait_until="networkidle", timeout=req.timeout_seconds * 1000)

                    if req.infinite_scroll:
                        _perform_infinite_scroll(page, req.scroll_count)

                    html = page.content()
                    final_url = page.url
                    yield html, final_url, page_num

                    if not req.follow_pagination or page_num >= req.max_pages:
                        break

                    # Try clicking next page button
                    next_url = _playwright_next_page(page, final_url)
                    if not next_url or next_url == url:
                        break

                    url = next_url
                    page_num += 1
                    time.sleep(req.delay_seconds)

                except PWTimeout as exc:
                    logger.error("Playwright timeout on %s: %s", url, exc)
                    raise
                except Exception as exc:
                    logger.error("Playwright error on %s: %s", url, exc)
                    raise

            context.close()
            browser.close()


def _perform_infinite_scroll(page, scroll_count: int) -> None:
    """Scroll to bottom repeatedly to trigger lazy loading."""
    for _ in range(scroll_count):
        prev_height = page.evaluate("document.body.scrollHeight")
        page.evaluate("window.scrollTo(0, document.body.scrollHeight)")
        page.wait_for_timeout(1500)
        new_height = page.evaluate("document.body.scrollHeight")
        if new_height == prev_height:
            break


def _playwright_next_page(page, current_url: str) -> Optional[str]:
    """Try to find and return next page URL from Playwright page."""
    selectors = ["a[rel='next']", "a.next", "li.next a", "[aria-label='Next']"]
    for sel in selectors:
        try:
            el = page.query_selector(sel)
            if el:
                href = el.get_attribute("href")
                if href:
                    return urljoin(current_url, href)
        except Exception:
            pass
    return None


# ---------------------------------------------------------------------------
# Main engine entrypoint
# ---------------------------------------------------------------------------


def run_scrape(req: ScrapeRequest) -> ScrapeResponse:
    """
    Execute a scrape job and return structured results.
    Handles robots.txt, deduplication, pagination, and error accounting.
    """
    start_time = time.monotonic()
    items: list[ScrapedItem] = []
    errors: list[str] = []
    pages_scraped = 0
    seen_hashes: set[str] = set()

    # robots.txt
    if req.check_robots_txt:
        if not _is_allowed_by_robots(req.url):
            return ScrapeResponse(
                items=[],
                pages_scraped=0,
                error_count=1,
                errors=[f"robots.txt disallows scraping: {req.url}"],
                duration_seconds=time.monotonic() - start_time,
            )

    try:
        if req.scrape_type == "dynamic":
            scraper = DynamicScraper()
            pages = scraper.scrape(req)
        else:
            scraper = StaticScraper(req)
            pages = scraper.scrape()

        for html, page_url, page_num in pages:
            pages_scraped += 1
            soup = _parse_html(html)
            page_items = _extract_items(soup, html, req, page_url, page_num)

            for item in page_items:
                if req.deduplicate and item.content_hash in seen_hashes:
                    continue
                seen_hashes.add(item.content_hash)
                items.append(item)

        if req.scrape_type == "static" and hasattr(scraper, "close"):
            scraper.close()

    except Exception as exc:
        error_msg = f"Scrape failed: {type(exc).__name__}: {exc}"
        logger.exception(error_msg)
        errors.append(error_msg)

    return ScrapeResponse(
        items=items,
        pages_scraped=pages_scraped,
        error_count=len(errors),
        errors=errors,
        duration_seconds=time.monotonic() - start_time,
    )