Spaces:
Sleeping
Sleeping
| 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 |