2010-09-03 14:29:27 -03:00
|
|
|
from zope.interface import implements
|
|
|
|
|
2011-07-19 19:39:27 -03:00
|
|
|
from scrapyd.interfaces import ISpiderQueue
|
2010-09-03 14:29:27 -03:00
|
|
|
from scrapy.utils.sqlite import JsonSqlitePriorityQueue
|
2010-10-31 03:25:37 -02:00
|
|
|
from scrapy.utils.project import sqlite_db
|
2010-09-03 14:29:27 -03:00
|
|
|
|
|
|
|
|
|
|
|
class SqliteSpiderQueue(object):
|
|
|
|
|
|
|
|
implements(ISpiderQueue)
|
|
|
|
|
|
|
|
def __init__(self, database=None, table='spider_queue'):
|
2010-10-31 03:25:37 -02:00
|
|
|
self.q = JsonSqlitePriorityQueue(database, table)
|
2010-09-03 14:29:27 -03:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def from_settings(cls, settings):
|
2010-10-31 03:25:37 -02:00
|
|
|
return cls(sqlite_db(settings['SQLITE_DB']))
|
2010-09-03 14:29:27 -03:00
|
|
|
|
|
|
|
def add(self, name, **spider_args):
|
|
|
|
d = spider_args.copy()
|
|
|
|
d['name'] = name
|
|
|
|
priority = float(d.pop('priority', 0))
|
|
|
|
self.q.put(d, priority)
|
|
|
|
|
|
|
|
def pop(self):
|
|
|
|
return self.q.pop()
|
|
|
|
|
|
|
|
def count(self):
|
|
|
|
return len(self.q)
|
|
|
|
|
|
|
|
def list(self):
|
|
|
|
return [x[0] for x in self.q]
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self.q.clear()
|