<s> """<STR_LIT>""" <EOL> from collections import defaultdict <EOL> from twisted . internet import reactor <EOL> from ants import signals <EOL> class CloseSpider ( object ) : <EOL> def __init__ ( self , crawler ) : <EOL> self . crawler = crawler <EOL> self . close_on = { <EOL> '<STR_LIT>' : crawler . settings . getfloat ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : crawler . settings . getint ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : crawler . settings . getint ( '<STR_LIT>' ) , <EOL> '<STR_LIT>' : crawler . settings . getint ( '<STR_LIT>' ) , <EOL> } <EOL> self . counter = defaultdict ( int ) <EOL> if self . close_on . get ( '<STR_LIT>' ) : <EOL> crawler . signals . connect ( self . error_count , signal = signals . spider_error ) <EOL> if self . close_on . get ( '<STR_LIT>' ) : <EOL> crawler . signals . connect ( self . page_count , signal = signals . response_received ) <EOL> if self . close_on . get ( '<STR_LIT>' ) : <EOL> crawler . signals . connect ( self . spider_opened , signal = signals . spider_opened ) <EOL> if self . close_on . get ( '<STR_LIT>' ) : <EOL> crawler . signals . connect ( self . item_scraped , signal = signals . item_scraped ) <EOL> crawler . signals . connect ( self . spider_closed , signal = signals . spider_closed ) <EOL> @ classmethod <EOL> def from_crawler ( cls , crawler ) : <EOL> return cls ( crawler ) <EOL> def error_count ( self , failure , response , spider ) : <EOL> self . counter [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> if self . counter [ '<STR_LIT>' ] == self . close_on [ '<STR_LIT>' ] : <EOL> self . crawler . engine . close_spider ( spider , '<STR_LIT>' ) <EOL> def page_count ( self , response , request , spider ) : <EOL> self . counter [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> if self . counter [ '<STR_LIT>' ] == self . close_on [ '<STR_LIT>' ] : <EOL> self . crawler . engine . close_spider ( spider , '<STR_LIT>' ) <EOL> def spider_opened ( self , spider ) : <EOL> self . task = reactor . callLater ( self . close_on [ '<STR_LIT>' ] , self . crawler . engine . close_spider , spider , reason = '<STR_LIT>' ) <EOL> def item_scraped ( self , item , spider ) : <EOL> self . counter [ '<STR_LIT>' ] += <NUM_LIT:1> <EOL> if self . counter [ '<STR_LIT>' ] == self . close_on [ '<STR_LIT>' ] : <EOL> self . crawler . engine . close_spider ( spider , '<STR_LIT>' ) <EOL> def spider_closed ( self , spider ) : <EOL> task = getattr ( self , '<STR_LIT>' , False ) <EOL> if task and task . active ( ) : <EOL> task . cancel ( ) </s>
