-
Notifications
You must be signed in to change notification settings - Fork 0
/
FirebaseData.py
90 lines (72 loc) · 2.75 KB
/
FirebaseData.py
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
import firebase_admin
from firebase_admin import credentials
from firebase_admin import db
class FirebaseData:
"""
Class to handle all things related to Firebase such as calls to add, update, or delete data
@github coolbrett
"""
def __init__(self):
"""
Code to run upon creation of FirebaseData object
"""
cred = credentials.Certificate("serviceAccountKey.json")
firebase_admin.initialize_app(cred, {
'databaseURL': 'https://fantasy-basketball-bot-default-rtdb.firebaseio.com/'
})
print("firebase connected")
def add_new_guild(self, data: dict) -> None:
"""
Add new guild to the database for the bot to recognize
@param data -> Key should be guild_id and value should be another object with guild_id, league_id, and private league creds if needed
"""
ref = db.reference('fbbot')
guild_ref = ref.child('guilds')
print(f"Add New Guild: data -> {data}")
guild_ref.set(data)
def update_guild(self, data: dict) -> None:
"""
THIS HASN'T BEEN TESTED -- Updates data stored at the key of the dict or JSON passed in
:param data: Key should be guild_id and value should be another object with guild_id, league_id, and private league creds if needed
"""
ref = db.reference('fbbot')
ref.child('guilds').update({data})
def delete():
"""
NOT WRITTEN YET - HAVEN'T NEEDED IT
"""
pass
def get_all_guild_ids(self):
"""
Gets all guild id's that are in firebase
"""
ref = db.reference('fbbot')
data = ref.get('guilds')
guild_ids = list()
if data[0] != None:
keys_as_list = list(data[0]['guilds'].keys())
for key in keys_as_list:
guild_ids.append(key)
print(f"FirebaseData guild_ids: {guild_ids}")
return guild_ids
def get_guild_information(self, guild_id: str) -> dict:
"""
Returns a dict containing information associated with guild id if it exists, otherwise returns None
"""
ref = db.reference('fbbot')
guilds = ref.get('guilds')[0]['guilds'][guild_id]
if self.__check_guild_existence(guild_id):
return guilds
def __check_guild_existence(self, key: str) -> bool:
"""
Helper function to check if a key already exists
"""
# create a reference to the desired location in the database
ref = db.reference('fbbot/guilds')
# get the value at the specified location
value = ref.get(key)
# check if the value is None
if value[0] is None:
return False
else:
return True