Skip to content

Commit 0f73041

Browse files
[投稿] 添加脚本: 每日一词 — by 一只菜鸡
1 parent 6f4eaf9 commit 0f73041

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
from widget import Widget, family, SMALL, MEDIUM, LARGE
2+
import datetime
3+
import ujson
4+
import ssl
5+
6+
# 从网络获取古诗词名句
7+
def get_quote():
8+
try:
9+
import urllib.request
10+
11+
ctx = ssl.create_default_context()
12+
ctx.check_hostname = False
13+
ctx.verify_mode = ssl.CERT_NONE
14+
15+
opener = urllib.request.build_opener(
16+
urllib.request.HTTPSHandler(context=ctx)
17+
)
18+
19+
req = urllib.request.Request(
20+
'https://v1.jinrishici.com/all.json',
21+
headers={
22+
'Accept': 'application/json',
23+
'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS like Mac OS X) AppleWebKit/605.1.15'
24+
}
25+
)
26+
27+
response = opener.open(req, timeout=10)
28+
29+
if response.status == 200:
30+
raw_data = response.read()
31+
data = ujson.loads(raw_data)
32+
content = data['content']
33+
origin = data['origin']
34+
author = data['author']
35+
category = data['category']
36+
return content, f"「{origin}{author}", category
37+
except Exception as e:
38+
print(f"获取诗词失败: {e}")
39+
40+
return None, None, None
41+
42+
quote, author, category = get_quote()
43+
44+
# 配色方案
45+
bg = ("#F8FAFC", "#0F172A")
46+
card_bg = ("#F1F5F9", "#1E293B")
47+
primary = ("#0F172A", "#F8FAFC")
48+
secondary = ("#475569", "#94A3B8")
49+
weak = ("#94A3B8", "#64748B")
50+
accent = ("#8B5CF6", "#A78BFA")
51+
52+
w = Widget(background=bg, padding=14)
53+
54+
if family == SMALL:
55+
with w.vstack(spacing=0, align="center"):
56+
w.spacer()
57+
w.icon("pencil.and.outline", size=20, color=accent)
58+
w.spacer(6)
59+
w.text(quote if quote else "加载中...", size=15, weight="medium", color=primary,
60+
max_lines=3, align="center")
61+
w.spacer(4)
62+
w.text(author if author else "", size=11, color=secondary, align="center")
63+
w.spacer()
64+
65+
elif family == MEDIUM:
66+
with w.vstack(spacing=0):
67+
w.spacer()
68+
with w.hstack(spacing=0):
69+
with w.vstack(spacing=0, align="center"):
70+
w.spacer()
71+
w.icon("pencil.and.outline", size=28, color=accent)
72+
w.spacer(6)
73+
w.text("古诗词", size=11, color=weak)
74+
w.spacer()
75+
w.spacer(12)
76+
with w.vstack(spacing=4, align="leading"):
77+
w.spacer()
78+
w.text(quote if quote else "网络不可用", size=16, weight="medium", color=primary,
79+
max_lines=3)
80+
w.text(author if author else "", size=12, color=secondary)
81+
w.spacer()
82+
w.spacer(8)
83+
w.text(category if category else "", size=11, color=weak, align="trailing")
84+
w.spacer()
85+
86+
else:
87+
with w.vstack(spacing=0):
88+
w.spacer()
89+
with w.hstack(spacing=8):
90+
w.icon("pencil.and.outline", size=22, color=accent)
91+
w.text("每日诗词", size=18, weight="semibold",
92+
color=("#334155", "#E2E8F0"))
93+
w.spacer()
94+
w.text(datetime.datetime.now().strftime("%Y/%m/%d"), size=13, color=weak)
95+
w.spacer(16)
96+
97+
with w.card(background=card_bg, corner_radius=12, padding=16, spacing=0):
98+
w.text(quote if quote else "无法获取诗词,请检查网络连接", size=20, weight="medium", color=primary,
99+
max_lines=4)
100+
w.spacer(10)
101+
w.text(author if author else "", size=14, color=secondary)
102+
103+
w.spacer()
104+
105+
with w.hstack(spacing=0):
106+
w.icon("leaf.fill", size=12, color="#10B981")
107+
w.spacer(6)
108+
w.text(category if category else "诗意的栖居", size=12, color=weak)
109+
w.spacer()
110+
111+
w.spacer()
112+
113+
w.render()

0 commit comments

Comments
 (0)