Python——实现Windows桌面壁纸和bing背景的同步

目的

实现Windows桌面壁纸和bing背景的同步


如何实现

requests(获取url)

json(解析对象)

os.path(设置图片保存路径以及日志信息)

ctypes(设置Windows壁纸)


具体过程

API:https://api.iyk0.com/mryt/

{
    "code": 1,
    "msg": "获取成功!",
    "data": [
        {
            "title": "冬日里的科赫尔湖,德国巴伐利亚州 (© Reinhard Schmid/eStock Photo)",
            "imgurl": "https://cn.bing.com/th?id=OHR.LakeKochelsee_ZH-CN0004970986_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
            "time": "20220106"
        },
        {
            "title": "树干上的扁嘴山巨嘴鸟,厄瓜多尔 (© Tui De Roy/Minden Pictures)",
            "imgurl": "https://cn.bing.com/th?id=OHR.MountainToucan_ZH-CN9939482570_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
            "time": "20220105"
        },
        {
            "title": "安沙波利哥沙漠州立公园,美国加利福尼亚州 (© Tom Hogan/plainpicture)",
            "imgurl": "https://cn.bing.com/th?id=OHR.BorregoBadlands_ZH-CN9913349081_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
            "time": "20220104"
        },
        {
            "title": "汉密尔顿山顶的利克天文台,美国加利福尼亚州 (© Jeffrey Lewis/Tandem Stills + Motion)",
            "imgurl": "https://cn.bing.com/th?id=OHR.LickObservatory_ZH-CN9676762110_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
            "time": "20220103"
        },
        {
            "title": "雪后的布拉格,捷克共和国 (© benkrut/Getty Images)",
            "imgurl": "https://cn.bing.com/th?id=OHR.SnowyPrague_ZH-CN9794475183_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
            "time": "20220102"
        },
        {
            "title": "睡在海滩上的竖琴海豹,纽约长岛 (© Vicki Jauron, Babylon and Beyond Photography/Getty Images)",
            "imgurl": "https://cn.bing.com/th?id=OHR.JonesBeachHarpSeal_ZH-CN9584238333_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
            "time": "20220101"
        },
        {
            "title": "跨年夜的篝火晚会,冰岛雷克雅未克 (© Ragnar Th Sigurdsson/Alamy)",
            "imgurl": "https://cn.bing.com/th?id=OHR.IcelandBonfire_ZH-CN9270966209_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
            "time": "20211231"
        },
        {
            "title": "星空下的灯塔,德国Westerhever镇 (© Sandra Bartocha/Minden Pictures)",
            "imgurl": "https://cn.bing.com/th?id=OHR.WesterheverLight_ZH-CN6827035695_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp",
            "time": "20211230"
        }
    ]
}

使用requests调用该接口进行imgurl下载

def http_api():
    api = "https://api.iyk0.com/mryt/"
    res = requests.get(api).text
    return json.loads(res)['data']

创建bing壁纸类

class BingImg:
    def __init__(self, img_dir):
        self.title = img_dir["title"]
        self.img_url = img_dir["imgurl"]
        self.time = img_dir["time"]

下载壁纸到指定路径

def download_img(BingImg):
    res = requests.get(BingImg.img_url)
    if res.status_code == 200:
        open('.\\res\\' + BingImg.time + '.png', 'wb').write(res.content)
        open('.\\log.txt', 'a', encoding='utf-8').write(
            time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
            + '--' + str(BingImg.time) + '--' + str(BingImg.title) + '\n')

汇总imgurl

def get_img():
    arr = http_api()
    img_res = []
    for img_dir in arr:
        img_res.append(BingImg(img_dir))
    for i in img_res:
        print(i.title)
    return img_res

设置Windows壁纸

def set_img_as_wallpaper(filepath):
    ctypes.windll.user32.SystemParametersInfoW(20, 0, filepath, 0)

主函数

if __name__ == '__main__':
    print("©2021 LIN ")
    img = get_img()
    for img_i in img:
        download_img(img_i)
    set_img_as_wallpaper(os.path.abspath('./res') + '\\' + time.strftime('%Y%m%d', time.localtime()) + '.png')

源程序及相关注意事项

import ctypes
import json
import os.path
import time
import requests


def http_api():
    api = "https://api.iyk0.com/mryt/"
    res = requests.get(api).text
    return json.loads(res)['data']


class BingImg:
    def __init__(self, img_dir):
        self.title = img_dir["title"]
        self.img_url = img_dir["imgurl"]
        self.time = img_dir["time"]


def download_img(BingImg):
    res = requests.get(BingImg.img_url)
    if res.status_code == 200:
        open('.\\res\\' + BingImg.time + '.png', 'wb').write(res.content)
        open('.\\log.txt', 'a', encoding='utf-8').write(
            time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
            + '--' + str(BingImg.time) + '--' + str(BingImg.title) + '\n')


def get_img():
    arr = http_api()
    img_res = []
    for img_dir in arr:
        img_res.append(BingImg(img_dir))
    for i in img_res:
        print(i.title)
    return img_res


def set_img_as_wallpaper(filepath):
    ctypes.windll.user32.SystemParametersInfoW(20, 0, filepath, 0)


if __name__ == '__main__':
    print("©2021 LIN ")
    img = get_img()
    for img_i in img:
        download_img(img_i)
    set_img_as_wallpaper(os.path.abspath('./res') + '\\' + time.strftime('%Y%m%d', time.localtime()) + '.png')
    
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2021/10/16     18:50                res
-a----       2021/10/16     18:47           1309 bing.py
-a----       2021/10/16     18:50              3 log.txt
-a----       2021/10/16     18:51              0 readme.txt


该程序实现了Windows桌面壁纸和https://cn.bing.com/背景的同步,并保存了其壁纸资源

文件结构如上,res存放资源图片,bing.py进行图片下载和设置,log.txt记录日志,请不要更改文件结构

使用者只需双击bing.py即可(需要有python requests库~~~)

效果图

在这里插入图片描述
在这里插入图片描述

源码地址:https://wwi.lanzouy.com/i3LtEyfr4ej


注意事项

该程序使用相对路径作为壁纸的存储目录,耦合度低。并对壁纸下载操作进行了日志记录,日志包括存储时间和壁纸详情。但有一丢丢小问题——未优化下载的过程(其实是为了测试一次下载多张图片,懒得每天测试一次获取图片)。该程序只实现了代码设置壁纸同步,对于想Windows下自动同步的小伙伴们参考其他资料使用Windows定时任务来实现。

2022-01-06 10:44:51--20220106--冬日里的科赫尔湖,德国巴伐利亚州 (© Reinhard Schmid/eStock Photo)
2022-01-06 10:44:52--20220105--树干上的扁嘴山巨嘴鸟,厄瓜多尔 (© Tui De Roy/Minden Pictures)
2022-01-06 10:44:52--20220104--安沙波利哥沙漠州立公园,美国加利福尼亚州 (© Tom Hogan/plainpicture)
2022-01-06 10:44:52--20220103--汉密尔顿山顶的利克天文台,美国加利福尼亚州 (© Jeffrey Lewis/Tandem Stills + Motion)
2022-01-06 10:44:53--20220102--雪后的布拉格,捷克共和国 (© benkrut/Getty Images)
2022-01-06 10:44:53--20220101--睡在海滩上的竖琴海豹,纽约长岛 (© Vicki Jauron, Babylon and Beyond Photography/Getty Images)
2022-01-06 10:44:54--20211231--跨年夜的篝火晚会,冰岛雷克雅未克 (© Ragnar Th Sigurdsson/Alamy)
2022-01-06 10:44:54--20211230--星空下的灯塔,德国Westerhever镇 (© Sandra Bartocha/Minden Pictures)

---------------------------------------------------------------------------------------------------
贴一个我的文件结构 O(∩_∩)O
---------------------------------------------------------------------------------------------------
Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2021/10/16     18:50                res
-a----       2021/10/16     18:47           1309 bing.py
-a----       2021/10/16     18:50              3 log.txt
-a----       2021/10/16     18:51              0 readme.txt

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值