更新 get_cloud_info.py

This commit is contained in:
chickliu 2025-02-14 12:24:05 +08:00
parent e88109e61c
commit 341a782351

View File

@ -1,37 +1,66 @@
import requests import requests
import os
# 获取云盘信息 # 新增函数从api.txt获取服务器地址与登录模块保持一致
def get_api_base_url():
try:
with open("api.txt", "r") as f:
lines = [line.strip() for line in f.readlines() if line.strip()]
if lines:
return lines[0].rstrip('/')
except FileNotFoundError:
print("api.txt文件未找到使用默认地址")
except Exception as e:
print(f"读取api.txt失败: {e},使用默认地址")
return "http://localhost:3000"
# 获取云盘信息修改了URL生成方式
def get_cloud_info(cookie): def get_cloud_info(cookie):
url = "http://localhost:3000/user/cloud" base_url = get_api_base_url()
params = {"cookie": cookie, "limit": 1} url = f"{base_url}/user/cloud"
# 保持参数传递方式与原始代码一致
params = {
"cookie": cookie,
"limit": 1
}
response = requests.get(url, params=params) response = requests.get(url, params=params)
try: try:
response_data = response.json() response_data = response.json()
if response_data.get('code') == 200: if response_data.get('code') == 200:
# 数据提取保持原始逻辑
total_size = response_data['size'] total_size = response_data['size']
max_size = response_data['maxSize'] max_size = response_data['maxSize']
file_count = response_data['count'] file_count = response_data['count']
# 输出云盘信息
print("云盘信息:") print("\n云盘信息:")
print(f"总大小: {convert_bytes(total_size)}") print(f"▸ 已用空间: {convert_bytes(total_size)}")
print(f"最大容量: {convert_bytes(max_size)}") print(f"▸ 总容量: {convert_bytes(max_size)}")
print(f"文件数: {file_count}") print(f"文件数: {file_count}")
else: else:
print(f"获取云盘信息失败: {response_data.get('message')}") print(f"获取失败: {response_data.get('message', '未知错误')}")
except json.JSONDecodeError: except requests.JSONDecodeError:
print("响应内容无法解析为JSON:", response.text) print("响应解析失败,原始内容:", response.text)
# 字节数转换为可读格式GB, MB, TB等 # 优化后的单位转换函数
def convert_bytes(size_in_bytes): def convert_bytes(size_in_bytes):
"""智能转换存储单位"""
units = ('B', 'KB', 'MB', 'GB', 'TB', 'PB')
try: try:
size_in_bytes = float(size_in_bytes) # 确保传入的值是一个数字 size = float(size_in_bytes)
except ValueError:
return "无效的字节数" # 如果无法转换为数字,则返回错误信息
units = ['B', 'KB', 'MB', 'GB', 'TB']
for unit in units: for unit in units:
if size_in_bytes < 1024.0: if size < 1024.0:
return f"{size_in_bytes:.2f} {unit}" break
size_in_bytes /= 1024.0 size /= 1024.0
return f"{size_in_bytes:.2f} {unit}" return f"{size:.2f} {unit}"
except (TypeError, ValueError):
return "0.00 B"
# 使用示例
if __name__ == "__main__":
# 从文件读取cookie模拟登录模块
with open("cookies.txt", "r") as f:
test_cookie = f.read().strip()
get_cloud_info(test_cookie)