Spaces:
Sleeping
Sleeping
import os | |
import firebase_admin | |
from firebase_admin import credentials | |
from firebase_admin import firestore | |
from datetime import datetime | |
from dotenv import load_dotenv | |
class FirebaseConnector(): | |
def __init__(self): | |
load_dotenv() | |
cert_path = self.get_credentials_path() | |
cred = credentials.Certificate(cert_path) | |
firebase_admin.initialize_app(cred) | |
self.db = self.get_db() | |
def get_db(self): | |
db = firestore.client() | |
return db | |
def get_credentials_path(self): | |
credentials_path = os.getenv('RS_FIREBASE_CREDENTIALS_PATH') | |
return credentials_path | |
def get_user(self): | |
user = os.getenv('RS_WORDLE_USER') | |
return user | |
def get_state_from_fb_result(self, firebase_result): | |
result_number_map = {'incorrect': '0', | |
'misplaced': '1', | |
'correct': '2'} | |
char_result_map = map( | |
lambda char_res: result_number_map[char_res], firebase_result | |
) | |
return ''.join(char_result_map) | |
def today(self): | |
return datetime.today().strftime('%Y%m%d') | |
def today_user_results(self): | |
daily_results_col = 'dailyResults' | |
currentUser = self.get_user() | |
# Execute the query and get the first result | |
docs = self.db.collection(daily_results_col).where( | |
'user.email', '==', currentUser).where( | |
'date', '==', self.today()).limit(1).get() | |
return docs | |
def today_user_attempts(self): | |
docs = self.today_user_results() | |
attempted_words = [] | |
if len(docs) > 0: | |
doc = docs[0] | |
attempted_words = doc.to_dict().get('attemptedWords') | |
return attempted_words | |
def today_word(self): | |
words_col = 'words' | |
doc = self.db.collection(words_col).document(self.today()) | |
return doc.get().get('word') | |