-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.py
More file actions
99 lines (83 loc) · 3.77 KB
/
database.py
File metadata and controls
99 lines (83 loc) · 3.77 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
import logging as log
from time import time
from typing import Optional
from pymongo import MongoClient
from models.Radio import Radio
from utils import is_database_enabled
from voice import Sound, SoundType
MONGO_DB_PROTOCOL = "mongodb://"
DATABASE_NAME = "ecibot"
STATS_COLLECTION = "stats"
RADIO_COLLECTION = "radio"
class Database:
def __init__(self, user: str, password: str, url: str) -> None:
if is_database_enabled():
self.__client = MongoClient(f"{MONGO_DB_PROTOCOL}{user}:{password}@{url}/")
self.__database = self.__client[DATABASE_NAME]
self.__stats_collection = self.__database[STATS_COLLECTION]
self.__radio_collection = self.__database[RADIO_COLLECTION]
def register_user_interaction_play_sound(self, username: str, sound: Sound):
sound_name = sound.get_name() if sound.get_sound_type() == SoundType.FILE else None
self.register_user_interaction(username, "play", sound_name)
def register_user_interaction(self, username: str, command: str, sound: Optional[str] = None) -> None:
try:
if is_database_enabled():
now = time() * 1000
item = {
"username": username,
"commandName": command,
"soundName": sound,
"timestamp": now
}
self.__stats_collection.insert_one(item)
except Exception as e:
log.error(f"bd >> Error while inserting user interaction", exc_info=e)
def register_radio(self, radio_name: str, radio_url: str):
try:
if is_database_enabled():
item = {
"name": radio_name,
"url": radio_url
}
self.__radio_collection.insert_one(item)
except Exception as e:
log.error(f"bd >> Error while inserting radio", exc_info=e)
def register_radio_list(self, radio_list: list[Radio], auto_fetched: bool = True):
try:
if is_database_enabled():
radio_dbo = map(
lambda radio: {"name": radio.get_radio_name(), "url": radio.get_radio_url(), "auto_fetched": auto_fetched},
radio_list
)
self.__radio_collection.insert_many(radio_dbo)
except Exception as e:
log.error(f"bd >> Error while inserting radio", exc_info=e)
def remove_radio(self, radio_name: str):
try:
if is_database_enabled():
self.__radio_collection.delete_one({"name": radio_name})
except Exception as e:
log.error(f"bd >> Error while removing radio", exc_info=e)
def get_radio(self, radio_name: str) -> Optional[Radio]:
try:
if is_database_enabled():
radio_raw = self.__radio_collection.find_one({"name": radio_name})
if radio_raw is not None:
return Radio(radio_raw["name"], radio_raw["url"])
return None
except Exception as e:
log.error(f"bd >> Error while looking for given radio. radio_name {radio_name}", exc_info=e)
def get_all_radios(self) -> Optional[list[Radio]]:
try:
if is_database_enabled():
radio_list_raw = self.__radio_collection.find({})
if radio_list_raw is not None:
return list(map(lambda radio_raw: Radio(radio_raw["name"], radio_raw["url"]), radio_list_raw))
return None
except Exception as e:
log.error(f"bd >> Error while fetching radios", exc_info=e)
def update_radio(self, radio_name: str, radio_url):
pass
if __name__ == "__main__":
db = Database("admin", "pass", "db")
db.register_user_interaction("wokis", "p", "a")