diff options
author | JJ <nicetry@noemail.com> | 2025-03-31 21:08:41 +0100 |
---|---|---|
committer | JJ <nicetry@noemail.com> | 2025-03-31 21:08:41 +0100 |
commit | 2e0b9c97af457da5c6afda611d48e59047d4cdb8 (patch) | |
tree | 199d10037f790bcade2ac86f478c10da77bbc771 /scripts | |
parent | 17529f38f4b4edf7249e18418ddfcc3f818a006d (diff) |
Basic AI functionality
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/database/mongo.py | 11 | ||||
-rw-r--r-- | scripts/recipes/handle_recipes.py | 2 | ||||
-rw-r--r-- | scripts/scraping/ai_scraping.py | 19 | ||||
-rw-r--r-- | scripts/scraping/scraper.py | 3 | ||||
-rw-r--r-- | scripts/users/handle_users.py | 10 |
5 files changed, 39 insertions, 6 deletions
diff --git a/scripts/database/mongo.py b/scripts/database/mongo.py index 3e97b0b..eaa2d97 100644 --- a/scripts/database/mongo.py +++ b/scripts/database/mongo.py @@ -3,7 +3,12 @@ from pymongo.server_api import ServerApi import Constants import certifi -uri = Constants.MONGO_CONNECTION_STRING +uri = Constants.MONGO_CONNECTION_STRING if Constants.MODE == "production" else Constants.MONGO_CONNECTION_STRING_DEV +database_str = "index-cooking" if Constants.MODE == "production" else "recipedb" -client = MongoClient(uri, server_api=ServerApi('1'), tlsCAFile=certifi.where()) -mongo_database = client["index-cooking"]
\ No newline at end of file +if Constants.MODE == "production": + client = MongoClient(uri, server_api=ServerApi('1'), tlsCAFile=certifi.where()) +else: + client = MongoClient(uri) + +mongo_database = client["index-cooking"] diff --git a/scripts/recipes/handle_recipes.py b/scripts/recipes/handle_recipes.py index 22ba0a4..73d19be 100644 --- a/scripts/recipes/handle_recipes.py +++ b/scripts/recipes/handle_recipes.py @@ -1,10 +1,12 @@ from bson.objectid import ObjectId +from flask import session from scripts.database.mongo import mongo_database from scripts.scraping.scraper import scrape mongo_collection = mongo_database.get_collection("recipes") def add_single_recipe(recipe): + recipe["user"] = session["username"] added_recipe = mongo_collection.insert_one(recipe) new_recipe = mongo_collection.find_one() return added_recipe diff --git a/scripts/scraping/ai_scraping.py b/scripts/scraping/ai_scraping.py new file mode 100644 index 0000000..b11a12b --- /dev/null +++ b/scripts/scraping/ai_scraping.py @@ -0,0 +1,19 @@ +from google import genai +import json +import requests +import Constants +import AIParams + +client = genai.Client(api_key="AIzaSyAdB7yo0qcnwHeC4T2rRaSXD588JRw94oQ") + +def run_ai_query(url): + req_url = f"https://r.jina.ai/{url}" + res = requests.get(req_url) + markdown_content = res.text + + prompt = AIParams.PROMPT.format(schema=AIParams.RECIPE_SCHEMA, markdown=markdown_content) + + ai_res = client.models.generate_content(model="gemini-2.0-flash", contents=prompt) + cleaned_text = ai_res.text.strip("```").strip("```json") + recipe_json = json.loads(cleaned_text) + return {"success": True, "data": recipe_json} diff --git a/scripts/scraping/scraper.py b/scripts/scraping/scraper.py index 8919d46..0e0b9c8 100644 --- a/scripts/scraping/scraper.py +++ b/scripts/scraping/scraper.py @@ -25,7 +25,7 @@ def extractInstructions(instructions): return returnedInstructions -def scrape(url, user_name): +def scrape(url): try: data = requests.get(url, headers= {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"}) html = BeautifulSoup(data.text, 'html.parser') @@ -43,7 +43,6 @@ def scrape(url, user_name): slug = parse.quote(i["name"]).lower() # The recipe - recipe["user"] = user_name recipe["slug"] = slug recipe["title"] = i["name"] recipe["image"] = i["image"][0] diff --git a/scripts/users/handle_users.py b/scripts/users/handle_users.py index 250c716..7a315f1 100644 --- a/scripts/users/handle_users.py +++ b/scripts/users/handle_users.py @@ -23,4 +23,12 @@ def delete_single_user(user): mongo_collection.delete_one({"name": user}) return {"success": True, "user": user} except: - return {"success": False, "user": user}
\ No newline at end of file + return {"success": False, "user": user} + +def update_user(user, user_prefs): + print(user, user_prefs, "in user handler") + try: + res = mongo_collection.update_one({"name": user}, {"$set": user_prefs}) + return {"success": True, "message": "user preferences successfully updated"} + except Exception as e: + return {"success": False, "message": str(e)} |