Update src/database.py
Browse files- src/database.py +57 -0
src/database.py
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from datetime import datetime
|
| 3 |
+
from pymongo import MongoClient
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
# MongoDB Configuration
|
| 10 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
| 11 |
+
DB_NAME = os.getenv("MONGO_DB")
|
| 12 |
+
COLLECTION_NAME = os.getenv("MONGO_COLLECTION")
|
| 13 |
+
|
| 14 |
+
# Initialize MongoDB client
|
| 15 |
+
client = MongoClient(MONGO_URI)
|
| 16 |
+
db = client[DB_NAME]
|
| 17 |
+
collection = db[COLLECTION_NAME]
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def insert_analysis_result(
|
| 21 |
+
video_name: str,
|
| 22 |
+
offer_details: str,
|
| 23 |
+
target_audience: str,
|
| 24 |
+
specific_hook: str,
|
| 25 |
+
additional_context: str,
|
| 26 |
+
response: dict
|
| 27 |
+
):
|
| 28 |
+
"""Insert a new video analysis result"""
|
| 29 |
+
document = {
|
| 30 |
+
"video_name": video_name,
|
| 31 |
+
"offer_details": offer_details,
|
| 32 |
+
"target_audience": target_audience,
|
| 33 |
+
"specific_hook": specific_hook,
|
| 34 |
+
"additional_context": additional_context,
|
| 35 |
+
"response": response,
|
| 36 |
+
"created_at": datetime.utcnow()
|
| 37 |
+
}
|
| 38 |
+
result = collection.insert_one(document)
|
| 39 |
+
return str(result.inserted_id)
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def get_all_results(limit: int = 20):
|
| 43 |
+
"""Fetch all saved results, sorted by newest"""
|
| 44 |
+
return list(collection.find().sort("created_at", -1).limit(limit))
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def get_result_by_id(doc_id):
|
| 48 |
+
"""Fetch a specific analysis by _id"""
|
| 49 |
+
from bson import ObjectId
|
| 50 |
+
return collection.find_one({"_id": ObjectId(doc_id)})
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def delete_result_by_id(doc_id):
|
| 54 |
+
"""Delete a specific analysis by _id"""
|
| 55 |
+
from bson import ObjectId
|
| 56 |
+
result = collection.delete_one({"_id": ObjectId(doc_id)})
|
| 57 |
+
return result.deleted_count > 0
|