-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathnotesdb
More file actions
80 lines (65 loc) · 1.82 KB
/
Copy pathnotesdb
File metadata and controls
80 lines (65 loc) · 1.82 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
from PyrogramBot import db
from typing import Dict, List, Union
notesdb = db.notes
async def get_notes_count() -> dict:
chats = notesdb.find({"chat_id": {"$lt": 0}})
if not chats:
return {}
chats_count = 0
notes_count = 0
for chat in await chats.to_list(length=1000000000):
notes_name = await get_note_names(chat['chat_id'])
notes_count += len(notes_name)
chats_count += 1
return {
"chats_count": chats_count,
"notes_count": notes_count
}
async def _get_notes(chat_id: int) -> Dict[str, int]:
_notes = await notesdb.find_one({"chat_id": chat_id})
if _notes:
_notes = _notes["notes"]
else:
_notes = {}
return _notes
async def get_note_names(chat_id: int) -> List[str]:
_notes = []
for note in await _get_notes(chat_id):
_notes.append(note)
return _notes
async def get_note(chat_id: int, name: str) -> Union[bool, dict]:
name = name.lower().strip()
_notes = await _get_notes(chat_id)
if name in _notes:
return _notes[name]
else:
return False
async def save_note(chat_id: int, name: str, note: dict):
name = name.lower().strip()
_notes = await _get_notes(chat_id)
_notes[name] = note
await notesdb.update_one(
{"chat_id": chat_id},
{
"$set": {
"notes": _notes
}
},
upsert=True
)
async def delete_note(chat_id: int, name: str) -> bool:
notesd = await _get_notes(chat_id)
name = name.lower().strip()
if name in notesd:
del notesd[name]
await notesdb.update_one(
{"chat_id": chat_id},
{
"$set": {
"notes": notesd
}
},
upsert=True
)
return True
return False