-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_tool_v2.py
209 lines (166 loc) · 6.27 KB
/
select_tool_v2.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
import json
import toml
import mysql.connector
# Reading data
toml_data = toml.load(".streamlit/secrets.toml")
# saving each credential into a variable
# 將 toml_data 中的屬性取出
host = toml_data["mysql"]["host"]
port = toml_data["mysql"]["port"]
user = toml_data["mysql"]["username"]
passwd = toml_data["mysql"]["password"]
db = toml_data["mysql"]["database"]
charset = "utf8" # 或其他適當的字符集
def select_1(product_id):
conn = mysql.connector.connect(
host=host, port=port, user=user, passwd=passwd, db=db, charset=charset
)
print("Successfully connected!")
cursor = conn.cursor()
sql = f"""
select ID, 簡稱, 平均分數, 效果, 優點, 缺點, 推薦1, 推薦2, 推薦3 from items_table
where ID = {product_id};
"""
cursor.execute(sql)
data = cursor.fetchone()
cursor.close()
conn.close()
return data
def select_2(product_id, age_type):
conn = conn = mysql.connector.connect(
host=host, port=port, user=user, passwd=passwd, db=db, charset=charset
)
print("Successfully connected!")
cursor = conn.cursor()
sql = f"""
select {age_type}分數, {age_type}效果 from items_table
where ID = {product_id};
"""
cursor.execute(sql)
data = cursor.fetchone()
cursor.close()
conn.close()
return data
def stars_1(js, math):
star = {"type": "icon", "size": "lg", "url": "https://imgur.com/ZCwfMp0.png"}
starhelf = {"type": "icon", "size": "lg", "url": "https://imgur.com/eIiB8Qn.png"}
starlast = {
"type": "text",
"text": f"{math}",
"size": "sm",
"margin": "md",
"color": "#111111",
"offsetTop": "none",
"offsetBottom": "none",
"offsetStart": "none",
"offsetEnd": "none",
}
for i in range(int(math)):
js["body"]["contents"][2]["contents"].append(star)
num = int(math * 10 % 10)
if num >= 7 and num <= 9:
js["body"]["contents"][2]["contents"].append(star)
js["body"]["contents"][2]["contents"].append(starlast)
elif num >= 4 and num <= 6:
js["body"]["contents"][2]["contents"].append(starhelf)
js["body"]["contents"][2]["contents"].append(starlast)
else:
js["body"]["contents"][2]["contents"].append(starlast)
return js
def stars_2(js, math, info_number):
star = {"type": "icon", "size": "lg", "url": "https://imgur.com/ZCwfMp0.png"}
starhelf = {"type": "icon", "size": "lg", "url": "https://imgur.com/eIiB8Qn.png"}
starlast = {
"type": "text",
"text": f"{math}",
"size": "sm",
"margin": "md",
"color": "#111111",
"offsetTop": "none",
"offsetBottom": "none",
"offsetStart": "none",
"offsetEnd": "none",
}
for i in range(int(math)):
js["contents"][info_number]["body"]["contents"][2]["contents"].append(star)
num = int(math * 10 % 10)
if num >= 7 and num <= 9:
js["contents"][info_number]["body"]["contents"][2]["contents"].append(star)
js["contents"][info_number]["body"]["contents"][2]["contents"].append(starlast)
elif num >= 4 and num <= 6:
js["contents"][info_number]["body"]["contents"][2]["contents"].append(starhelf)
js["contents"][info_number]["body"]["contents"][2]["contents"].append(starlast)
else:
js["contents"][info_number]["body"]["contents"][2]["contents"].append(starlast)
return js
def load_js1(data):
with open("v1.json", mode="r", encoding="utf-8") as fi:
js = json.load(fi)
math = data[2]
js = stars_1(js, math)
# 修改 "hero" 中的 "url"
js["hero"]["url"] = image_dict[data[0]] # 抓
js["body"]["contents"][0]["text"] = mark[data[0]] # 品牌
js["body"]["contents"][1]["text"] = data[1] # 商品名稱
js["body"]["contents"][3]["contents"][0]["contents"][1]["text"] = data[3] # 效果
js["body"]["contents"][3]["contents"][1]["contents"][1]["text"] = data[4] # 優點
js["body"]["contents"][3]["contents"][1]["contents"][2]["contents"][1]["text"] = (
data[5]
) # 缺點
js["footer"]["contents"][1]["action"]["text"] = f"推薦:{data[1]}" # 推薦商品
# js["contents"]["footer"]["contents"][0]["action"]["uri"] = product_url_dict[data[0]]
js["footer"]["contents"][0]["action"]["uri"] = product_url_dict[
data[0]
] # 推薦商品三種 url
return js
def load_js2(data):
with open("v2.json", mode="r", encoding="utf-8") as fi:
js = json.load(fi)
for info_number in range(3):
math = data[info_number][2]
js = stars_2(js, math, info_number)
js["contents"][info_number]["hero"]["url"] = image_dict[data[info_number][0]]
js["contents"][info_number]["body"]["contents"][0]["text"] = mark[
data[info_number][0]
] # 品牌
js["contents"][info_number]["body"]["contents"][1]["text"] = data[info_number][
1
] # 商品名稱
js["contents"][info_number]["body"]["contents"][3]["contents"][0]["contents"][
1
]["text"] = data[info_number][
3
] # 效果
js["contents"][info_number]["body"]["contents"][3]["contents"][1]["contents"][
1
]["text"] = data[info_number][
4
] # 優點
js["contents"][info_number]["body"]["contents"][3]["contents"][1]["contents"][
2
]["contents"][1]["text"] = data[info_number][
5
] # 缺點
js["contents"][info_number]["footer"]["contents"][1]["action"][
"text"
] = f"推薦:{data[info_number][1]}" # 推薦商品
js["contents"][info_number]["footer"]["contents"][0]["action"]["uri"] = (
product_url_dict[data[info_number][0]]
)
return js
# 載入本機圖片路徑/face_rank
def push_db(id_tp):
conn = conn = mysql.connector.connect(
host=host, port=port, user=user, passwd=passwd, db=db, charset=charset
)
print("Successfully connected!")
cursor = conn.cursor()
sql = f"""
select ID, 簡稱, 平均分數, 效果, 優點, 缺點, 推薦1, 推薦2, 推薦3 from items_table
where ID = {id_tp[0]} or ID = {id_tp[1]} or ID = {id_tp[2]};
"""
cursor.execute(sql)
data = cursor.fetchall()
cursor.close()
conn.close()
return data