-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredisTomongo.py
57 lines (53 loc) · 2.34 KB
/
redisTomongo.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
# coding:utf-8
import pymongo
import json
import redis
def redis_tomongo():
rediscli = redis.Redis(host="127.0.0.1", port=6379, db=0)
host = "127.0.0.1"
port = 27017
dbname = "baidu"
sheetname = "zhidao"
client = pymongo.MongoClient(host=host, port=port)
mydb = client[dbname]
mysheet = mydb[sheetname]
num=0
while True:
try:
source, data = rediscli.blpop("zhidaospider:items")
item = json.loads(data)
if item.has_key('question'):
mysheet.insert({"_id": item['questionNum'], "question": item['question'],
"questionDes": item['questionDes'], "questionUrl": item['questionUrl']})
num +=1
print num
elif item.has_key('otherPostime'):
mysheet.update_one({"_id": item['questionNum']},{"$set": {("%s.%s") % (item['questionNum'], int(item['rate'] * 100)):
{
"otherAns": item['otherAns'],
"otherPostime": item['otherPostime'],
"otherUp": item['otherUp'],
"otherDown": item['otherDown'],
"otherAuthor": item['otherAuthor'],
"rate": item['rate']
}
}}, True)
num +=1
print num
else:
mysheet.update_one({"_id": item['questionNum']},{"$set": {("%s.%s") % (item['questionNum'], int(item['rate'] * 100)):
{
"bestAns": item['bestAns'],
"bestPostime": item['bestPostime'],
"bestUp": item['bestUp'],
"bestDown": item['bestDown'],
"bestAuthor": item['bestAuthor'],
"rate": item['rate']
}
}}, True)
num+=1
print num
except Exception,e:
print e
if __name__=="__main__":
redis_tomongo()