-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscript_mna05y9m.py
More file actions
312 lines (262 loc) · 13.5 KB
/
Copy pathscript_mna05y9m.py
File metadata and controls
312 lines (262 loc) · 13.5 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# 92-95每日油价桌面小组件
# 数据来源:20121212.cn 油价接口
# 城市可自定义设置
# 注意:每次刷新都会从网络获取最新数据,无缓存
from widget import Widget, family, SMALL, MEDIUM, LARGE
import datetime
import json
# ═══════════════════════════════════════════════════════════
# 配置区 - 设置你所在的城市
# 支持城市:北京、上海、广州、深圳、杭州、南京、武汉、成都、重庆、西安、天津 等
# ═══════════════════════════════════════════════════════════
CITY_NAME = "北京" # ← 在这里修改城市名称
# ═══════════════════════════════════════════════════════════
# 油价API接口
# ═══════════════════════════════════════════════════════════
OIL_PRICE_API = "https://20121212.cn/ci/index.php/iol/like/"
def get_oil_price_from_api(city_name):
"""
从油价API获取数据
接口:https://20121212.cn/ci/index.php/iol/like/{城市名}
"""
try:
import requests
except ImportError:
return None
url = f"{OIL_PRICE_API}{city_name}"
try:
resp = requests.get(url, timeout=10)
text = resp.text
resp.close()
# 解析JSON数据
data = json.loads(text)
# 检查是否成功返回数据
if isinstance(data, list) and len(data) > 0:
item = data[0]
return {
"price92": float(item.get("iol92", 0)),
"price95": float(item.get("iol95", 0)),
"price98": float(item.get("iol98", 0)),
"price89": float(item.get("iol89", 0)),
"price0": float(item.get("iol0", 0)),
"price10": float(item.get("iol10", 0)),
"price20": float(item.get("iol20", 0)),
"price35": float(item.get("iol35", 0)),
"ton92": item.get("ton92", ""),
"ton95": item.get("ton95", ""),
"ton98": item.get("ton98", ""),
"update": item.get("date", ""),
"city_raw": item.get("city", ""),
}
except Exception as e:
print(f"API请求失败: {e}")
return None
def get_oil_price(city_name=None):
"""
获取油价数据(直接请求API,不使用缓存)
"""
# 使用配置的城市名
if not city_name:
city_name = CITY_NAME
# 直接从API获取
api_data = get_oil_price_from_api(city_name)
if api_data:
return api_data, city_name, True
# API不可用,返回失败状态
return None, city_name, False
# ═══════════════════════════════════════════════════════════
# 主程序 - 小组件渲染
# ═══════════════════════════════════════════════════════════
# 获取油价数据
oil_data, current_city, data_ok = get_oil_price()
now = datetime.datetime.now()
# 配色方案 - 暖白主题
BG_COLOR = ("#FFFBEB", "#1C1917")
CARD_BG = ("#FEF3C7", "#292524")
TITLE_COLOR = ("#78350F", "#FDE68A")
TEXT_COLOR = ("#92400E", "#D6D3D1")
SUB_COLOR = ("#B45309", "#78716C")
ACCENT_COLOR = ("#EA580C", "#FB923C")
ERROR_COLOR = ("#DC2626", "#F87171")
# 创建小组件
w = Widget(background=BG_COLOR, padding=14)
if family == SMALL:
# ═══ SMALL (127×127pt) ═══
# 极简设计:单行展示核心油价,居中显示
with w.vstack(spacing=0, align="center"):
w.spacer() # 顶部弹性
# 图标 + 标题
w.icon("fuelpump.fill", size=16, color=ACCENT_COLOR)
w.spacer(4)
w.text("油价", size=11, weight="medium", color=SUB_COLOR)
w.spacer(6)
# 核心数据
if data_ok and oil_data:
price92 = oil_data.get("price92", 0)
with w.hstack(spacing=8):
w.text("92#", size=12, weight="semibold", color=TEXT_COLOR)
w.text(f"¥{price92:.2f}", size=20, weight="bold",
design="rounded", color=TITLE_COLOR)
else:
w.text("N/A", size=20, weight="bold",
design="rounded", color=ERROR_COLOR)
w.spacer(4)
# 副标题
if data_ok:
w.text(current_city, size=10, color=SUB_COLOR)
else:
w.text("网络异常", size=10, color=ERROR_COLOR)
w.spacer() # 底部弹性
elif family == MEDIUM:
# ═══ MEDIUM (301×127pt) ═══
# 整体居中:标题行居中 + 卡片组居中
with w.vstack(spacing=0, align="center"):
w.spacer() # 顶部弹性
# 顶部标题行 - 居中
with w.hstack(spacing=6, align="center"):
w.icon("fuelpump.fill", size=14, color=ACCENT_COLOR)
w.text(f"{current_city}今日油价", size=14, weight="semibold",
color=TITLE_COLOR)
w.spacer()
if data_ok and oil_data and oil_data.get("update"):
update_date = oil_data.get("update", "")
try:
date_str = datetime.datetime.strptime(update_date[:10], "%Y-%m-%d").strftime("%m-%d")
except:
date_str = update_date[:10]
w.text(date_str, size=11, color=SUB_COLOR)
else:
w.text("加载中", size=11, color=ERROR_COLOR)
w.spacer() # 弹性撑开
# 主数据区 - 三列卡片居中
if data_ok and oil_data:
price92 = oil_data.get("price92", 0)
price95 = oil_data.get("price95", 0)
price98 = oil_data.get("price98", 0)
with w.hstack(spacing=6, align="center"):
# 左侧 92号
with w.card(background=CARD_BG, corner_radius=10,
padding=10, spacing=0):
with w.vstack(spacing=2, align="center"):
w.text("92#", size=11, weight="semibold", color=SUB_COLOR)
w.text(f"¥{price92:.2f}", size=20, weight="bold",
design="rounded", color=TITLE_COLOR)
w.text("元/升", size=9, color=SUB_COLOR)
# 中间 95号
with w.card(background=CARD_BG, corner_radius=10,
padding=10, spacing=0):
with w.vstack(spacing=2, align="center"):
w.text("95#", size=11, weight="semibold", color=SUB_COLOR)
w.text(f"¥{price95:.2f}", size=20, weight="bold",
design="rounded", color=TITLE_COLOR)
w.text("元/升", size=9, color=SUB_COLOR)
# 右侧 98号
with w.card(background=CARD_BG, corner_radius=10,
padding=10, spacing=0):
with w.vstack(spacing=2, align="center"):
w.text("98#", size=11, weight="semibold", color=SUB_COLOR)
w.text(f"¥{price98:.2f}", size=20, weight="bold",
design="rounded", color=TITLE_COLOR)
w.text("元/升", size=9, color=SUB_COLOR)
else:
# 网络异常时显示错误提示
with w.card(background=CARD_BG, corner_radius=10,
padding=16, spacing=0):
with w.vstack(spacing=4, align="center"):
w.icon("wifi.slash", size=24, color=ERROR_COLOR)
w.text("无法获取油价数据", size=13, weight="medium",
color=TEXT_COLOR)
w.text("请检查网络连接", size=11, color=SUB_COLOR)
w.spacer() # 底部弹性
else:
# ═══ LARGE (301×317pt) ═══
# 完整展示:头部+价格卡片+底部提示,全部居中
with w.vstack(spacing=0, align="center"):
w.spacer() # 顶部弹性
# 头部标题区 - 居中
with w.hstack(spacing=6, align="center"):
w.icon("fuelpump.fill", size=18, color=ACCENT_COLOR)
w.text(f"{current_city}今日油价", size=18, weight="semibold",
color=TITLE_COLOR)
w.spacer()
if data_ok and oil_data and oil_data.get("update"):
update_date = oil_data.get("update", "")
try:
date_str = datetime.datetime.strptime(update_date[:10], "%Y-%m-%d").strftime("%m月%d日")
except:
date_str = update_date[:10]
w.text(date_str, size=12, color=SUB_COLOR)
else:
w.text("加载中", size=12, color=ERROR_COLOR)
w.spacer(12)
if data_ok and oil_data:
price92 = oil_data.get("price92", 0)
price95 = oil_data.get("price95", 0)
price98 = oil_data.get("price98", 0)
price0 = oil_data.get("price0", 0)
update_date = oil_data.get("update", "")
# 价格卡片区 - 2x2布局,全部居中
with w.hstack(spacing=8, align="center"):
# 92号卡片
with w.card(background=CARD_BG, corner_radius=12, padding=12):
with w.vstack(spacing=2, align="center"):
w.text("92# 汽油", size=12, weight="medium", color=SUB_COLOR)
w.text(f"¥{price92:.2f}", size=26, weight="bold",
design="rounded", color=TITLE_COLOR)
w.text("元/升", size=10, color=SUB_COLOR)
# 95号卡片
with w.card(background=CARD_BG, corner_radius=12, padding=12):
with w.vstack(spacing=2, align="center"):
w.text("95# 汽油", size=12, weight="medium", color=SUB_COLOR)
w.text(f"¥{price95:.2f}", size=26, weight="bold",
design="rounded", color=TITLE_COLOR)
w.text("元/升", size=10, color=SUB_COLOR)
w.spacer(8)
with w.hstack(spacing=8, align="center"):
# 98号卡片
with w.card(background=CARD_BG, corner_radius=12, padding=12):
with w.vstack(spacing=2, align="center"):
w.text("98# 汽油", size=12, weight="medium", color=SUB_COLOR)
w.text(f"¥{price98:.2f}", size=26, weight="bold",
design="rounded", color=TITLE_COLOR)
w.text("元/升", size=10, color=SUB_COLOR)
# 0号柴油卡片
with w.card(background=CARD_BG, corner_radius=12, padding=12):
with w.vstack(spacing=2, align="center"):
w.text("0# 柴油", size=12, weight="medium", color=SUB_COLOR)
w.text(f"¥{price0:.2f}", size=26, weight="bold",
design="rounded", color=TITLE_COLOR)
w.text("元/升", size=10, color=SUB_COLOR)
w.spacer() # 弹性撑开
# 底部提示区 - 居中
with w.card(background=CARD_BG, corner_radius=10, padding=10):
with w.hstack(spacing=8, align="center"):
w.icon("info.circle.fill", size=14, color=ACCENT_COLOR)
with w.vstack(spacing=1):
w.text("数据来源:油价查询接口", size=10, color=SUB_COLOR)
if update_date:
w.text(f"更新时间 {update_date},实际价格以加油站为准",
size=10, color=SUB_COLOR)
else:
w.text("实际价格以加油站为准",
size=10, color=SUB_COLOR)
else:
# 网络异常时显示错误提示
with w.card(background=CARD_BG, corner_radius=12, padding=20):
with w.vstack(spacing=8, align="center"):
w.icon("wifi.slash", size=36, color=ERROR_COLOR)
w.text("无法获取油价数据", size=16, weight="semibold",
color=TEXT_COLOR)
w.text("请检查网络连接后刷新", size=12, color=SUB_COLOR)
w.spacer()
# 底部提示区
with w.card(background=CARD_BG, corner_radius=10, padding=10):
with w.hstack(spacing=8, align="center"):
w.icon("exclamationmark.triangle.fill", size=14, color=ERROR_COLOR)
with w.vstack(spacing=1):
w.text("提示", size=10, weight="medium", color=TEXT_COLOR)
w.text("确保网络连接正常,小组件将在下次刷新时重试",
size=10, color=SUB_COLOR)
w.spacer() # 底部弹性
# 渲染小组件
w.render()