diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/database/__init__.py | 0 | ||||
-rw-r--r-- | scripts/database/mongo.py | 5 | ||||
-rw-r--r-- | scripts/recipes/__init__.py | 0 | ||||
-rw-r--r-- | scripts/recipes/handle_recipes.py | 79 | ||||
-rw-r--r-- | scripts/scraping/__init__.py | 0 | ||||
-rw-r--r-- | scripts/scraping/scraper.py | 35 | ||||
-rw-r--r-- | scripts/users/__init__.py | 0 | ||||
-rw-r--r-- | scripts/users/handle_users.py | 30 |
8 files changed, 149 insertions, 0 deletions
diff --git a/scripts/database/__init__.py b/scripts/database/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/scripts/database/__init__.py diff --git a/scripts/database/mongo.py b/scripts/database/mongo.py new file mode 100644 index 0000000..cc4dffa --- /dev/null +++ b/scripts/database/mongo.py @@ -0,0 +1,5 @@ +from pymongo import MongoClient + +# Monogo +mongo_client = MongoClient("mongodb://localhost:27017/") +mongo_database = mongo_client["recipedb"] diff --git a/scripts/recipes/__init__.py b/scripts/recipes/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/scripts/recipes/__init__.py diff --git a/scripts/recipes/handle_recipes.py b/scripts/recipes/handle_recipes.py new file mode 100644 index 0000000..76faf86 --- /dev/null +++ b/scripts/recipes/handle_recipes.py @@ -0,0 +1,79 @@ +from bson.objectid import ObjectId +from scripts.database.mongo import mongo_database +from scripts.scraping.scraper import scrape + +mongo_collection = mongo_database["recipes"] + +def add_single_recipe(recipe): + added_recipe = mongo_collection.insert_one(recipe) + new_recipe = mongo_collection.find_one() + return added_recipe + +def recipe_exists(user, url): + recipe_exists = mongo_collection.find_one({"url": url, "user": user}) + if recipe_exists is not None: + return True + else: + return False + +def get_all_recipes(user): + all_recipes = mongo_collection.find({"user": user}).to_list() + return all_recipes + +def get_single_recipe(objectId): + single_recipe = mongo_collection.find_one({"_id": ObjectId(objectId) }) + return single_recipe + +def delete_single_recipe(objectId): + try: + mongo_collection.delete_one({"_id": ObjectId(objectId)}) + return {"success": True, "id": objectId} + except: + return {"success": False, "error": "Could not delete recipe"} + +def search_recipes(query_data, user): + query = query_data["q"] + facets_list = [] + + for item in query_data: + if "choice" in item: + facets_list.append(query_data[item]) + + if len(facets_list) > 0: + mongo_query = {"tags":{ "$all": facets_list}, "user": user, "title": { "$regex": query, "$options": "xi" }} + else: + mongo_query = {"title": { "$regex": query, "$options": "xi" }, "user": user} + + all_recipes = mongo_collection.find(mongo_query).to_list() + return all_recipes + +def get_facets(user, query_data=None): + if query_data is not None: + facets_list = [] + new_list = [] + query = query_data["q"] + for item in query_data: + if "choice" in item: + facets_list.append(query_data[item]) + + if len(facets_list) > 0: + mongo_query = {"tags":{ "$all": facets_list}, "user": user, "title": { "$regex": query, "$options": "xi" }} + else: + mongo_query = {"title": { "$regex": query, "$options": "xi" }, "user": user} + + facets = mongo_collection.distinct("tags", mongo_query) + + for facet in facets: + if facet in facets_list: + new_list.append({"name": facet, "checked": True}) + else: + new_list.append({"name": facet, "checked": False}) + + return new_list + + else: + new_list = [] + facets = mongo_collection.distinct("tags", {"user": user}) + for facet in facets: + new_list.append({"name": facet, "checked": False}) + return new_list diff --git a/scripts/scraping/__init__.py b/scripts/scraping/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/scripts/scraping/__init__.py diff --git a/scripts/scraping/scraper.py b/scripts/scraping/scraper.py new file mode 100644 index 0000000..50b46ae --- /dev/null +++ b/scripts/scraping/scraper.py @@ -0,0 +1,35 @@ +import requests +from urllib import parse +import json +from bs4 import BeautifulSoup + +def scrape(url, user_name): + 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') + inner_html = html.find('script', class_='yoast-schema-graph') + json_data = json.loads(inner_html.contents[0]) + graph_data = json_data["@graph"] + for i in graph_data: + if(i["@type"] == "Recipe"): + recipe = {} + instructions = [] + for instruction in i["recipeInstructions"]: + instructions.append(instruction["text"]) + keywords_list = i["keywords"].split(",") + tags = i["recipeCuisine"] + keywords_list + cleaned_tags = list(set([tag.strip().lower() for tag in tags])) + slug = parse.quote(i["name"]).lower() + + # The recipe + recipe["user"] = user_name + recipe["slug"] = slug + recipe["title"] = i["name"] + recipe["image"] = i["image"][0] + recipe["url"] = i["mainEntityOfPage"] + recipe["tags"] = cleaned_tags + recipe["ingredients"] = i["recipeIngredient"] + recipe["instructions"] = instructions + recipe["visible_by"] = ["jez"] + # recipe["encoded_url"] = urllib.parse.quote(i["name"]) + # Complete this all later!! + return recipe diff --git a/scripts/users/__init__.py b/scripts/users/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/scripts/users/__init__.py diff --git a/scripts/users/handle_users.py b/scripts/users/handle_users.py new file mode 100644 index 0000000..87ff7cd --- /dev/null +++ b/scripts/users/handle_users.py @@ -0,0 +1,30 @@ +from scripts.database.mongo import mongo_database +mongo_collection = mongo_database["users"] + +def user_exists(user): + try: + res = mongo_collection.find_one({"name": user}, {"password": 1}) + if res is None: + return {"success": True, "user": None} + else: + return {"success": True, "user": user, "password": res["password"]} + except: + return {"success": False, "error": "Request error, try again"} + +def authenticate_user(user, password_matches): + try: + res = mongo_collection.find_one({"name": user}) + if res is None: + return {"success": False, "error": "Password doesn't match, try again"} + else: + return {"success": True, "user": user} + except: + return {"success": False, "error": "Something went wrong matching your password"} + +def add_user(user, password): + try: + res = mongo_collection.insert_one({"name": user, "password": password}) + return {"success": True, "user": user} + except: + print("error") + |