forked from Joelkb/DQ-the-file-donor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
filters_mdb.py
162 lines (135 loc) · 4.47 KB
/
filters_mdb.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
import pymongo
from info import DATABASE_URI, DATABASE_NAME, SECONDDB_URI
from pyrogram import enums
import logging
from sample_info import tempDict
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
myclient = pymongo.MongoClient(DATABASE_URI)
mydb = myclient[DATABASE_NAME]
myclient2 = pymongo.MongoClient(SECONDDB_URI)
mydb2 = myclient2[DATABASE_NAME]
async def add_filter(grp_id, text, reply_text, btn, file, alert):
if tempDict['indexDB'] == DATABASE_URI:
mycol = mydb[str(grp_id)]
else:
mycol = mydb2[str(grp_id)]
# mycol.create_index([('text', 'text')])
data = {
'text':str(text),
'reply':str(reply_text),
'btn':str(btn),
'file':str(file),
'alert':str(alert)
}
try:
mycol.update_one({'text': str(text)}, {"$set": data}, upsert=True)
except:
logger.exception('Some error occured!', exc_info=True)
async def find_filter(group_id, name):
mycol = mydb[str(group_id)]
mycol2 = mydb2[str(group_id)]
query = mycol.find( {"text":name})
query2 = mycol2.find({"text": name})
# query = mycol.find( { "$text": {"$search": name}})
try:
for file in query:
reply_text = file['reply']
btn = file['btn']
fileid = file['file']
try:
alert = file['alert']
except:
alert = None
return reply_text, btn, alert, fileid
except:
try:
for file in query2:
reply_text = file['reply']
btn = file['btn']
fileid = file['file']
try:
alert = file['alert']
except:
alert = None
return reply_text, btn, alert, fileid
except:
return None, None, None, None
async def get_filters(group_id):
mycol = mydb[str(group_id)]
mycol2 = mydb2[str(group_id)]
texts = []
query = mycol.find()
query2 = mycol2.find()
try:
for file in query:
text = file['text']
texts.append(text)
except:
pass
try:
for file in query2:
text = file['text']
texts.append(text)
except:
pass
return texts
async def delete_filter(message, text, group_id):
mycol = mydb[str(group_id)]
mycol2 = mydb2[str(group_id)]
myquery = {'text':text }
query = mycol.count_documents(myquery)
query2 = mycol2.count_documents(myquery)
if query == 1:
mycol.delete_one(myquery)
await message.reply_text(
f"'`{text}`' deleted. I'll not respond to that filter anymore.",
quote=True,
parse_mode=enums.ParseMode.MARKDOWN
)
else:
if query2 == 1:
mycol2.delete_one(myquery)
await message.reply_text(
f"'`{text}`' deleted. I'll not respond to that filter anymore.",
quote=True,
parse_mode=enums.ParseMode.MARKDOWN
)
else:
await message.reply_text("Couldn't find that filter!", quote=True)
async def del_all(message, group_id, title):
if str(group_id) not in mydb.list_collection_names() and str(group_id) not in mydb2.list_collection_names():
await message.edit_text(f"Nothing to remove in {title}!")
return
mycol = mydb[str(group_id)]
mycol2 = mydb2[str(group_id)]
try:
mycol.drop()
mycol2.drop()
await message.edit_text(f"All filters from {title} has been removed")
except:
await message.edit_text("Couldn't remove all filters from group!")
return
async def count_filters(group_id):
mycol = mydb[str(group_id)]
mycol2 = mydb2[str(group_id)]
count = (mycol.count())+(mycol2.count())
return False if count == 0 else count
async def filter_stats():
collections = mydb.list_collection_names()
collections2 = mydb2.list_collection_names()
if "CONNECTION" in collections:
collections.remove("CONNECTION")
elif "CONNECTION" in collections2:
collections2.remove("CONNECTION")
totalcount = 0
for collection in collections:
mycol = mydb[collection]
count = mycol.count()
totalcount += count
for collection in collections2:
mycol2 = mydb2[collection]
count2 = mycol2.count()
totalcount += count2
totalcollections = len(collections)+len(collections2)
return totalcollections, totalcount