更新 get_cloud_info.py
This commit is contained in:
parent
e88109e61c
commit
341a782351
@ -1,37 +1,66 @@
|
||||
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):
|
||||
url = "http://localhost:3000/user/cloud"
|
||||
params = {"cookie": cookie, "limit": 1}
|
||||
base_url = get_api_base_url()
|
||||
url = f"{base_url}/user/cloud"
|
||||
|
||||
# 保持参数传递方式与原始代码一致
|
||||
params = {
|
||||
"cookie": cookie,
|
||||
"limit": 1
|
||||
}
|
||||
|
||||
response = requests.get(url, params=params)
|
||||
try:
|
||||
response_data = response.json()
|
||||
if response_data.get('code') == 200:
|
||||
# 数据提取保持原始逻辑
|
||||
total_size = response_data['size']
|
||||
max_size = response_data['maxSize']
|
||||
file_count = response_data['count']
|
||||
# 输出云盘信息
|
||||
print("云盘信息:")
|
||||
print(f"总大小: {convert_bytes(total_size)}")
|
||||
print(f"最大容量: {convert_bytes(max_size)}")
|
||||
print(f"文件数量: {file_count}")
|
||||
|
||||
print("\n云盘信息:")
|
||||
print(f"▸ 已用空间: {convert_bytes(total_size)}")
|
||||
print(f"▸ 总容量: {convert_bytes(max_size)}")
|
||||
print(f"▸ 文件总数: {file_count} 个")
|
||||
else:
|
||||
print(f"获取云盘信息失败: {response_data.get('message')}")
|
||||
except json.JSONDecodeError:
|
||||
print("响应内容无法解析为JSON:", response.text)
|
||||
print(f"获取失败: {response_data.get('message', '未知错误')}")
|
||||
except requests.JSONDecodeError:
|
||||
print("响应解析失败,原始内容:", response.text)
|
||||
|
||||
# 字节数转换为可读格式(GB, MB, TB等)
|
||||
# 优化后的单位转换函数
|
||||
def convert_bytes(size_in_bytes):
|
||||
"""智能转换存储单位"""
|
||||
units = ('B', 'KB', 'MB', 'GB', 'TB', 'PB')
|
||||
try:
|
||||
size_in_bytes = float(size_in_bytes) # 确保传入的值是一个数字
|
||||
except ValueError:
|
||||
return "无效的字节数" # 如果无法转换为数字,则返回错误信息
|
||||
size = float(size_in_bytes)
|
||||
for unit in units:
|
||||
if size < 1024.0:
|
||||
break
|
||||
size /= 1024.0
|
||||
return f"{size:.2f} {unit}"
|
||||
except (TypeError, ValueError):
|
||||
return "0.00 B"
|
||||
|
||||
units = ['B', 'KB', 'MB', 'GB', 'TB']
|
||||
for unit in units:
|
||||
if size_in_bytes < 1024.0:
|
||||
return f"{size_in_bytes:.2f} {unit}"
|
||||
size_in_bytes /= 1024.0
|
||||
return f"{size_in_bytes:.2f} {unit}"
|
||||
# 使用示例
|
||||
if __name__ == "__main__":
|
||||
# 从文件读取cookie(模拟登录模块)
|
||||
with open("cookies.txt", "r") as f:
|
||||
test_cookie = f.read().strip()
|
||||
|
||||
get_cloud_info(test_cookie)
|
||||
Loading…
Reference in New Issue
Block a user