凉风有信,秋月无边。
亏我思娇的情绪好比度日如年。

用python导出百度云盘的文件目录

参考了Python | 用蟒蛇代码导出百度网盘文件目录的代码,我第一次发现原来百度云盘在用户端本地有文件缓存的数据库文件,而且使用的数据库是sqlite3,该程序的思路是:给出文件缓存的数据库文件,然后读取出数据库文件中的内容,再按照文件夹目录的形式写到csv文件中。

1. DB的文件名:BaiduYunCacheFileV0.db,在百度云盘安装的路径中,
2.我的参考路径:
\baidu\BaiduNetdisk\users\34ff153956ff308c032b17809da08081\

详细的代码如下(时间有限+能力不足=>代码很low,但是功能都实现了):

#!/usr/bin/env python3

# -*- coding:utf-8 -*-

from tkinter import *
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import asksaveasfilename
from tkinter.ttk import *
import sqlite3

def set_degree(value):
    global input_degree
    input_degree = value

def select_db_file():
    db_file = askopenfilename(title="请选择BaiduYunCacheFileV0.db文件", filetypes=[('db', '*.db')])
    db.set(db_file)
    print("run select_db_file end!")

def select_save_file():
    save_file = asksaveasfilename(filetypes=[('文本文件', '*.txt')])
    f.set(save_file + ".txt")
    print("run select_save_file end!")

def set_degree_value(*args):
    value = comboxlist.get()
    if value == "全部":
        set_degree(100)
    else:
        set_degree(value)
    print(input_degree)

def write_txt_file(file_dict, f, item, gap="", curr_degree=-1):
    f.write(item + "\n")
    if item == "/":
        f.write("/\n")
        for i in file_dict["/"]:
            f.write("  " + i + "\n")
            i = item + i + "/"
            if i in file_dict:
                write_txt_file(file_dict, f, i, gap="┣━", curr_degree=1)
    else:
        curr_degree += 1
        for i in file_dict[item]:
            f.write(gap + i + "\n")
            i = item + i + "/"
            if i in file_dict and curr_degree < int(input_degree):
                write_txt_file(file_dict, f, i, gap, curr_degree=curr_degree)

def create_baiduyun_filelist():
    print("start get baidun_filelist..... ")
    file_dict = {}
    conn = sqlite3.connect(db.get())
    cursor = conn.cursor()
    cursor.execute("select * from cache_file")
    while True:
        value = cursor.fetchone()
        if not value:
            break
        path = value[2]
        name = value[3]
        size = value[4]
        isdir = value[6]
        if path not in file_dict:
            file_dict[path] = []
            file_dict[path].append(name)
        else:
            file_dict[path].append(name)

    with open(f.get(), "w", encoding='utf-8') as fp:
        write_txt_file(file_dict, fp, "/")

window = Tk()
window.geometry('600x400')
window.title('百度云文件列表生成工具')

db_select = Button(window, text=' 选择DB文件 ', command=select_db_file)
db_select.grid(row=1, column=1, sticky=W, padx=(2, 0), pady=(2, 0))

db = StringVar()
db_path = Entry(window, width=80, textvariable=db)
db_path['state'] = 'readonly'
db_path.grid(row=1, column=2, padx=3, pady=3, sticky=W + E)

save_path = Button(window, text='选择保存地址', command=select_save_file)
save_path.grid(row=2, column=1, sticky=W, padx=(2, 0), pady=(2, 0))

f = StringVar()
file_path = Entry(window, width=80, textvariable=f)
file_path['state'] = 'readonly'
file_path.grid(row=2, column=2, padx=3, pady=3, sticky=W + E)

degree_button = Button(window, text='选择文件深度')
degree_button.grid(row=3, column=1, sticky=W, padx=(2, 0), pady=(2, 0))

input_degree = 100   #默认为所有的

comboxlist = Combobox(window, textvariable=input_degree)
comboxlist["values"] = ("全部", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
comboxlist.current(0)  #选择第一个
comboxlist.bind("<<ComboboxSelected>>", set_degree_value)  #绑定事件,(下拉列表框被选中时,绑定set_degree_value()函数)
comboxlist.grid(row=3, column=2, padx=3, pady=3, sticky=W + E)

create_btn = Button(window, text='生成文件列表', command=create_baiduyun_filelist)
create_btn.grid(row=4, column=1, columnspan=3, pady=(0, 2))
window.columnconfigure(2, weight=1)
window.mainloop()

现在的版本被我改成导出的是txt文件了。方便处理以及使用

赞(0) 打赏
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的内容,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。本博客资源仅供个人学习交流,请于下载后24小时内删除,不允许用于商业用途,否则法律问题自行承担。如果本文导致的版权问题以及内容纠错问题请联系站长QQ:1004619 | 点此给我发送邮件
本文标题:《用python导出百度云盘的文件目录》
本文地址:https://www.1004619.com/biji/yong-python-dao-chu-bai-du-yun-pan-de-wen-jian-mu-lu/

相关推荐

  • 暂无文章