兰空图床删除单个用户所有图片


共计 1429 个字符,预计需要花费 4 分钟才能阅读完成。

有时候需要一个专门的用户来存储一些临时图片,但事实是兰空图床删除临时图片非常不方便,所以可以创建一个专门的账号用来存储这些图片,不需要的时候直接全部删除即可。

使用如下脚本可以方便地删除图片,需要替换自己图床的 url、账号密码:

import requests
import json

# 定义公共的API基础URL
BASE_URL = "https://img.xxx.com"
email = "xxx"
password = "xxx"

# 登录获取token
def get_token(email, password):
    url = f"{BASE_URL}/api/v1/tokens"
    data = {
        "email": email,
        "password": password
    }
    response = requests.post(url, json=data)
    if response.status_code == 200:
        result = response.json()
        if result["status"]:
            return result["data"]["token"]
    return None

# 获取图片列表并返回所有图片的key
def get_images(token):
    url = f"{BASE_URL}/api/v1/images"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    image_keys = []
    while True:
        response = requests.get(url, headers=headers)
        if response.status_code == 200:
            result = response.json()
            if result["status"]:
                if not result["data"]["data"]:
                    print(f"该用户下图片为空!")
                    break
                for image in result["data"]["data"]:
                    image_keys.append(image["key"])
                # 获取下一页的URL
                next_page_url = result["data"]["next_page_url"]
                if not next_page_url:
                    break
                url = next_page_url
            else:
                break
        else:
            break
    return image_keys

# 删除图片
def delete_image(token, key):
    url = f"{BASE_URL}/api/v1/images/{key}"
    headers = {
        "Authorization": f"Bearer {token}"
    }
    response = requests.delete(url, headers=headers)
    if response.status_code == 200:
        result = response.json()
        if result["status"]:
            print(f"删除图片: {key}")
        else:
            print(f"删除图片失败: {key}")
    else:
        print(f"删除图片失败: {key}")

# 主函数
def main():
    token = get_token(email, password)
    if token:
        image_keys = get_images(token)
        for key in image_keys:
            delete_image(token, key)
    else:
        print("获取token失败")

if __name__ == "__main__":
    main()

Tips:清朝云网络工作室

阅读剩余
THE END