百度网盘 API 获取空间大小示例
使用 Fetch API 获取百度网盘空间大小
本文提供一个使用 JavaScript fetch API 获取百度网盘空间大小的示例代码,并对代码进行优化,使代码更加简洁易读。
原代码
fetch('https://pan.baidu.com/api/quota?checkexpire=1&checkfree=1&channel=chunlei&web=1&app_id=250528&bdstoken=&logid==&clienttype=0').then(function (response) {
return response.json();
}).then(function (data) {
function bytesToSize(bytes) {
if (bytes === 0) return '0 B';
var k = 1024;
sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toPrecision(4) + ' ' + sizes[i];
}
console.log(
'%c空间大小:%c' + bytesToSize(data.total)
, 'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #606060; font-weight: bold;',
'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #06A7FF; font-weight: bold;',
);
console.log(
'%c已用空间:%c' + bytesToSize(data.used)
, 'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #606060; font-weight: bold;',
'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #FF9A09; font-weight: bold;',
);
console.log(
'%c可用空间:%c' + bytesToSize(data.free)
, 'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #606060; font-weight: bold;',
'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #F5622E; font-weight: bold;',
);
});
优化后的代码
const formatBytes = (bytes) => {
if (bytes === 0) return '0 B';
const k = 1024;
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return (bytes / Math.pow(k, i)).toPrecision(4) + ' ' + sizes[i];
};
fetch('https://pan.baidu.com/api/quota?checkexpire=1&checkfree=1&channel=chunlei&web=1&app_id=250528&bdstoken=&logid==&clienttype=0')
.then((response) => response.json())
.then((data) => {
console.log(
'%c空间大小:%c' + formatBytes(data.total),
'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #606060; font-weight: bold;',
'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #06A7FF; font-weight: bold;'
);
console.log(
'%c已用空间:%c' + formatBytes(data.used),
'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #606060; font-weight: bold;',
'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #FF9A09; font-weight: bold;'
);
console.log(
'%c可用空间:%c' + formatBytes(data.free),
'font-family: 微软雅黑;padding: 5px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #606060; font-weight: bold;',
'font-family: 微软雅黑;padding: 5px 4px; border-radius: 0 3px 3px 0; color: #fff; background: #F5622E; font-weight: bold;'
);
});
改进点
- 将字节转换为易读的格式的函数封装成了单独的函数
formatBytes,提高了代码的可维护性和可读性。 - 使用箭头函数和
const、let等 ES6 语法,使代码更加简洁和易读。 - 增加了代码注释,方便他人阅读和理解代码的作用和功能。
注意事项
- 此代码需要在浏览器环境中运行。
- 需要在代码中添加
bdstoken参数,获取方法请参考百度网盘 API 文档。 - 代码中使用的 API 地址可能需要根据实际情况进行调整。
其他
此代码仅供参考,具体的实现方式可以根据实际需求进行调整。
希望本文能帮助你了解如何使用 Fetch API 获取百度网盘空间大小。
原文地址: https://www.cveoy.top/t/topic/lH23 著作权归作者所有。请勿转载和采集!