163cloud/get_cloud_info.py

66 lines
2.1 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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):
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("\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 requests.JSONDecodeError:
print("响应解析失败,原始内容:", response.text)
# 优化后的单位转换函数
def convert_bytes(size_in_bytes):
"""智能转换存储单位"""
units = ('B', 'KB', 'MB', 'GB', 'TB', 'PB')
try:
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"
# 使用示例
if __name__ == "__main__":
# 从文件读取cookie模拟登录模块
with open("cookies.txt", "r") as f:
test_cookie = f.read().strip()
get_cloud_info(test_cookie)