Download database.py from nexus4rd/README: direct link, hf CLI and curl.
- Browser
- Download file 3.84 kB
-
https://huggingface.co/spaces/nexus4rd/README/resolve/main/database.py
- Command line
-
hf download hf://spaces/nexus4rd/README/database.py
-
curl -L -o database.py https://huggingface.co/spaces/nexus4rd/README/resolve/main/database.py
3.84 kB
| from pymongo import MongoClient, errors | |
| from bson.objectid import ObjectId | |
| import logging | |
| class MongoDB: | |
| def __init__(self, uri, db_name): | |
| try: | |
| self.client = MongoClient(uri, serverSelectionTimeoutMS=5000) | |
| self.db = self.client[db_name] | |
| # Check the connection | |
| self.client.admin.command('ping') | |
| logging.info("Connected to MongoDB") | |
| except errors.ServerSelectionTimeoutError as err: | |
| logging.error(f"Failed to connect to MongoDB: {err}") | |
| raise | |
| def insert_video_detail(self, video_detail): | |
| try: | |
| result = self.db.videos.insert_one(video_detail) | |
| return str(result.inserted_id) | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error inserting video detail: {e}") | |
| return None | |
| def get_video_detail(self, video_id): | |
| try: | |
| video_detail = self.db.videos.find_one({"_id": ObjectId(video_id)}) | |
| return video_detail | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error retrieving video detail: {e}") | |
| return None | |
| def update_video_detail(self, video_id, update_data): | |
| try: | |
| result = self.db.videos.update_one({"_id": ObjectId(video_id)}, {"$set": update_data}) | |
| return result.modified_count | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error updating video detail: {e}") | |
| return None | |
| def delete_video_detail(self, video_id): | |
| try: | |
| result = self.db.videos.delete_one({"_id": ObjectId(video_id)}) | |
| return result.deleted_count | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error deleting video detail: {e}") | |
| return None | |
| def insert_chat_detail(self, chat_detail): | |
| try: | |
| result = self.db.chats.insert_one(chat_detail) | |
| return str(result.inserted_id) | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error inserting chat detail: {e}") | |
| return None | |
| def get_chat_detail(self, chat_id): | |
| try: | |
| chat_detail = self.db.chats.find_one({"_id": ObjectId(chat_id)}) | |
| return chat_detail | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error retrieving chat detail: {e}") | |
| return None | |
| def update_chat_detail(self, chat_id, update_data): | |
| try: | |
| result = self.db.chats.update_one({"_id": ObjectId(chat_id)}, {"$set": update_data}) | |
| return result.modified_count | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error updating chat detail: {e}") | |
| return None | |
| def delete_chat_detail(self, chat_id): | |
| try: | |
| result = self.db.chats.delete_one({"_id": ObjectId(chat_id)}) | |
| return result.deleted_count | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error deleting chat detail: {e}") | |
| return None | |
| def insert_api_key(self, user_id, api_key): | |
| try: | |
| result = self.db.api_keys.update_one( | |
| {"user_id": user_id}, | |
| {"$set": {"api_key": api_key}}, | |
| upsert=True | |
| ) | |
| return result.upserted_id or result.modified_count | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error inserting API key: {e}") | |
| return None | |
| def get_api_key(self, user_id): | |
| try: | |
| api_key_doc = self.db.api_keys.find_one({"user_id": user_id}) | |
| return api_key_doc["api_key"] if api_key_doc else None | |
| except errors.PyMongoError as e: | |
| logging.error(f"Error retrieving API key: {e}") | |
| return None | |