from scrapy import Request
from scrapy.http import Response
from scrapy.extensions.httpcache import DummyPolicy
from scrapy.settings import BaseSettings

import re


class CacheOnlyPolicy(DummyPolicy):
    def __init__(self, settings: BaseSettings):
        super().__init__(settings)
        accept = settings.get('HTTPCACHE_ACCEPT_HTTP_CODES')
        if accept and len(accept) > 0:
            self.accept = set(int(x) for x in accept.split(','))
        self.regex_accept = settings.get('HTTPCACHE_ACCEPT_HTTP_CODES_REGEX')

    def should_cache_request(self, request: Request) -> bool:
        if request.meta.get('dont_cache', False):
            return False
        return super().should_cache_request(request)

    def should_cache_response(self, response: Response, request: Request):
        if hasattr(self, 'accept'):
            if response.status in self.accept:
                return True
        if hasattr(self, 'regex_accept'):
            if re.match(self.regex_accept, str(response.status)):
                return True
        return False
