File size: 3,840 Bytes
623d61b 218d0d7 623d61b 218d0d7 623d61b 218d0d7 623d61b 218d0d7 623d61b 218d0d7 623d61b 218d0d7 623d61b 218d0d7 623d61b 218d0d7 bfc4e9f 623d61b 218d0d7 623d61b 218d0d7 623d61b bfc4e9f 623d61b bfc4e9f 623d61b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 | 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
|