最近需要統計百度網盤里文件的數量,百度網盤又沒有提供這樣的功能,因此之前自己寫了段腳本進行查驗,見《利用瀏覽器調試功能 計算 百度網盤》。之后發現每個目錄最多文件數只有1000,因此研究了哈百度的接口,重新對腳本進行了修改。具體見代碼:
/********************
*百度文件數量統計 V2
*2018.06.15
*Jackie
********************//************
*百度查詢接口
http://pan.baidu.com/api/list?
dir= //查詢目錄
&num=100000 //分頁大小 最大支持99999999999999 默認1000
&page=1 //頁碼
&order=time //排序屬性
&desc=1 //排序順序
&clienttype=0
&showempty=0
************/var root = "";//指定目錄,空取當前目錄
var maxPageSize=99999999999999; //最多支持這么多,超過此值無效
var dskApi="https://pan.baidu.com/api/list?&num="+maxPageSize+"&page=1&dir=";
var totalCount = 0;
var startTime = new Date();
function timeSpan(stime, etime) {var usedTime = etime - stime;var days = Math.floor(usedTime / (24 * 3600 * 1000));var leave1 = usedTime % (24 * 3600 * 1000);var hours = Math.floor(leave1 / (3600 * 1000));var leave2 = leave1 % (3600 * 1000);var minutes = Math.floor(leave2 / (60 * 1000));var leave3 = leave2 % (60 * 1000);var seconds = Math.round(leave3 / 1000);var time ="";if(days>0){time+=days+"天";}if(hours>0){time+=hours+"小時";}if(minutes>0){time+=minutes+"分鐘";}time+=seconds+"秒";return time;
} (function($){$.getUrlParam = function (name) {var search = document.location.hash;var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");var matcher = pattern.exec(search);var items = null;if (null != matcher) {try {items = decodeURIComponent(decodeURIComponent(matcher[1]));} catch (e) {try {items = decodeURIComponent(matcher[1]);} catch (e) {items = matcher[1];}}}return items;}
})(jQuery);if(root=="")
{root=$.getUrlParam("path");
}function GetFilesCount(fileLists)
{var count=0;var fl=fileLists.length;for (var index=0; index<fl; index++) { (function(index) { var file=fileLists[index];if(file.isdir==0){count++;totalCount++;}else if(file.isdir==1){count=count+GetDirFilsCount(file.path);}})(index); } return count;
}function GetDirFilsCount(dirName)
{var dfCount=0;$.ajax({url: dskApi+''+dirName,type: 'get',async: true,//true 異步,false 同步success: function(data) {var fileLists = data.list;dfCount=GetFilesCount(fileLists); console.log(decodeURIComponent(dirName)+":"+dfCount);console.log("統計目錄為:“"+decodeURIComponent(root)+"” 當前計算總數:"+totalCount+" 用時:" +timeSpan(startTime, new Date()));}});return dfCount;
}GetDirFilsCount(root);
提示:由于百度服務器或者接口可能不穩定,會造成查詢不準確,數據僅供參考。
文件多的話,查詢比較耗時,同步的準確率比異步要高,但更耗時。
如圖,用了半個小時才統計了15W左右的數據,據百度稱俺的文件數超過了500W,哈哈。