forked from JHying/ChatGPT-Line-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request TheExplainthis#21 from TheExplainthis/develop
Add MongoDB
- Loading branch information
Showing
3 changed files
with
76 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import os | ||
|
||
from pymongo import MongoClient | ||
|
||
|
||
class MongoDB(): | ||
""" | ||
Environment Variables: | ||
MONGODB__PATH | ||
MONGODB__DBNAME | ||
""" | ||
client: None | ||
db: None | ||
|
||
def connect_to_database(self, mongo_path=None, db_name=None): | ||
mongo_path = mongo_path or os.getenv('MONGODB__PATH') | ||
db_name = db_name or os.getenv('MONGODB__DBNAME') | ||
self.client = MongoClient(mongo_path) | ||
assert self.client.config.command('ping')['ok'] == 1.0 | ||
self.db = self.client[db_name] | ||
|
||
|
||
mongodb = MongoDB() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,54 @@ | ||
import json | ||
import datetime | ||
|
||
|
||
class Storage(): | ||
class FileStorage: | ||
def __init__(self, file_name): | ||
self.fine_name = file_name | ||
self.history = {} | ||
|
||
def save(self, data): | ||
self.history.update(data) | ||
with open(self.fine_name, 'w', newline='') as f: | ||
json.dump(data, f) | ||
json.dump(self.history, f) | ||
|
||
def load(self): | ||
with open(self.fine_name, newline='') as jsonfile: | ||
data = json.load(jsonfile) | ||
return data | ||
self.history = data | ||
return self.history | ||
|
||
|
||
class MongoStorage: | ||
def __init__(self, db): | ||
self.db = db | ||
|
||
def save(self, data): | ||
user_id, api_key = list(data.items())[0] | ||
self.db['api_key'].update_one({ | ||
'user_id': user_id | ||
}, { | ||
'$set': { | ||
'user_id': user_id, | ||
'api_key': api_key, | ||
'created_at': datetime.datetime.utcnow() | ||
} | ||
}, upsert=True) | ||
|
||
def load(self): | ||
data = list(self.db['api_key'].find()) | ||
res = {} | ||
for i in range(len(data)): | ||
res[data[i]['user_id']] = data[i]['api_key'] | ||
return res | ||
|
||
|
||
class Storage: | ||
def __init__(self, storage): | ||
self.storage = storage | ||
|
||
def save(self, data): | ||
self.storage.save(data) | ||
|
||
def load(self): | ||
return self.storage.load() |