From 38e292a13207443c9f966fb7168e37fb69965a7e Mon Sep 17 00:00:00 2001 From: muriloviana Date: Wed, 19 Oct 2016 12:47:23 -0200 Subject: [PATCH] add from_crawler method to template and turn the returns methods explicit --- .../project/module/middlewares.py.tmpl | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/scrapy/templates/project/module/middlewares.py.tmpl b/scrapy/templates/project/module/middlewares.py.tmpl index 91d05baa6..4f7c8d191 100644 --- a/scrapy/templates/project/module/middlewares.py.tmpl +++ b/scrapy/templates/project/module/middlewares.py.tmpl @@ -5,22 +5,34 @@ # See documentation in: # http://doc.scrapy.org/en/latest/topics/spider-middleware.html +from scrapy import signals + class ${ProjectName}SpiderMiddleware(object): + # If any of methods bellow is not defined, in practice, the middleware + # manager only add the methods that the middleware has defined. + + @classmethod + def from_crawler(cls, crawler): + # This method is used by Scrapy to create your spiders. + s = cls(crawler.stats) + crawler.signals.connect(s.spider_opened, signal=signals.spider_opened) + return s def process_spider_input(response, spider): # Called for each response that goes through the spider # middleware and into the spider. # Should return None or raise an exception. - pass + return None def process_spider_output(response, result, spider): # Called with the results returned from the Spider, after # it has processed the response. # Must return an iterable of Request, dict or Item objects. - pass + for i in result: + yield i def process_spider_exception(response, exception, spider): # Called when a spider or process_spider_input() method @@ -36,4 +48,5 @@ class ${ProjectName}SpiderMiddleware(object): # that it doesn’t have a response associated. # Must return only requests (not items). - pass + for r in start_requests: + yield r