"""XNXX.com — direct HTML scrape search results.

Search: `https://www.xnxx.com/search/<q>/<page-1>` (xnxx 0-indexed)
Scene URL: `https://www.xnxx.com/video-<id>/<slug>`
"""
from __future__ import annotations

import re

from app.connectors.direct_scrapers._search_base import BaseSearchScraper


class XnxxScraper(BaseSearchScraper):
    sitetag = "xnxxcom"
    # `/<page-1>` — handle override in search() by replacing {page}.
    _search_url_template = "https://www.xnxx.com/search/{query}/{page}"
    _scene_url_re = re.compile(
        r'href="(?P<url>/video-[a-z0-9]+/(?P<slug>[a-z0-9_\-]+))"',
        re.IGNORECASE,
    )

    def search(self, query, *, page=1, limit=None):
        original = self._search_url_template
        self._search_url_template = original.replace("{page}", str(page - 1))
        try:
            yield from super().search(query, page=page, limit=limit)
        finally:
            self._search_url_template = original
