-
-
Notifications
You must be signed in to change notification settings - Fork 280
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Bookmark support for storing Destination Drive IDs into database
- This improves the destination change support for cloning data by providing a bookmarked drive key - Change the authorize command of auth module from /authorize to /auth - Change the unauthorize command of auth module from /unauthorize to /unauth - Fix bugs - Tidy up
- Loading branch information
Showing
20 changed files
with
193 additions
and
133 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,10 @@ data* | |
.idea | ||
*.json | ||
*.pickle | ||
*.zip | ||
log.txt | ||
accounts/* | ||
drives.txt | ||
drive_list | ||
fly.toml | ||
/temp.py |
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
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
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,47 +1,64 @@ | ||
from pymongo import MongoClient | ||
from pymongo.errors import PyMongoError | ||
|
||
from bot import LOGGER, AUTHORIZED_USERS, DATABASE_URL | ||
from bot import LOGGER, AUTHORIZED_USERS, BOOKMARKS, DATABASE_URL | ||
|
||
class DatabaseHelper: | ||
|
||
def __init__(self): | ||
self.__err = False | ||
self.__client = None | ||
self.__db = None | ||
self.__collection = None | ||
self.__connect() | ||
|
||
def __connect(self): | ||
try: | ||
self.__client = MongoClient(DATABASE_URL) | ||
self.__db = self.__client['SearchX'] | ||
self.__collection = self.__db['users'] | ||
except PyMongoError as err: | ||
LOGGER.error(err) | ||
self.__err = True | ||
|
||
def auth_user(self, user_id: int): | ||
def load_db(self): | ||
if self.__err: | ||
return | ||
self.__collection.insert_one({"user_id": user_id}) | ||
return 'Authorization granted' | ||
if self.__db.users.find_one(): | ||
users_dict = self.__db.users.find().sort("user_id") | ||
for user in users_dict: | ||
AUTHORIZED_USERS.add(user["user_id"]) | ||
if self.__db.bms.find_one(): | ||
bms_dict = self.__db.bms.find().sort("drive_key") | ||
for bm in bms_dict: | ||
BOOKMARKS[bm["drive_key"]] = str(bm["drive_id"]) | ||
self.__client.close() | ||
|
||
def auth_user(self, user_id): | ||
if self.__err: | ||
return | ||
self.__db.users.insert_one({"user_id": user_id}) | ||
self.__client.close() | ||
return 'Authorization granted' | ||
|
||
def unauth_user(self, user_id: int): | ||
def unauth_user(self, user_id): | ||
if self.__err: | ||
return | ||
self.__collection.delete_many({"user_id": user_id}) | ||
self.__db.users.delete_one({"user_id": user_id}) | ||
self.__client.close() | ||
return 'Authorization revoked' | ||
|
||
def add_bm(self, drive_key, drive_id): | ||
if self.__err: | ||
return | ||
self.__db.bms.insert_one({"drive_key": drive_key, "drive_id": drive_id}) | ||
self.__client.close() | ||
return 'Bookmark added' | ||
|
||
def load_users(self): | ||
def remove_bm(self, drive_key): | ||
if self.__err: | ||
return | ||
users = self.__collection.find().sort("user_id") | ||
for user in users: | ||
AUTHORIZED_USERS.add(user["user_id"]) | ||
self.__db.bms.delete_one({"drive_key": drive_key}) | ||
self.__client.close() | ||
return 'Bookmark removed' | ||
|
||
if DATABASE_URL is not None: | ||
DatabaseHelper().load_users() | ||
DatabaseHelper().load_db() |
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
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
Oops, something went wrong.