-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
40 lines (29 loc) · 1.15 KB
/
db.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
# -*- coding: utf-8 -*-
# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
from pymongo import MongoClient
class MongoDBPipeline(object):
def __init__(self,db,col,address,replicaSet):
# self.db = DB['db']
# self.col = DB['col']
# #connection = pymongo.Connection(self.server,self.port)
# #connection = MongoClient(self.server,self.port)
# connection = MongoClient(DB['address'], replicaSet=DB['replicaSet'])
# db = connection[self.db]
# self.collection = db[self.col]
print 'db is ['+db+']'
self.db = db
self.col = col
#connection = pymongo.Connection(self.server,self.port)
#connection = MongoClient(self.server,self.port)
connection = MongoClient(address, replicaSet=replicaSet)
db = connection[self.db]
self.collection = db[self.col]
def save(self, item):
self.collection.insert_one(dict(item))
return item
def find(self, item):
item = self.collection.find_one(item)
return item