-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsearchbar.py
executable file
·80 lines (62 loc) · 1.79 KB
/
searchbar.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
#!/usr/bin/env python
"""
module docstring
"""
import pymysql
import os
import dotenv
import walmart
import buycott
dotenv.load_dotenv('.env')
db_connect = pymysql.connect(host=os.getenv('HOST'), port=3306, user=os.getenv('USER'), passwd=os.getenv('PASSWORD'), db=os.getenv('DB') )
''' FIXME: Line 29 Have buycott.scrape() return None if no results found
else:
result = buycott.scrape(upc)
if result:
brandName, name = resul
'''
def update_database(cur, upc):
result = walmart.callAPI(upc)
name = None
if result:
brandName, walmartPrice, name = result
if name:
if not brandName:
brandName = ""
if not walmartPrice:
walmartPrice = ""
cur.execute('INSERT INTO Item (Brand, WalmartPrice, Name, UPC) VALUES (\"' + brandName + '\",\"' + walmartPrice + '\",\"' + name + '\",\"' + upc + "\");");
db_connect.commit()
return True
return False
def query(param, crit):
if not param:
return None
cur = db_connect.cursor()
if crit == "UPC":
cur.execute("SELECT * FROM Item WHERE UPC = \"" + param + "\"")
item_data = cur.fetchall()
print(item_data)
if not item_data:
# Fetch new data and try again
if update_database(cur, param):
cur.execute("SELECT * FROM Item WHERE UPC = \"" + param + "\"")
item_data = cur.fetchall()
elif crit == "Name":
cur.execute("SELECT * FROM Item WHERE Name LIKE \"%" + param.strip() + "%\"")
item_data = cur.fetchall()
for info in item_data:
if info[0] == None:
brandName = "Unknown"
if info[1] == None:
walmartPrice = "Unknown"
if info[2] == None:
costcoPrice = "Unknown"
if info[3] == None:
name = "Unknown"
if info[4] == None:
upc = "Unknown"
cur.close()
return item_data
if __name__ == "__main__":
pass