Skip to content

Commit 9f445ea

Browse files
committed
♻️ refactor: update Bing image API fetching method
1 parent 894f9f7 commit 9f445ea

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ https://notion-widgets.rylan.cn/poem
3131

3232
### 🔮 英文引言
3333

34-
#### 🎁 致谢 [Quotesy](https://github.com/dwyl/quotes/) 库和 [BitURL](https://biturl.top/) 开放 API
34+
#### 🎁 致谢 [Quotesy](https://github.com/dwyl/quotes/) 库和 [Bing 每日壁纸](https://www.bing.com/) 开放 API
3535

3636
```
3737
https://notion-widgets.rylan.cn/quote

server/routes.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
88

99
const router = express.Router();
1010

11+
/* ========== 页面 ========== */
12+
1113
// 首页
1214
router.get("/", (_, res) => {
1315
res.sendFile(path.join(__dirname, "../views/index.html"));
@@ -26,7 +28,9 @@ router.get("/music", (_, res) => {
2628
res.sendFile(path.join(__dirname, "../views/widgets/music-player.html"));
2729
});
2830

29-
// API
31+
/* ========== API ========== */
32+
33+
// 随机引言
3034
router.get("/api/quote", (_, res) => {
3135
const quote = quotes.random();
3236
res.json({
@@ -35,4 +39,14 @@ router.get("/api/quote", (_, res) => {
3539
});
3640
});
3741

42+
// Bing 每日壁纸
43+
router.get("/api/bing-image", async (_, res) => {
44+
const bingResponse = await fetch("https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN");
45+
const bingData = await bingResponse.json();
46+
const bingImageUrl = bingData.images[0].url;
47+
res.json({
48+
url: `https://www.bing.com/${bingImageUrl}`
49+
});
50+
});
51+
3852
export default router;

vercel.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"dest": "/views/index.html"
2121
},
2222
{
23-
"src": "/api/quote",
23+
"src": "/api/(.*)",
2424
"dest": "/server/app.js"
2525
},
2626
{

views/widgets/english-quote.html

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@
8080

8181
<script>
8282
const urlParams = new URLSearchParams(window.location.search);
83-
let bg = "";
8483

8584
function applyParmToStyle() {
8685
const size = urlParams.get("size");
@@ -92,8 +91,6 @@
9291
if (width) {
9392
document.documentElement.style.setProperty("--primary-width", `${width}px`);
9493
}
95-
96-
bg = urlParams.get("bg") || "";
9794
}
9895

9996
/**
@@ -135,26 +132,23 @@
135132

136133
async function fetchImage() {
137134
const imgContainer = document.querySelector(".img-container");
138-
let imageUrl = "";
139135

140136
const tryFetchImage = async () => {
141137
try {
142-
const bingApi = "https://bing.biturl.top/";
143-
const response = await fetch(bingApi);
144-
const data = await response.json();
145-
imageUrl = await data.url;
138+
const bingResponse = await fetch('/api/bing-image');
139+
const bingData = await bingResponse.json();
140+
return bingData.url;
146141
} catch (error) {
147142
console.error("Error fetching image", error);
148143
}
149144
};
150145

151-
if (!bg) {
152-
await tryFetchImage();
153-
} else {
154-
imageUrl = bg;
146+
let imageUrl = urlParams.get("bg");
147+
if (!imageUrl) {
148+
imageUrl = await tryFetchImage();
155149
}
156-
157150
calculateBrightness(imageUrl);
151+
158152
imgContainer.style.backgroundSize = "cover";
159153
imgContainer.style.backgroundPosition = "center";
160154
imgContainer.style.backgroundImage = `url('${imageUrl}')`;

0 commit comments

Comments
 (0)