forked from sebdah/scrapy-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrapy_mongodb.py
278 lines (233 loc) · 9.32 KB
/
scrapy_mongodb.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
"""
scrapy-mongodb - MongoDB pipeline for Scrapy
Homepage: https://github.com/sebdah/scrapy-mongodb
Author: Sebastian Dahlgren <[email protected]>
License: Apache License 2.0 <http://www.apache.org/licenses/LICENSE-2.0.html>
Copyright 2013 Sebastian Dahlgren
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import datetime
from pymongo import errors
from pymongo.mongo_client import MongoClient
from pymongo.mongo_replica_set_client import MongoReplicaSetClient
from pymongo.read_preferences import ReadPreference
from scrapy import log
VERSION = '0.7.1'
def not_set(string):
""" Check if a string is None or ''
:returns: bool - True if the string is empty
"""
if string is None:
return True
elif string == '':
return True
return False
class MongoDBPipeline():
""" MongoDB pipeline class """
# Default options
config = {
'uri': 'mongodb://localhost:27017',
'fsync': False,
'write_concern': 0,
'database': 'scrapy-mongodb',
'collection': 'items',
'replica_set': None,
'unique_key': None,
'buffer': None,
'append_timestamp': False,
'stop_on_duplicate': 0,
}
# Item buffer
current_item = 0
item_buffer = []
# Duplicate key occurence count
duplicate_key_count = 0
@classmethod
def from_crawler(cls, crawler):
return cls(crawler)
def __init__(self, crawler):
""" Constructor """
self.settings = crawler.settings
self.crawler = crawler
# Configure the connection
self.configure()
if self.config['replica_set'] is not None:
connection = MongoReplicaSetClient(
self.config['uri'],
replicaSet=self.config['replica_set'],
w=self.config['write_concern'],
fsync=self.config['fsync'],
read_preference=ReadPreference.PRIMARY_PREFERRED)
else:
# Connecting to a stand alone MongoDB
connection = MongoClient(
self.config['uri'],
fsync=self.config['fsync'],
read_preference=ReadPreference.PRIMARY)
# Set up the collection
database = connection[self.config['database']]
self.collection = database[self.config['collection']]
log.msg('Connected to MongoDB {0}, using "{1}/{2}"'.format(
self.config['uri'],
self.config['database'],
self.config['collection']))
# Ensure unique index
if self.config['unique_key']:
self.collection.ensure_index(self.config['unique_key'], unique=True)
log.msg('Ensuring index for key {0}'.format(
self.config['unique_key']))
# Get the duplicate on key option
if self.config['stop_on_duplicate']:
tmpValue = self.config['stop_on_duplicate']
if tmpValue < 0:
log.msg(
(
'Negative values are not allowed for'
' MONGODB_STOP_ON_DUPLICATE option.'
),
level=log.ERROR
)
raise SyntaxError(
(
'Negative values are not allowed for'
' MONGODB_STOP_ON_DUPLICATE option.'
)
)
self.stop_on_duplicate = self.config['stop_on_duplicate']
else:
self.stop_on_duplicate = 0
def configure(self):
""" Configure the MongoDB connection """
# Handle deprecated configuration
if not not_set(self.settings['MONGODB_HOST']):
log.msg(
'DeprecationWarning: MONGODB_HOST is deprecated',
level=log.WARNING)
mongodb_host = self.settings['MONGODB_HOST']
if not not_set(self.settings['MONGODB_PORT']):
log.msg(
'DeprecationWarning: MONGODB_PORT is deprecated',
level=log.WARNING)
self.config['uri'] = 'mongodb://{0}:{1:i}'.format(
mongodb_host,
self.settings['MONGODB_PORT'])
else:
self.config['uri'] = 'mongodb://{0}:27017'.format(mongodb_host)
if not not_set(self.settings['MONGODB_REPLICA_SET']):
if not not_set(self.settings['MONGODB_REPLICA_SET_HOSTS']):
log.msg(
(
'DeprecationWarning: '
'MONGODB_REPLICA_SET_HOSTS is deprecated'
),
level=log.WARNING)
self.config['uri'] = 'mongodb://{0}'.format(
self.settings['MONGODB_REPLICA_SET_HOSTS'])
# Set all regular options
options = [
('uri', 'MONGODB_URI'),
('fsync', 'MONGODB_FSYNC'),
('write_concern', 'MONGODB_REPLICA_SET_W'),
('database', 'MONGODB_DATABASE'),
('collection', 'MONGODB_COLLECTION'),
('replica_set', 'MONGODB_REPLICA_SET'),
('unique_key', 'MONGODB_UNIQUE_KEY'),
('buffer', 'MONGODB_BUFFER_DATA'),
('append_timestamp', 'MONGODB_ADD_TIMESTAMP'),
('stop_on_duplicate', 'MONGODB_STOP_ON_DUPLICATE')
]
for key, setting in options:
if not not_set(self.settings[setting]):
self.config[key] = self.settings[setting]
# Check for illegal configuration
if self.config['buffer'] and self.config['unique_key']:
log.msg(
(
'IllegalConfig: Settings both MONGODB_BUFFER_DATA '
'and MONGODB_UNIQUE_KEY is not supported'
),
level=log.ERROR)
raise SyntaxError(
(
'IllegalConfig: Settings both MONGODB_BUFFER_DATA '
'and MONGODB_UNIQUE_KEY is not supported'
))
def process_item(self, item, spider):
""" Process the item and add it to MongoDB
:type item: Item object
:param item: The item to put into MongoDB
:type spider: BaseSpider object
:param spider: The spider running the queries
:returns: Item object
"""
if self.config['buffer']:
self.current_item += 1
item = dict(item)
if self.config['append_timestamp']:
item['scrapy-mongodb'] = {'ts': datetime.datetime.utcnow()}
self.item_buffer.append(item)
if self.current_item == self.config['buffer']:
self.current_item = 0
return self.insert_item(self.item_buffer, spider)
else:
return item
return self.insert_item(item, spider)
def close_spider(self, spider):
""" Method called when the spider is closed
:type spider: BaseSpider object
:param spider: The spider running the queries
:returns: None
"""
if self.item_buffer:
self.insert_item(self.item_buffer, spider)
def insert_item(self, item, spider):
""" Process the item and add it to MongoDB
:type item: (Item object) or [(Item object)]
:param item: The item(s) to put into MongoDB
:type spider: BaseSpider object
:param spider: The spider running the queries
:returns: Item object
"""
if not isinstance(item, list):
item = dict(item)
if self.config['append_timestamp']:
item['scrapy-mongodb'] = {'ts': datetime.datetime.utcnow()}
if self.config['unique_key'] is None:
try:
self.collection.insert(item, continue_on_error=True)
log.msg(
'Stored item(s) in MongoDB {0}/{1}'.format(
self.config['database'], self.config['collection']),
level=log.DEBUG,
spider=spider)
except errors.DuplicateKeyError:
log.msg('Duplicate key found', level=log.DEBUG)
if (self.stop_on_duplicate > 0):
self.duplicate_key_count += 1
if (self.duplicate_key_count >= self.stop_on_duplicate):
self.crawler.engine.close_spider(
spider,
'Number of duplicate key insertion exceeded'
)
pass
else:
self.collection.update(
{
self.config['unique_key']: item[self.config['unique_key']]
},
item,
upsert=True)
log.msg(
'Stored item(s) in MongoDB {0}/{1}'.format(
self.config['database'], self.config['collection']),
level=log.DEBUG,
spider=spider)
return item