forked from subinps/VCPlayerBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
85 lines (68 loc) · 2.67 KB
/
database.py
File metadata and controls
85 lines (68 loc) · 2.67 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
#!/usr/bin/env python3
# Copyright (C) @subinps
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from logger import LOGGER
import motor.motor_asyncio
from config import Config
class Database:
def __init__(self):
self._client = motor.motor_asyncio.AsyncIOMotorClient(Config.DATABASE_URI)
self.db = self._client[Config.DATABASE_NAME]
self.col = self.db.config
self.playlist = self.db.playlist
def new_config(self, name, value):
return dict(
name = name,
value = value,
)
def add_config(self, name, value):
config = self.new_config(name, value)
self.col.insert_one(config)
def new_song(self, id_, song):
return dict(
id = id_,
song = song,
)
def add_to_playlist(self, id_, song):
song_ = self.new_song(id_, song)
self.playlist.insert_one(song_)
async def is_saved(self, name):
config = await self.col.find_one({'name':name})
return True if config else False
async def edit_config(self, name, value):
await self.col.update_one({'name': name}, {'$set': {'value': value}})
async def get_config(self, name):
config = await self.col.find_one({'name':name})
return config.get('value')
async def del_config(self, name):
await self.col.delete_one({'name':name})
return
async def is_in_playlist(self, id_):
song = await self.playlist.find_one({'id':id_})
return True if song else False
async def get_song(self, id_):
song_ = await self.playlist.find_one({'id':id_})
return song_.get('song')
async def del_song(self, id_):
await self.playlist.delete_one({'id':id_})
return
async def clear_playlist(self):
await self.playlist.drop()
return
async def get_playlist(self):
k = self.playlist.find({})
l=[]
async for song in k:
song_ = {int(k):v for k,v in song['song'].items()}
l.append(song_)
return l
db=Database()