File: downloadtimeout.py

package info (click to toggle)
python-scrapy 0.14.4-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,064 kB
  • sloc: python: 19,468; xml: 199; sh: 134; makefile: 67
file content (22 lines) | stat: -rw-r--r-- 631 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
"""
Download timeout middleware

See documentation in docs/topics/downloader-middleware.rst
"""
from scrapy.utils.python import WeakKeyCache


class DownloadTimeoutMiddleware(object):

    def __init__(self):
        self._cache = WeakKeyCache(self._download_timeout)

    def _download_timeout(self, spider):
        if hasattr(spider, 'download_timeout'):
            return spider.download_timeout
        return spider.settings.getint('DOWNLOAD_TIMEOUT')

    def process_request(self, request, spider):
        timeout = self._cache[spider]
        if timeout:
            request.meta.setdefault('download_timeout', timeout)