1. 緣起
上週男票說自己在整理網盤資料的時候,發現資料太多,整理起來很麻煩,問我能不能幫他寫個程序把網盤裏的文件目錄全部導出來存爲Excel文件,這樣就能夠知道網盤裏的全部目錄結構了。
於是,作爲一個傲嬌的程序媛,怎麼能夠放棄在自己帥氣男票面前顯擺的機會能?故便欣然答應了。在網上百度搜索了一番,在簡書中發現了一篇現成的代碼分享Python | 用蟒蛇代碼導出百度網盤文件目錄,便花了幾分鐘照着該簡書分享寫了一個小程序,打包爲exe可執行文件,給男票發了過去。原本以爲他會很滿意,正等着他來誇我來呢,結果,他竟然當起了甲方,挑起了刺,說:程序是導出了所有的文件目錄,但要是能夠指定導出文件的級別就好了(比如說我只想導出二級目錄,程序只導出二級目錄......)。雖然自己氣到爆炸,但是作爲一枚有修養的程序媛,還是乖乖的去改需求了。
2. 經過
參考了Python | 用蟒蛇代碼導出百度網盤文件目錄的代碼,我第一次發現原來百度雲盤在用戶端本地有文件緩存的數據庫文件,而且使用的數據庫是sqlite3,該程序的思路是:給出文件緩存的數據庫文件,然後讀取出數據庫文件中的內容,再按照文件夾目錄的形式寫到csv文件中。
1. DB的文件名:BaiduYunCacheFileV0.db,在百度雲盤安裝的路徑中,
2.我的參考路徑:
\baidu\BaiduNetdisk\users\34ff153956ff308c032b17809da08081\
詳細的代碼如下(時間有限+能力不足=>代碼很low,但是功能都實現了):
"""
__title__ = ''
__author__ = 'yonghui Luo'
__mtime__ = '2019-02-27'
"""
#!/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 select_save_csv_file():
save_file = asksaveasfilename(filetypes=[('文件', '*.csv')])
f.set(save_file + ".csv")
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_csv_file(file_dict, f, item, gap="", curr_degree=-1):
# print("degree=" + str(curr_degree) + " input_degree=" + str(input_degree))
tem_list = ["┣━"]
if item == "/":
f.write("━" + "/" + "\n")
for i in file_dict["/"]:
f.write("┣" + "━" + i + "\n")
i = item + i + "/"
if i in file_dict:
write_csv_file(file_dict, f, i, gap="┣━", curr_degree=1)
else:
curr_degree += 1
tem_list.insert(0, "┃ ")
gap = "┃ " + gap
for i in file_dict[item]:
tem_list.append(i)
f.write(gap + i + "\n")
i = item + i + "/"
if i in file_dict and curr_degree < int(input_degree):
write_csv_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_csv_file(file_dict, fp, "/")
window = Tk()
window.geometry('600x300')
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_csv_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)
print(input_degree)
create_btn = Button(window, text='生成文件列表', command=create_baiduyun_filelist)
create_btn.grid(row=4, column=1, columnspan=2, pady=(0, 2))
window.columnconfigure(2, weight=1)
window.mainloop()
程序運行:
結果:
3. 結果
刷刷的,花了一會兒功夫吧代碼改好,測試通過,打包給男票發過去了。心想這次應該滿足需求,會被他誇獎一番了吧。
正當我滿心期待誇獎、沉浸在自己的幻想中時。男票打來電話,他在電話中小心翼翼的說:能不能再提個小小的意見?
我想都沒想,就回了他:不能。哼,小姐姐也是有脾氣的。
(由於已經滿足了男票的需求,後面就沒有再花時間去改男票提出的最後的意見啦)
4. 附件
可執行程序下載