Bing 壁纸获取与自动更换

Bing 壁纸获取与自动更换
Photo by Cristina Gottardi / Unsplash
通过腾讯云 EdgeOne 边缘函数获取 Bing 每日壁纸,支持 UHD 超高清分辨率。边缘加速 + 缓存优化,响应速度快。

边缘函数实现

async function handleRequest(request) {
    try {
        // 获取 Bing 每日壁纸的 JSON 数据
        const bingResponse = await fetch('<https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN>');
        const bingData = await bingResponse.json();
        
        // 从返回的 JSON 中提取壁纸 URL 并替换为 UHD 版本
        const imageUrl = `https://www.bing.com${bingData.images[0].url.replace('1920x1080', 'UHD')}`;
        
        // 获取壁纸图像内容
        const response = await fetch(imageUrl);
        const seconds = 60*60;
        // 设置缓存时间为 1 小时
        response.headers.set('Cache-Control', `public, max-age=${seconds}, s-maxage=${seconds}`);
        return response;
    } catch (error) {
        // 错误处理
        return new Response(JSON.stringify({error: error.message}), {
            status: 500,
            headers: {
                'content-type': 'application/json; charset=UTF-8'
            }
        });
    }
}

addEventListener('fetch', event => {
    event.respondWith(handleRequest(event.request));
});

功能说明

  • 自动获取 Bing 官方每日壁纸
  • 自动替换为 UHD 超高清版本
  • 边缘缓存 1 小时,减少源站请求
  • 错误处理与 JSON 格式响应

EdgeOne 触发规则

在 EdgeOne 控制台配置以下规则,将特定路径的请求转发到边缘函数:

if ($host equal "img.amsdba.com"|"i.amsdba.com" and $url equal "/wallpaper")

访问地址

https://img.amsdba.com/wallpaper
https://i.amsdba.com/wallpaper

Bing 每日壁纸


应用场景

绿联 NAS 每日自动更换壁纸

通过定时任务自动下载最新壁纸,并应用到绿联 NAS 的桌面和登录界面。

自动更新脚本

#!/bin/bash

# 定义目标目录和文件路径
TARGET_DIR="/ugreen/wallpaper"
TARGET_FILE="${TARGET_DIR}/default_01.jpg"
TEMP_FILE=$(mktemp --suffix=.jpg)

# 确保临时文件创建成功
if [ ! -f "$TEMP_FILE" ]; then
    echo "Error: Failed to create temporary file"
    exit 1
fi

# 创建目标目录(如果不存在)
mkdir -p "$TARGET_DIR"

# 设置壁纸 API URL
WALLPAPER_URL="https://img.amsdba.com/wallpaper"

# 下载图片到临时文件
echo "Downloading Bing UHD wallpaper from $WALLPAPER_URL"
curl -fLk -o "$TEMP_FILE" "$WALLPAPER_URL" 

# 验证下载结果
if [ $? -eq 0 ] && [ -s "$TEMP_FILE" ]; then
    echo "Download successful, moving to final destination"
    # 移动文件到目标位置
    mv "$TEMP_FILE" "$TARGET_FILE"
    
    # 验证移动是否成功
    if [ $? -eq 0 ] && [ -f "$TARGET_FILE" ]; then
        echo "Bing wallpaper successfully saved to $TARGET_FILE"
        # 设置适当的权限
        chmod 644 "$TARGET_FILE"
        exit 0
    else
        echo "Error: Failed to move file to $TARGET_FILE"
        rm -f "$TEMP_FILE"
        exit 1
    fi
else
    echo "Error: Failed to download wallpaper"
    rm -f "$TEMP_FILE"
    exit 1
fi

保存脚本到:/root/bing-wallpaper.sh

配置定时任务

使用 crontab 添加计划任务,每天凌晨 0:15 自动更新壁纸:

# 编辑 crontab
crontab -e

# 添加以下任务
15 0 * * * /root/bing-wallpaper.sh

同步登录界面壁纸

让登录界面使用与桌面相同的壁纸:

# 删除原有登录界面壁纸
rm -rf /ugreen/wallpaper/login/custom.jpg

# 创建软链接到桌面壁纸
ln -s /ugreen/wallpaper/default_01.jpg /ugreen/wallpaper/login/custom.jpg

Ghost 博客主页动态背景

为 Ghost 博客首页设置每日自动更新的 Bing 壁纸作为模糊背景,提升视觉效果。

配置步骤

  1. 确认主题的 body class 名称
    • 在博客首页按 F12 打开浏览器开发者工具
    • 查看 <body> 标签的 class 属性
    • 找到用于标识首页的类名(如 home-templatehomepaged 等)
    • 不同主题的类名可能不同,需要根据实际情况调整
  2. 登录 Ghost 管理后台
  3. 进入 SettingsCode injection
  4. Site Header 区域添加以下代码(将 home-template 替换为你的主题实际使用的类名)

样式代码

<style>
/* 当body标签有home-template类时应用模糊背景 */
body.home-template,
body.post-template::before,
body.page-links {
    position: relative;
    min-height: 100vh;
    overflow-x: hidden; /* 防止横向滚动条 */
}

body.home-template::before,
body.post-template::before,
body.page-links::before {
    content: '';
    position: fixed; /* 使用fixed定位覆盖整个视口 */
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background-image: url('https://img.amsdba.com/wallpaper');
    background-size: cover;
    background-position: center;
    filter: blur(10px);
    z-index: -1; /* 放在最底层 */
}
body.post-template::before {
    filter: blur(70px); /* 文章页:强烈模糊 */
}
</style>

效果说明

  • 仅在首页显示背景(home-template 类)
  • 背景图片自动拉伸填充,居中显示
  • 应用 10px 高斯模糊效果
  • 固定定位,不随页面滚动
  • 不影响页面其他内容的正常显示

自定义选项

调整模糊程度:

filter: blur(15px);  /* 增强模糊效果 */
filter: blur(5px);   /* 减弱模糊效果 */