写了个随机获取4K壁纸的API

发布于:2024-04-30 ⋅ 阅读:(24) ⋅ 点赞:(0)

API 使用了CloudFlare的Serverless架构,每日提供10W次调用。

(说成最高可以应对10W并发没毛病吧,嗯,怎么不算高并发呢?:doge)

image.png

API 信息

Request Method: GET

API URL:https://wallpaper.googlec.org

Response body:

{
  "images": [
    {
      "startdate": "20240424",
      "fullstartdate": "202404241600",
      "enddate": "20240425",
      "url": "https://cn.bing.com/th?id=OHR.PenguinDirections_ZH-CN8498684753_UHD.jpg",
      "urlbase": "/th?id=OHR.PenguinDirections_ZH-CN8498684753",
      "copyright": "南极洲冰山上的阿德利企鹅 (© Patrick J. Endres/Getty Images)",
      "copyrightlink": "https://www.bing.com/search?q=%E4%B8%96%E7%95%8C%E4%BC%81%E9%B9%85%E6%97%A5&form=hpcapt&mkt=zh-cn",
      "title": "很高兴在这里见到你们!",
      "quiz": "/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20240424_PenguinDirections%22&FORM=HPQUIZ",
      "wp": true,
      "hsh": "1dd70590bd5a4185bbfa3e225445ec40",
      "drk": 1,
      "top": 1,
      "bot": 1,
      "hs": []
    }
}

备注:每次请求会随机返回8张壁纸的Json信息

4K壁纸预览

image.png

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.")

金角:博客最重要的是什么?

银角:这不废话嘛,肯定是博文啊

金角:说的好,没有博文怎么办呢?

银角:没博文等死吧,你整个嘎嘎帅的域名也行。

金角:好主意,如果博客域名是怎么样?

银角:????点了

name: One blog

link: https://googlec.org

tips: 最近想换工作,有没有招人的大佬啊,我是练习时长两年的Java开发,硕2求带飞。

网站公告

今日签到

点亮在社区的每一天
去签到