How do I clear the photos from the recently deleted iCloud album? I tried several libraries without success. Recently Deleted is essentially a photo recovery site. I am a newbie. Please help me.
import logging
from icloudpy import ICloudPyService
import requests
# 配置日志记录
logging.basicConfig(level=logging.INFO)
# iCloud 帐户信息
apple_id = "secret"
password = "secret"
# 创建 iCloud 服务实例
logging.info("正在创建 iCloud 服务实例...")
api = ICloudPyService(apple_id, password)
# 获取所有相册
logging.info("正在获取所有相册...")
albums = api.photos.albums
for album_name, album in albums.items():
logging.info(f"找到相册: {album_name}")
# 获取“Recently Deleted”相册
recently_deleted_album = albums['Recently Deleted']
# 获取会话令牌
session_token = None
for cookie in api.session.cookies:
if cookie.name == 'X-APPLE-WEBAUTH-TOKEN':
session_token = cookie.value
break
if not session_token:
logging.error("无法获取会话令牌")
else:
client_id = api.session.client_id if hasattr(api.session, 'client_id') else "client_id_placeholder"
service_endpoint = "p104-ckdatabasews.icloud.com" # 请根据需要修改此值
# 删除“Recently Deleted”相册中的所有照片
logging.info("开始从 'Recently Deleted' 相册中彻底删除所有照片...")
for photo in recently_deleted_album:
try:
logging.info(f"正在删除照片: {photo.filename}")
delete_url = f"https://{service_endpoint}/database/1/com.apple.photos.cloud/production/private/ph/startBatchDelete"
payload = {
"clientId": client_id,
"command": {
"operation": "delete",
"ids": [photo.id]
}
}
headers = {
'Authorization': f'Bearer {session_token}',
'Content-Type': 'application/json'
}
response = requests.post(delete_url, json=payload, headers=headers)
response.raise_for_status()
logging.info(f"照片 {photo.filename} 已删除")
except Exception as e:
logging.error(f"删除照片时出错: {e}")
logging.info("照片删除完毕。")