Skip to content

Instantly share code, notes, and snippets.

@snowden-fu
Last active July 15, 2022 11:18
Show Gist options
  • Save snowden-fu/f29366d4855210c2e832942dc00f5599 to your computer and use it in GitHub Desktop.
Save snowden-fu/f29366d4855210c2e832942dc00f5599 to your computer and use it in GitHub Desktop.
download latest wallpaper from bing.cn
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
BING_WALLPAPER_API="https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=10&nc=1612409408851&pid=hp&FORM=BEHPTB&uhd=1&uhdwidth=3840&uhdheight=2160"
BING_URL = "https://cn.bing.com";
import sys
import requests
import json
import getpass
import os
if 'requests' not in sys.modules:
print('please install requests library first')
sys_username=getpass.getuser()
response = requests.get(BING_WALLPAPER_API)
data = json.loads(response.text)
img_caption = data['images'][0]['caption']
img_caption = img_caption.replace(',', '')
wallpaper_file_name = data['images'][0]['date']+img_caption+'.jpg'
wallpaper_file_name = wallpaper_file_name.replace(' ', '')
wallpaper_dir_path = "/Users/"+sys_username+"/Pictures/bing wallpaper/"
wallpaper_file_path = wallpaper_dir_path+wallpaper_file_name
wallpaper_url = BING_URL+data['images'][0]['url']
# before get, look insight whether image have been existed
f_list =os.listdir(wallpaper_dir_path)
if wallpaper_file_name in f_list:
print('wallpaper has been latest')
else:
wallpaper_data=requests.get(wallpaper_url)
with open(wallpaper_file_path, 'wb')as f:
f.write(wallpaper_data.content)
print("file downloaded as "+wallpaper_file_path)
if __name__ == "__main__":
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment