-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
220 lines (200 loc) · 6.61 KB
/
app.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
import sqlite3
from http.server import HTTPServer, SimpleHTTPRequestHandler
import json
from urllib.parse import urlparse, parse_qs
def step_through_db():
global current_index
# Connect to SQLite database
conn = sqlite3.connect("search_results.db")
cursor = conn.cursor()
# Select all records from the upc_results table
cursor.execute("SELECT * FROM upc_results")
# Fetch all rows
rows = cursor.fetchall()
# Check if there are more rows to display
if current_index < len(rows):
row = rows[current_index]
# Retrieve the star rating for the current entry
cursor.execute("SELECT rating FROM ratings WHERE upc = ?", (row[0],))
rating_row = cursor.fetchone()
rating = rating_row[0] if rating_row else 0
# Retrieve the chili rating for the current entry
cursor.execute("SELECT rating FROM chili_ratings WHERE upc = ?", (row[0],))
chili_rating_row = cursor.fetchone()
chili_rating = chili_rating_row[0] if chili_rating_row else 0
# Format the current row's details
result = {
"upc": row[0],
"title": row[1],
"url": row[2],
"rating": rating,
"chiliRating": chili_rating,
}
current_index += 1
else:
result = {"message": "No more entries."}
# Close the connection
conn.close()
return json.dumps(result)
def reset_index():
global current_index
current_index = 0
return "Press 'Next Entry' to start stepping through the entries."
def save_rating(upc, rating):
# Connect to SQLite database
conn = sqlite3.connect("search_results.db")
cursor = conn.cursor()
# Create the ratings table if it doesn't exist
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS ratings (
upc TEXT PRIMARY KEY,
rating INTEGER
)
"""
)
# Insert or update the rating for the given UPC
cursor.execute(
"""
INSERT OR REPLACE INTO ratings (upc, rating)
VALUES (?, ?)
""",
(upc, rating),
)
# Commit the changes and close the connection
conn.commit()
conn.close()
def save_chili_rating(upc, rating):
# Connect to SQLite database
conn = sqlite3.connect("search_results.db")
cursor = conn.cursor()
# Create the chili_ratings table if it doesn't exist
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS chili_ratings (
upc TEXT PRIMARY KEY,
rating INTEGER
)
"""
)
# Insert or update the chili rating for the given UPC
cursor.execute(
"""
INSERT OR REPLACE INTO chili_ratings (upc, rating)
VALUES (?, ?)
""",
(upc, rating),
)
# Commit the changes and close the connection
conn.commit()
conn.close()
def get_comments(upc):
# Connect to SQLite database
conn = sqlite3.connect("search_results.db")
cursor = conn.cursor()
# Create the comments table if it doesn't exist
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS comments (
upc TEXT,
comment TEXT
)
"""
)
# Retrieve comments for the given UPC
cursor.execute("SELECT comment FROM comments WHERE upc = ?", (upc,))
comments = [row[0] for row in cursor.fetchall()]
# Close the connection
conn.close()
return json.dumps(comments)
def add_comment(upc, comment):
# Connect to SQLite database
conn = sqlite3.connect("search_results.db")
cursor = conn.cursor()
# Create the comments table if it doesn't exist
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS comments (
upc TEXT,
comment TEXT
)
"""
)
# Insert the comment for the given UPC
cursor.execute(
"""
INSERT INTO comments (upc, comment)
VALUES (?, ?)
""",
(upc, comment),
)
# Commit the changes and close the connection
conn.commit()
conn.close()
class RequestHandler(SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == "/":
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
with open("index.html", "rb") as file:
self.wfile.write(file.read())
elif self.path == "/step":
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(step_through_db().encode())
elif self.path == "/reset":
self.send_response(200)
self.send_header("Content-type", "text/plain")
self.end_headers()
self.wfile.write(reset_index().encode())
elif self.path.startswith("/comments"):
parsed_url = urlparse(self.path)
query_params = parse_qs(parsed_url.query)
upc = query_params.get("upc", [""])[0]
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(get_comments(upc).encode())
else:
self.send_error(404)
def do_POST(self):
if self.path == "/rating":
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length).decode("utf-8")
data = json.loads(post_data)
upc = data["upc"]
rating = data["rating"]
save_rating(upc, rating)
self.send_response(200)
self.end_headers()
elif self.path == "/chili-rating":
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length).decode("utf-8")
data = json.loads(post_data)
upc = data["upc"]
rating = data["rating"]
save_chili_rating(upc, rating)
self.send_response(200)
self.end_headers()
elif self.path == "/comments":
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length).decode("utf-8")
data = json.loads(post_data)
upc = data["upc"]
comment = data["comment"]
add_comment(upc, comment)
self.send_response(200)
self.end_headers()
else:
self.send_error(404)
def run_server(port=8000):
server_address = ("", port)
httpd = HTTPServer(server_address, RequestHandler)
print(f"Server running on port {port}")
httpd.serve_forever()
# Initialize the current index
current_index = 0
if __name__ == "__main__":
run_server()