Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: add BanAwareCachePolicy for http caching #26

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ scrapy-rotating-proxies. To disable proxying for a request set
``request.meta['proxy'] = None``; to set proxy explicitly use
``request.meta['proxy'] = "<my-proxy-address>"``.

To use scrapy's http cache with rotating proxies enable ban aware caching policy that prevents caching of banned responses:

HTTPCACHE_POLICY = 'rotating_proxies.policy.BanAwareCachePolicy'


Concurrency
-----------
Expand Down
14 changes: 14 additions & 0 deletions rotating_proxies/policy.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# -*- coding: utf-8 -*-
from scrapy.exceptions import IgnoreRequest
from scrapy.extensions.httpcache import DummyPolicy


class BanAwareCachePolicy(DummyPolicy):
"""
This policy is for scrapy http caching that will prevent caching banned responses
Usage, in settings.py:
HTTPCACHE_POLICY = 'rotating_proxies.policy.BanAwareCachePolicy'
"""

def should_cache_response(self, response, request):
if request.meta.get('_ban', False):
return False
return super().should_cache_response(response, request):


class BanDetectionPolicy(object):
Expand Down