Spaces:
Sleeping
Sleeping
File size: 1,525 Bytes
8102d44 b4bb680 fb733c0 8102d44 b4bb680 8102d44 b4bb680 8102d44 b4bb680 8102d44 b4bb680 8102d44 b4bb680 |
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 |
import os
from datetime import datetime
from pymongo import MongoClient
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# MongoDB Configuration
MONGO_URI = os.getenv("MONGO_URI")
DB_NAME = os.getenv("MONGO_DB")
COLLECTION_NAME = "analysis_results"
# Initialize MongoDB client
client = MongoClient(MONGO_URI)
db = client[DB_NAME]
collection = db[COLLECTION_NAME]
def insert_analysis_result(
video_name: str,
offer_details: str,
target_audience: str,
specific_hook: str,
additional_context: str,
response: dict
):
"""Insert a new video analysis result"""
document = {
"video_name": video_name,
"offer_details": offer_details,
"target_audience": target_audience,
"specific_hook": specific_hook,
"additional_context": additional_context,
"response": response,
"created_at": datetime.utcnow()
}
result = collection.insert_one(document)
return str(result.inserted_id)
def get_all_results(limit: int = 20):
"""Fetch all saved results, sorted by newest"""
return list(collection.find().sort("created_at", -1).limit(limit))
def get_result_by_id(doc_id):
"""Fetch a specific analysis by _id"""
from bson import ObjectId
return collection.find_one({"_id": ObjectId(doc_id)})
def delete_result_by_id(doc_id):
"""Delete a specific analysis by _id"""
from bson import ObjectId
result = collection.delete_one({"_id": ObjectId(doc_id)})
return result.deleted_count > 0 |