写了个随机获取4K壁纸的API
API 使用了CloudFlare的Serverless架构,每日提供10W次调用。
(说成最高可以应对10W并发没毛病吧,嗯,怎么不算高并发呢?:doge)
API 信息
Request Method: GET
API URL:https://wallpaper.googlec.org
备注:每次请求会随机返回8张壁纸的Json信息
API 预览(以下为实时响应)
4K壁纸预览
Windows更换壁纸脚本
顺手写了一个Windows更换壁纸的Python脚本
import os
import requests
import ctypes
from PIL import Image
from io import BytesIO
import time
import random
def get_image_urls():
try:
response = requests.get("https://wallpaper.googlec.org")
if response.status_code == 200:
data = response.json()
print("Get API response successfully")
return [image_data["url"] for image_data in data["images"]]
else:
print("Failed to retrieve image URLs:", response.status_code)
return []
except Exception as e:
print("Error:", e)
return []
def get_random_image_url(image_urls):
if image_urls:
return random.choice(image_urls)
else:
return None
def download_image(url):
try:
response = requests.get(url)
if response.status_code == 200:
image = Image.open(BytesIO(response.content))
return image
else:
print("Failed to download image:", response.status_code)
return None
except Exception as e:
print("Error:", e)
return None
def save_image_to_desktop(image, filename):
try:
desktop_path = os.environ['USERPROFILE']
filepath = os.path.join(desktop_path, filename)
image.save(filepath, "JPEG")
return filepath
except Exception as e:
print("Error saving image:", e)
return None
def set_wallpaper(image_path):
try:
ctypes.windll.user32.SystemParametersInfoW(20, 0, image_path , 3)
print("Wallpaper set successfully.")
except Exception as e:
print("Error setting wallpaper:", e)
if __name__ == "__main__":
# Get image URLs from the API
image_urls = get_image_urls()
# Choose a random image URL
random_image_url = get_random_image_url(image_urls)
if random_image_url:
# Download the image
image = download_image(random_image_url)
if image:
# Save image to desktop
timestamp = time.strftime("%Y%m%d%H%M%S")
filename = f"wallpaper_{timestamp}.jpg"
image_path = save_image_to_desktop(image, filename)
if image_path:
# Set desktop wallpaper
set_wallpaper(image_path)
else:
print("No image URLs found.")
评论
匿名评论
隐私政策
你无需删除空行,直接评论以获取最佳展示效果