forked from qiyeboy/IPProxyPool
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMongoHelper.py
More file actions
74 lines (63 loc) · 2.45 KB
/
Copy pathMongoHelper.py
File metadata and controls
74 lines (63 loc) · 2.45 KB
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
import pymongo
from config import DB_CONFIG, DEFAULT_SCORE
from db.ISqlHelper import ISqlHelper
class MongoHelper(ISqlHelper):
def __init__(self):
self.client = pymongo.MongoClient(DB_CONFIG['DB_CONNECT_STRING'], connect=False)
def init_db(self):
self.db = self.client.proxy
self.proxys = self.db.proxys
def drop_db(self):
self.client.drop_database(self.db)
def insert(self, value=None):
if value:
proxy = dict(ip=value['ip'], port=value['port'], types=value['types'], protocol=value['protocol'],
country=value['country'],
area=value['area'], speed=value['speed'], score=DEFAULT_SCORE)
self.proxys.insert(proxy)
def delete(self, conditions=None):
if conditions:
self.proxys.remove(conditions)
return ('deleteNum', 'ok')
else:
return ('deleteNum', 'None')
def update(self, conditions=None, value=None):
# update({"UserName":"libing"},{"$set":{"Email":"libing@126.com","Password":"123"}})
if conditions and value:
self.proxys.update(conditions, {"$set": value})
return {'updateNum': 'ok'}
else:
return {'updateNum': 'fail'}
def select(self, count=None, conditions=None):
if count:
count = int(count)
else:
count = 0
if conditions:
conditions = dict(conditions)
if 'count' in conditions:
del conditions['count']
conditions_name = ['types', 'protocol']
for condition_name in conditions_name:
value = conditions.get(condition_name, None)
if value:
conditions[condition_name] = int(value)
else:
conditions = {}
items = self.proxys.find(conditions, limit=count).sort(
[("speed", pymongo.ASCENDING), ("score", pymongo.DESCENDING)])
results = []
for item in items:
result = (item['ip'], item['port'], item['score'])
results.append(result)
return results
if __name__ == '__main__':
# from db.MongoHelper import MongoHelper as SqlHelper
# sqlhelper = SqlHelper()
# sqlhelper.init_db()
# # print sqlhelper.select(None,{'types':u'1'})
# items= sqlhelper.proxys.find({'types':0})
# for item in items:
# print item
# # # print sqlhelper.select(None,{'types':u'0'})
pass