diff --git a/py/db.py b/py/db.py index a4405d1..3f66ac7 100644 --- a/py/db.py +++ b/py/db.py @@ -1,17 +1,20 @@ import hashlib import json +import os +import pycouchdb class DB: def __init__(self): self.database = {} - self.emails = [] def ensure_username(self, data): if hasattr(data, 'username'): return data.get['username'] elif hasattr(data, 'email'): - return self.emails[data.get['username']] + for index, entry in self.database: + if entry.get['email'] == data.get['email']: + return index @staticmethod def hash_password(password): @@ -24,10 +27,9 @@ class DB: password = data.get['password'] email = data.get['email'] hashed_password = self.hash_password(password) - user_data = {"hashed_password": hashed_password} + user_data = {"hashed_password": hashed_password, "email": email, "data": None} if username not in self.database: self.database[username] = user_data - self.emails[email] = username return True return False @@ -38,9 +40,6 @@ class DB: return False del self.database[username] - for i in self.emails: - if i == username: - del i self.save_database() return True @@ -84,12 +83,28 @@ class DB: return send_back def save_database(self): - with open("database.json", 'w') as file: - json.dump(self.database, file) + if os.environ.get('PRODUCTION') == "YES": + server = pycouchdb.Server("http://admin:admin@localhost:5984/") + db = server.database("interstellar_ai") + db.save(self.database) + + else: + with open("database.json", 'w') as file: + json.dump(self.database, file) def load_database(self): - try: - with open("database.json", 'r') as file: - self.database = json.load(file) - except FileNotFoundError: - pass + if os.environ.get('PRODUCTION') == "YES": + server = pycouchdb.Server("http://admin:admin@localhost:5984/") + db = server.database("interstellar_ai") + if db: + self.database = db + else: + server.create("interstellar_ai") + db = server.database("interstellar_ai") + db.save(self.database) + else: + try: + with open("database.json", 'r') as file: + self.database = json.load(file) + except FileNotFoundError: + pass diff --git a/py/requirements.txt b/py/requirements.txt index 3277ca6..6cd3616 100644 --- a/py/requirements.txt +++ b/py/requirements.txt @@ -9,4 +9,5 @@ SpeechRecognition PocketSphinx google-cloud-speech google-generativeai -python-weather \ No newline at end of file +python-weather +pycouchdb \ No newline at end of file