-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathhuw_recommend.py
More file actions
96 lines (87 loc) · 3.96 KB
/
huw_recommend.py
File metadata and controls
96 lines (87 loc) · 3.96 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from flask import Flask, request, session, render_template, redirect, url_for, g
from flask_restful import Api, Resource, reqparse
import os
import psycopg2
from pymongo import MongoClient
from dotenv import load_dotenv
import thebestsql as bsql
app = Flask(__name__)
api = Api(app)
# We define these variables to (optionally) connect to an external MongoDB
# instance.
envvals = ["MONGODBUSER","MONGODBPASSWORD","MONGODBSERVER"]
dbstring = 'mongodb+srv://{0}:{1}@{2}/test?retryWrites=true&w=majority'
# Since we are asked to pass a class rather than an instance of the class to the
# add_resource method, we open the connection to the database outside of the
# Recom class.
load_dotenv()
if os.getenv(envvals[0]) is not None:
envvals = list(map(lambda x: str(os.getenv(x)), envvals))
client = MongoClient(dbstring.format(*envvals))
else:
client = MongoClient()
database = client.huwebshop
# Connecting to PostgreSQL
# def SQL_fetch_data(SQL):
# connection = psycopg2.connect(user="postgres",
# password="Floris09",
# host="localhost",
# port="5432",
# database="huwebshop")
# cursor = connection.cursor()
# cursor.execute(SQL)
# fetched_data = cursor.fetchall()
# cursor.close()
# connection.close()
# return fetched_data
class Recom(Resource):
""" This class represents the REST API that provides the recommendations for
the webshop. At the moment, the API simply returns a random set of products
to recommend."""
def get(self, profileid, count, recommendationtype):
connection = bsql.get_connection("Floris09", "huwebshop")
cursor = bsql.get_cursor(connection)
""" This function represents the handler for GET requests coming in
through the API. It currently returns a random sample of products. """
prodids = []
if recommendationtype == "popular":
cat = profileid
cat = cat.replace("-", " ")
cat = cat.replace(" en ", " & ")
print(cat)
data = bsql.select_data(cursor, f"SELECT * FROM andere_kochten_ook WHERE LOWER(category) = LOWER('{cat}');")
for row in data:
print(row)
prodids = [row[1], row[2], row[3], row[4]]
print(prodids)
if recommendationtype == "similar":
prod_id = profileid
data = bsql.select_data(cursor, f"SELECT * FROM soort_gelijke_producten WHERE productid = '{prod_id}';")
for row in data:
print(row)
prodids = [row[1], row[2], row[3], row[4]]
print(prod_id)
if recommendationtype == "behaviour":
data = bsql.select_data(cursor, f"SELECT * FROM passend_bij_uw_gedrag WHERE profileid = '{profileid}';")
for row in data:
print(row)
prodids = [row[1], row[2], row[3], row[4]]
if recommendationtype == "combination":
print(recommendationtype)
prod_id = profileid
data = bsql.select_data(cursor, f"SELECT * FROM combineert_goed_met WHERE productid = '{prod_id}'")
for row in data:
print(row)
prodids = [row[1], row[2], row[3], row[4]]
if recommendationtype == "personal":
data = bsql.select_data(cursor, f"SELECT * FROM public.persoonlijk_aangeboden WHERE profileid = '{profileid}';")
for row in data:
print(row)
prodids = [row[1], row[2], row[3], row[4]]
return prodids, 200
randcursor = database.products.aggregate([{ '$sample': { 'size': count } }])
prodids = list(map(lambda x: x['_id'], list(randcursor)))
return prodids, 200
# This method binds the Recom class to the REST API, to parse specifically
# requests in the format described below.
api.add_resource(Recom, "/<string:profileid>/<int:count>/<string:recommendationtype>")