-
Notifications
You must be signed in to change notification settings - Fork 3
/
multifeedexporter.py
78 lines (64 loc) · 2.73 KB
/
multifeedexporter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
""" Scrapy extension to export scraped items of different types to multiple feeds. """
import inspect
import importlib
from datetime import datetime
from twisted.internet import defer
from scrapy import log
from scrapy.contrib.feedexport import FeedExporter, SpiderSlot
__version__ = '0.1.1'
class MultiFeedExporter(FeedExporter):
def __init__(self, settings):
super( MultiFeedExporter, self ).__init__(settings)
self.item_names = settings['MULTIFEEDEXPORTER_ITEMS']
self.slots = {}
self.item2uri = {}
@classmethod
def get_bot_items(cls, bot_name):
""" Get item names from items module of the given bot module name
"""
module_name = bot_name + ".items"
item_module = importlib.import_module(module_name)
item_names = [name for name,obj in inspect.getmembers(item_module, inspect.isclass) if obj.__module__.startswith(module_name)]
return item_names
def open_spider(self, spider):
uri_slots = {}
for item_name in self.item_names :
up = self._get_uri_params(spider, item_name)
uri = self.urifmt % up
if uri not in self.slots:
storage = self._get_storage(uri)
file = storage.open(spider)
exporter = self._get_exporter(file)
exporter.start_exporting()
self.slots[uri] = SpiderSlot(file, exporter, storage, uri)
self.item2uri[item_name] = uri
def close_spider(self, spider):
deferreds = []
for uri in self.slots:
slot = self.slots[uri]
if not slot.itemcount and not self.store_empty:
continue
slot.exporter.finish_exporting()
logfmt = "%%s %s feed (%d items) in: %s" % (self.format, \
slot.itemcount, slot.uri)
d = defer.maybeDeferred(slot.storage.store, slot.file)
d.addCallback(lambda _,logfmt=logfmt: log.msg(logfmt % "Stored", spider=spider))
d.addErrback(log.err, logfmt % "Error storing", spider=spider)
deferreds.append(d)
if deferreds:
return defer.DeferredList(deferreds)
def item_scraped(self, item, spider):
item_name = type(item).__name__
slot = self.slots[self.item2uri[item_name]]
slot.exporter.export_item(item)
slot.itemcount += 1
return item
def _get_uri_params(self, spider, item_name):
params = {}
for k in dir(spider):
params[k] = getattr(spider, k)
ts = datetime.utcnow().replace(microsecond=0).isoformat().replace(':', '-')
params['time'] = ts
params['item_name'] = item_name
self._uripar(params, spider)
return params