-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.py
More file actions
126 lines (98 loc) · 2.96 KB
/
main.py
File metadata and controls
126 lines (98 loc) · 2.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from flask import Flask, render_template, request, jsonify, send_from_directory
from pymongo import MongoClient
from dotenv import load_dotenv
import os
from scrape import scrapeCompareRaja, scrapeDetailPage
from predict import predictReview
load_dotenv()
# connect to mongo db
client = MongoClient(os.getenv('MONGO_URI'))
db = client['comparego']
productsCollection = db['productreviews']
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/search', methods=['GET'])
def search():
query = request.args.get('query')
results = scrapeCompareRaja(query)
return render_template('search.html', results=results)
# /details/id
@app.route('/details/<id>')
def details(id):
url = 'https://www.compareraja.in/' + id + '.html'
print(url)
results = scrapeDetailPage(url)
results['id'] = id
# get product reviews , and overall rating
productDetail = productsCollection.find_one({'id': id})
if productDetail:
productDetail['reviews'].reverse()
else:
productDetail = {
'reviews': [],
'overallRating': 0,
'reviewsCount': 0
}
return render_template('details.html', results=results, productDetail=productDetail)
# review structure
# {
# id: '123', # product id
# reviews: [
# {
# comment: 'good product',
# rating: 4,
# }
# ],
# overallRating: 4.5
# reviewsCount: 1
# }
@app.route('/api/review', methods=['POST'])
def addReview():
data = request.get_json()
id = data['id']
review = data['review']
# check if product exists
product = productsCollection.find_one({'id': id})
if product:
# update the product
updated_rating = (product['overallRating'] + review['rating']) / 2
productsCollection.update_one({'id': id}, {'$push': {
'reviews': review},
'$set': {
'overallRating': updated_rating},
'$inc': {
'reviewsCount': 1}
},
)
else:
# create the product
productsCollection.insert_one({
'id': id,
'reviews': [review],
'overallRating': review['rating'],
'reviewsCount': 1
})
return jsonify({'message': 'success'})
@app.route('/api/predict', methods=['POST'])
def predict():
data = request.get_json()
stars = predictReview(data['comment'])
return jsonify({'stars': stars})
# ---------------------logos---------------------
@app.route('/logo/fpk')
def fkp():
return send_from_directory('static', 'fpk.png')
@app.route('/logo/amzn')
def amzn():
return send_from_directory('static', 'amzn.png')
@app.route('/logo/tclck')
def tclck():
return send_from_directory('static', 'tclck.png')
# wildcard route for other than these
@app.route('/logo/<path:path>')
def send_logo(path):
return send_from_directory('static', "shopping.png")
if __name__ == '__main__':
app.run(debug=True)