azils3 commited on
Commit
57b6dd6
·
verified ·
1 Parent(s): 94c8e6e

Update database/filters_mdb.py

Browse files
Files changed (1) hide show
  1. database/filters_mdb.py +66 -33
database/filters_mdb.py CHANGED
@@ -2,37 +2,42 @@ import pymongo
2
  from pyrogram import enums
3
  from info import DATABASE_URL, DATABASE_NAME
4
  import logging
 
5
  logger = logging.getLogger(__name__)
6
- logger.setLevel(logging.ERROR)
7
 
8
  myclient = pymongo.MongoClient(DATABASE_URL)
9
  mydb = myclient["ManualFilters"]
10
 
11
-
12
-
13
  async def add_filter(grp_id, text, reply_text, btn, file, alert):
 
 
 
 
14
  mycol = mydb[str(grp_id)]
15
- # mycol.create_index([('text', 'text')])
16
 
17
  data = {
18
- 'text':str(text),
19
- 'reply':str(reply_text),
20
- 'btn':str(btn),
21
- 'file':str(file),
22
- 'alert':str(alert)
23
  }
24
 
25
  try:
26
- mycol.update_one({'text': str(text)}, {"$set": data}, upsert=True)
27
- except:
28
- logger.exception('Some error occured!', exc_info=True)
29
-
30
-
31
  async def find_filter(group_id, name):
 
 
 
 
32
  mycol = mydb[str(group_id)]
33
 
34
- query = mycol.find( {"text":name})
35
- # query = mycol.find( { "$text": {"$search": name}})
36
  try:
37
  for file in query:
38
  reply_text = file['reply']
@@ -42,12 +47,19 @@ async def find_filter(group_id, name):
42
  alert = file['alert']
43
  except:
44
  alert = None
45
- return reply_text, btn, alert, fileid
46
- except:
 
 
 
 
47
  return None, None, None, None
48
-
49
 
50
  async def get_filters(group_id):
 
 
 
 
51
  mycol = mydb[str(group_id)]
52
 
53
  texts = []
@@ -56,15 +68,20 @@ async def get_filters(group_id):
56
  for file in query:
57
  text = file['text']
58
  texts.append(text)
59
- except:
60
- pass
61
- return texts
62
-
 
63
 
64
  async def delete_filter(message, text, group_id):
 
 
 
 
65
  mycol = mydb[str(group_id)]
66
 
67
- myquery = {'text':text }
68
  query = mycol.count_documents(myquery)
69
  if query == 1:
70
  mycol.delete_one(myquery)
@@ -73,35 +90,50 @@ async def delete_filter(message, text, group_id):
73
  quote=True,
74
  parse_mode=enums.ParseMode.MARKDOWN
75
  )
 
76
  else:
77
  await message.reply_text("Couldn't find that filter!", quote=True)
78
-
79
 
80
  async def del_all(message, group_id, title):
 
 
 
 
81
  if str(group_id) not in mydb.list_collection_names():
82
  await message.edit_text(f"Nothing to remove in {title}!")
 
83
  return
84
 
85
  mycol = mydb[str(group_id)]
86
  try:
87
  mycol.drop()
88
  await message.edit_text(f"All filters from {title} has been removed")
89
- except:
 
 
90
  await message.edit_text("Couldn't remove all filters from group!")
91
- return
92
-
93
 
94
  async def count_filters(group_id):
 
 
 
 
95
  mycol = mydb[str(group_id)]
96
 
97
- count = mycol.count()
98
  if count == 0:
 
99
  return False
100
  else:
 
101
  return count
102
 
103
-
104
  async def filter_stats():
 
 
 
 
105
  collections = mydb.list_collection_names()
106
 
107
  if "CONNECTION" in collections:
@@ -110,9 +142,10 @@ async def filter_stats():
110
  totalcount = 0
111
  for collection in collections:
112
  mycol = mydb[collection]
113
- count = mycol.count()
114
  totalcount += count
 
115
 
116
  totalcollections = len(collections)
117
-
118
- return totalcollections, totalcount
 
2
  from pyrogram import enums
3
  from info import DATABASE_URL, DATABASE_NAME
4
  import logging
5
+
6
  logger = logging.getLogger(__name__)
7
+ logger.setLevel(logging.INFO)
8
 
9
  myclient = pymongo.MongoClient(DATABASE_URL)
10
  mydb = myclient["ManualFilters"]
11
 
 
 
12
  async def add_filter(grp_id, text, reply_text, btn, file, alert):
13
+ """
14
+ Add a filter to a specific group.
15
+ """
16
+ logger.info(f"Adding filter for group {grp_id} with text: {text}.")
17
  mycol = mydb[str(grp_id)]
 
18
 
19
  data = {
20
+ 'text': str(text),
21
+ 'reply': str(reply_text),
22
+ 'btn': str(btn),
23
+ 'file': str(file),
24
+ 'alert': str(alert)
25
  }
26
 
27
  try:
28
+ mycol.update_one({'text': str(text)}, {"$set": data}, upsert=True)
29
+ logger.info(f"Filter added/updated for group {grp_id} with text: {text}.")
30
+ except Exception as e:
31
+ logger.exception(f"Error adding/updating filter for group {grp_id} with text: {text}: {e}")
32
+
33
  async def find_filter(group_id, name):
34
+ """
35
+ Find a filter in a specific group.
36
+ """
37
+ logger.info(f"Finding filter for group {group_id} with text: {name}.")
38
  mycol = mydb[str(group_id)]
39
 
40
+ query = mycol.find({"text": name})
 
41
  try:
42
  for file in query:
43
  reply_text = file['reply']
 
47
  alert = file['alert']
48
  except:
49
  alert = None
50
+ logger.info(f"Filter found for group {group_id} with text: {name}.")
51
+ return reply_text, btn, alert, fileid
52
+ logger.info(f"No filter found for group {group_id} with text: {name}.")
53
+ return None, None, None, None
54
+ except Exception as e:
55
+ logger.exception(f"Error finding filter for group {group_id} with text: {name}: {e}")
56
  return None, None, None, None
 
57
 
58
  async def get_filters(group_id):
59
+ """
60
+ Get all filters in a specific group.
61
+ """
62
+ logger.info(f"Getting all filters for group {group_id}.")
63
  mycol = mydb[str(group_id)]
64
 
65
  texts = []
 
68
  for file in query:
69
  text = file['text']
70
  texts.append(text)
71
+ logger.info(f"Retrieved {len(texts)} filters for group {group_id}.")
72
+ return texts
73
+ except Exception as e:
74
+ logger.exception(f"Error getting filters for group {group_id}: {e}")
75
+ return []
76
 
77
  async def delete_filter(message, text, group_id):
78
+ """
79
+ Delete a specific filter from a group.
80
+ """
81
+ logger.info(f"Deleting filter for group {group_id} with text: {text}.")
82
  mycol = mydb[str(group_id)]
83
 
84
+ myquery = {'text': text}
85
  query = mycol.count_documents(myquery)
86
  if query == 1:
87
  mycol.delete_one(myquery)
 
90
  quote=True,
91
  parse_mode=enums.ParseMode.MARKDOWN
92
  )
93
+ logger.info(f"Filter deleted for group {group_id} with text: {text}.")
94
  else:
95
  await message.reply_text("Couldn't find that filter!", quote=True)
96
+ logger.info(f"No filter found to delete for group {group_id} with text: {text}.")
97
 
98
  async def del_all(message, group_id, title):
99
+ """
100
+ Delete all filters from a specific group.
101
+ """
102
+ logger.info(f"Deleting all filters for group {group_id} titled: {title}.")
103
  if str(group_id) not in mydb.list_collection_names():
104
  await message.edit_text(f"Nothing to remove in {title}!")
105
+ logger.info(f"No filters to remove in group {group_id} titled: {title}.")
106
  return
107
 
108
  mycol = mydb[str(group_id)]
109
  try:
110
  mycol.drop()
111
  await message.edit_text(f"All filters from {title} has been removed")
112
+ logger.info(f"All filters removed from group {group_id} titled: {title}.")
113
+ except Exception as e:
114
+ logger.exception(f"Error removing all filters from group {group_id} titled: {title}: {e}")
115
  await message.edit_text("Couldn't remove all filters from group!")
 
 
116
 
117
  async def count_filters(group_id):
118
+ """
119
+ Count the number of filters in a specific group.
120
+ """
121
+ logger.info(f"Counting filters for group {group_id}.")
122
  mycol = mydb[str(group_id)]
123
 
124
+ count = mycol.count_documents({})
125
  if count == 0:
126
+ logger.info(f"No filters found in group {group_id}.")
127
  return False
128
  else:
129
+ logger.info(f"Found {count} filters in group {group_id}.")
130
  return count
131
 
 
132
  async def filter_stats():
133
+ """
134
+ Get statistics about all filters.
135
+ """
136
+ logger.info("Getting filter statistics.")
137
  collections = mydb.list_collection_names()
138
 
139
  if "CONNECTION" in collections:
 
142
  totalcount = 0
143
  for collection in collections:
144
  mycol = mydb[collection]
145
+ count = mycol.count_documents({})
146
  totalcount += count
147
+ logger.info(f"Collection {collection} has {count} filters.")
148
 
149
  totalcollections = len(collections)
150
+ logger.info(f"Total collections: {totalcollections}, Total filters: {totalcount}.")
151
+ return totalcollections, totalcount