侧边栏壁纸
博主头像
Eoser's page! 博主等级

@学习@生活@自己

  • 累计撰写 114 篇文章
  • 累计创建 29 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

Python+Alist+阿里云实现静态Bing每日一图API

eoser
2023-04-09 / 0 评论 / 0 点赞 / 0 阅读 / 0 字

Alist

Alist是一个各种存储综合管理的而工具 1.用这个工具把阿里网盘挂载上 2.云盘中创建一个文件夹,将其挂载到根目录,不要把这个文件夹加到元信息,保证能外部访问 3.不管是内网穿透,还是公网IP,或者是通过动态IP。无论什么方法,确保能在公网访问能里的Alist。 4.创建你的bing图片存放的位置文件夹,且上传一张index.jpg,图片确认能够访问,访问地址为 http(s)://你的域名/d/Alist中的文件路径/index.jpg 确认OK后删除文件 5.将你的Alist挂载到你的服务器上,具体挂载方法自己网上搜索

python脚本实现图片更新

  • 获取图片信息的json数据:https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&uhd=1&uhdwidth=7680&uhdheight=4320
  • 我将Alist挂载到了 /webdav ,阿里网盘上我分享的目录是 api/bing
  • 每次更新后会将图片复制到云盘两份,一份带日期的,一份是index,这样还可以把每天的图都存下来
  • 记得将脚本加入定时任务执行
    # 打开定时任务编辑
    crontab -e

    这里是打开后要加的内容

    # 每天0点7分获取bing图片,上传到阿里云
    7 0 * * * /bin/python3 /root/scripts/bing_picture.py
  • 这里是 bing_picture.py 的具体代码

    #!/bin/python3
    import datetime
    import json
    import requests
    import os
    tmp_path="/tmp/index.jpg"
    backup_path="/webdav/api/bing"
    info_api="https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&uhd=1&uhdwidth=7680&uhdheight=4320"
    # 解析获取URL
    def get_image_url():
    res=requests.get(info_api)
    jres=json.loads(res.text)
    return "https://www.bing.com"+jres.get("images")[0].get("url")
    # 拷贝文件
    def copy_file(srcFile,destFile):
    # fs=open(srcFile,"rb+")
    # fd=open(destFile,"wb+")
    # fd.write(fs.read())
    # fs.close()
    # fd.close()
    os.popen("cp -f %s %s"%(srcFile,destFile))
    # 下载图片
    def download_picture(image_url):
    resp=requests.get(image_url,timeout=10)
    f=open(tmp_path,"wb+")
    f.write(resp.content)
    f.close()
    # 入口函数
    def main():
    download_picture(get_image_url())
    date_str=datetime.datetime.now().strftime("%Y%m%d")
    file_dir=datetime.datetime.now().strftime("%Y%m")
    os.popen("mkdir -p %s"%(backup_path+"/"+file_dir))
    
    copy_file(tmp_path,backup_path+"/index.jpg")
    copy_file(tmp_path,backup_path+"/"+file_dir+"/"+date_str+".jpg")
    # 入口
    if __name__ == "__main__":
    main()

将羊毛薅到底 302转发

  • Alist挂载时可以选择连接方式,这个分享目录不建议使用代理,建议选择302策略
  • 策略验证,可以通过浏览器访问图片的时候 F12 打开网络检测,观察访问的地址状态码会显示302,接着跳转一个阿里云的分布式链接地址。

bing每日一图

0

评论区