-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.py
69 lines (61 loc) · 2.12 KB
/
database.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
# coding:utf-8
import time
from datetime import time, datetime, timedelta, date
from pymongo import MongoClient
"""
这个模块用于进行微博内容存储与读取的操作
"""
# TODO: 组织好的微博数据存入数据库。
# TODO: 提取数据库中的数据
# TODO: 存储前查询数据是否存在
class DataBase(object):
def __init__(self):
self.client = MongoClient()
self.sinadb = self.client.sina
self.weibocol = self.sinadb.weibo
self.status = 'INITIATE'
if self.weibocol.find_one({'test':'ok'}):
print('已连接Mongodb, collection:weibo')
self.status = 'CONNECTED'
else:
self.status = 'FAILURE_MONGODB'
def addin(self, weibodict, check_mode=False):
if check_mode is True:
if self.check(weibodict['i_stamp']) is True:
print('发现重复数据,不执行插入')
return {}
else:
pass
else:
pass
try:
message = self.weibocol.insert_one(weibodict)
strnow = datetime.now().strftime('%Y-%m-%d %X')
if message._WriteResult__acknowledged:
print('已插入微博数据到数据库,集合:weibo,插入时间为:{}'.format(strnow))
self.status = 'INSERT_OK'
return {}
else:
print('插入操作失败,集合:weibo,插入时间为:{}'.format(strnow))
self.status = 'INSERT_FAILURE'
return weibodict
except (UnicodeEncodeError, UnicodeError, UnicodeDecodeError):
print('出现编码问题,跳过插入此条微博到数据库')
return weibodict
def close(self):
self.client.close()
def fetch(self, **kwargs):
pass
def check(self, stamp=None):
res = self.weibocol.find_one({'i_stamp':stamp})
if res:
return True
else:
return False
def delete(self, **kwargs):
pass
if __name__ == '__main__':
a = {'a':'a', 'b':'b'}
d = DataBase()
mes = d.addin(a)
d.close()