<s> __author__ = '<STR_LIT>' <EOL> import traceback <EOL> import logging <EOL> from threading import Thread <EOL> from twisted . internet import reactor <EOL> from scrapy . crawler import Crawler <EOL> from scrapy import log , signals <EOL> from scrapy . xlib . pydispatch import dispatcher <EOL> from scrapy . exceptions import DontCloseSpider <EOL> from scrapy . utils . project import get_project_settings <EOL> from stockspider . spiders . hq_spider import HqSpider <EOL> from common import inject , depends_on <EOL> @ depends_on ( '<STR_LIT>' ) <EOL> class ScrapySpider : <EOL> def __init__ ( self ) : <EOL> self . spider = HqSpider ( ) <EOL> self . crawler = crawler = Crawler ( get_project_settings ( ) ) <EOL> crawler . signals . connect ( reactor . stop , signal = signals . spider_closed ) <EOL> crawler . configure ( ) <EOL> crawler . crawl ( self . spider ) <EOL> dispatcher . connect ( self . _dont_close_me , signals . spider_idle ) <EOL> self . thread = None <EOL> self . _started = False <EOL> self . _stopped = False <EOL> def start ( self ) : <EOL> def run ( ) : <EOL> try : <EOL> logging . info ( '<STR_LIT>' ) <EOL> reactor . run ( installSignalHandlers = False ) <EOL> except Exception , e : <EOL> print traceback . format_exc ( ) <EOL> if not self . _started : <EOL> self . _started = True <EOL> self . crawler . start ( ) <EOL> log . start_from_settings ( get_project_settings ( ) ) <EOL> self . thread = Thread ( target = run ) <EOL> log . msg ( '<STR_LIT>' ) <EOL> self . thread . start ( ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> def stop ( self ) : <EOL> if not self . _started : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> elif self . _stopped : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> else : <EOL> log . msg ( '<STR_LIT>' ) <EOL> self . _stopped = True <EOL> self . crawler . stop ( ) <EOL> def _dont_close_me ( self , spider ) : <EOL> raise DontCloseSpider ( "<STR_LIT>" ) </s>
