-
Notifications
You must be signed in to change notification settings - Fork 492
Expand file tree
/
Copy pathmdb.py
More file actions
186 lines (138 loc) · 4.29 KB
/
mdb.py
File metadata and controls
186 lines (138 loc) · 4.29 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @sachin9742s
import re
import pymongo
from pymongo.errors import DuplicateKeyError
from marshmallow.exceptions import ValidationError
from config import DATABASE_URI, DATABASE_NAME
myclient = pymongo.MongoClient(DATABASE_URI)
mydb = myclient[DATABASE_NAME]
async def savefiles(docs, group_id):
mycol = mydb[str(group_id)]
try:
mycol.insert_many(docs, ordered=False)
except Exception:
pass
async def channelgroup(channel_id, channel_name, group_id, group_name):
mycol = mydb["ALL DETAILS"]
channel_details = {
"channel_id" : channel_id,
"channel_name" : channel_name
}
data = {
'_id': group_id,
'group_name' : group_name,
'channel_details' : [channel_details],
}
if mycol.count_documents( {"_id": group_id} ) == 0:
try:
mycol.insert_one(data)
except:
print('Some error occured!')
else:
print(f"files in '{channel_name}' linked to '{group_name}' ")
else:
try:
mycol.update_one({'_id': group_id}, {"$push": {"channel_details": channel_details}})
except:
print('Some error occured!')
else:
print(f"files in '{channel_name}' linked to '{group_name}' ")
async def ifexists(channel_id, group_id):
mycol = mydb["ALL DETAILS"]
query = mycol.count_documents( {"_id": group_id} )
if query == 0:
return False
else:
ids = mycol.find( {'_id': group_id} )
channelids = []
for id in ids:
for chid in id['channel_details']:
channelids.append(chid['channel_id'])
if channel_id in channelids:
return True
else:
return False
async def deletefiles(channel_id, channel_name, group_id, group_name):
mycol1 = mydb["ALL DETAILS"]
try:
mycol1.update_one(
{"_id": group_id},
{"$pull" : { "channel_details" : {"channel_id":channel_id} } }
)
except:
pass
mycol2 = mydb[str(group_id)]
query2 = {'channel_id' : channel_id}
try:
mycol2.delete_many(query2)
except:
print("Couldn't delete channel")
return False
else:
print(f"filters from '{channel_name}' deleted in '{group_name}'")
return True
async def deletealldetails(group_id):
mycol = mydb["ALL DETAILS"]
query = { "_id": group_id }
try:
mycol.delete_one(query)
except:
pass
async def deletegroupcol(group_id):
mycol = mydb[str(group_id)]
if mycol.count() == 0:
return 1
try:
mycol.drop()
except Exception as e:
print(f"delall group col drop error - {str(e)}")
return 2
else:
return 0
async def channeldetails(group_id):
mycol = mydb["ALL DETAILS"]
query = mycol.count_documents( {"_id": group_id} )
if query == 0:
return False
else:
ids = mycol.find( {'_id': group_id} )
chdetails = []
for id in ids:
for chid in id['channel_details']:
chdetails.append(
str(chid['channel_name']) + " ( <code>" + str(chid['channel_id']) + "</code> )"
)
return chdetails
async def countfilters(group_id):
mycol = mydb[str(group_id)]
query = mycol.count()
if query == 0:
return False
else:
return query
async def findgroupid(channel_id):
mycol = mydb["ALL DETAILS"]
ids = mycol.find()
groupids = []
for id in ids:
for chid in id['channel_details']:
if channel_id == chid['channel_id']:
groupids.append(id['_id'])
return groupids
async def searchquery(group_id, name):
mycol = mydb[str(group_id)]
filenames = []
filelinks = []
# looking for a better regex :(
pattern = name.lower().strip().replace(' ','.*')
raw_pattern = r"\b{}\b".format(pattern)
regex = re.compile(raw_pattern, flags=re.IGNORECASE)
query = mycol.find( {"file_name": regex} )
for file in query:
filename = "[" + str(file['file_size']//1048576) + "MB] " + file['file_name']
filenames.append(filename)
filelink = file['link']
filelinks.append(filelink)
return filenames, filelinks