百度网盘

在windows操作系统当中,当安装了百度网盘之后,BaiduYunCacheFileV0当中会保存百度网盘的所有文件信息以及文件下载上传记录,即时没办法登录百度网盘也能够通过BaiduYunCacheFileV0获取全部信息。以下将展示基于python实现的百度网盘信息获取。

代码实现

下面是对代码的详细解释

获取百度网盘安装目录:

1
users_path = os.path.join(self.get_baidu_pan_install_dir(), "users")

这行代码调用 get_baidu_pan_install_dir 方法获取百度网盘的安装目录,并拼接上 “users” 文件夹的路径,得到用户文件夹的路径。

遍历用户文件夹:

1
2
3
4
5
for md5 in os.listdir(users_path):
md5_path = os.path.join(users_path, md5)
if not os.path.isdir(md5_path):
continue
for i in os.listdir(md5_path):

这段代码遍历用户文件夹中的每个子文件夹(以 MD5 值命名),并进一步遍历每个子文件夹中的文件和文件夹。

处理用户文件夹:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if i!= "AutoBackupFileList" and os.path.isdir(os.path.join(md5_path, i)):
source_data = {
"Name": i,
"FilePath": "/{}".format(i),
"ParentPath": "",
"Size": "",
"Md5": "",
"iconMd5": "folder",
"UpdateTime": "",
"User": i
}
user_path = "/{}".format(i)
clean_data = get_line(self, source_data, self.display)
data = json.dumps(clean_data, ensure_ascii=False)
write_file(self.file, data)

这段代码处理用户文件夹,如果文件夹名不是 “AutoBackupFileList”,则创建一个包含文件夹信息的 source_data 字典,并将其写入到输出文件中。

判断数据库文件是否存在:

1
2
3
all_file_db = os.path.join(md5_path, "BaiduYunCacheFileV0.db")
if not os.path.exists(all_file_db):
break

这段代码检查每个用户文件夹中是否存在名为 “BaiduYunCacheFileV0.db” 的数据库文件,如果不存在,则跳过该用户文件夹。

读取数据库文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
try:
conn = sqlite3.connect(all_file_db)
conn.row_factory = self.dict_factory
cursor = conn.cursor()
# 同理,上传下载分别是download_history_file、upload_history_file
for item in cursor.execute("select * from cache_file"):
parent_path = item.get("parent_path")
server_filename = item.get("server_filename")
try:
iconMd5 = server_filename.split(".", maxsplit=1)[1]
except:
iconMd5 = "folder"
source_data = {
"Name": server_filename,
"FilePath": user_path + os.path.join(parent_path, server_filename),
"ParentPath": user_path + parent_path[:-1:],
"Size": item.get("file_size"),
"Md5": item.get("md5"),
"iconMd5": iconMd5,
"ServerMtime": item.get("server_mtime"),
"LocalMtime": item.get("local_mtime"),
"User": i
}
clean_data = get_line(self, source_data, self.display)
data = json.dumps(clean_data, ensure_ascii=False)
write_file(self.file, data)
except Exception as e:
setting.log.error(e)
break

这段代码尝试连接到数据库文件,并读取其中的 “cache_file” 表中的所有数据。对于每一条数据,它创建一个包含文件信息的 source_data 字典,并将其写入到输出文件中。如果在处理过程中遇到异常,它会记录错误信息并跳过当前用户文件夹。