Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEA] 支持排除不需要导出的文件夹 #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,15 @@ pip install -r requirements.txt
"local_dir": "",
"ydnote_dir": "",
"smms_secret_token": "",
"exclude_dirs": [],
"is_relative_path": true
}
```

* `local_dir`:选填,本地存放导出文件的文件夹(绝对路径),不填则默认为当前文件夹
* `ydnote_dir`:选填,有道云笔记指定导出文件夹名,不填则导出所有文件
* `smms_secret_token`:选填, [SM.MS](https://sm.ms) 的 `Secret Token`(注册后 -> Dashboard -> API Token),用于上传笔记中有道云图床图片到 SM.MS 图床,不填则只下载到本地(`youdaonote-images` 文件夹),`Markdown` 中使用本地链接
* `exclude_dirs`:选填,指定不导出的文件夹的名称的一部分,不填则默认不排除文件夹,比如笔记里的文件夹名称为 `我的手机`,则填写 `"的手机"` 也可以
* `is_relative_path`:选填,在 MD 文件中图片 / 附件是否采用相对路径展示,不填或 false 为绝对路径,true 为相对路径

示例:
Expand All @@ -154,7 +156,8 @@ pip install -r requirements.txt
{
"local_dir": "/Users/deppwang/Documents/youdaonote-pull/test",
"ydnote_dir": "",
"smms_secret_token": "SGSLk9yWdTe4RenXYqEPWkqVrx0Yexample"
"smms_secret_token": "SGSLk9yWdTe4RenXYqEPWkqVrx0Yexample",
"exclude_dirs": ["我的手机"],
}
```

Expand All @@ -164,7 +167,8 @@ pip install -r requirements.txt
{
"local_dir": "D:/Documents/youdaonote-pull/test",
"ydnote_dir": "",
"smms_secret_token": "SGSLk9yWdTe4RenXYqEPWkqVrx0Yexample"
"smms_secret_token": "SGSLk9yWdTe4RenXYqEPWkqVrx0Yexample",
"exclude_dirs": ["我的手机"],
}
```

Expand Down
4 changes: 2 additions & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"local_dir": "",
"ydnote_dir": "",
"smms_secret_token": "",
"exclude_dirs": [],
"is_relative_path": true
}

}
23 changes: 17 additions & 6 deletions pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(self):
self.youdaonote_api = None
self.smms_secret_token = None
self.is_relative_path = None # 是否使用相对路径
self.exclude_dirs = None # 需要排除的目录

def _covert_config(self, config_path=None) -> Tuple[dict, str]:
"""
Expand All @@ -76,11 +77,11 @@ def _covert_config(self, config_path=None) -> Tuple[dict, str]:
"请检查「config.json」格式是否为 utf-8 格式的 json!建议使用 Sublime 编辑「config.json」",
)

key_list = ["local_dir", "ydnote_dir", "smms_secret_token", "is_relative_path"]
if key_list != list(config_dict.keys()):
key_list = {"local_dir", "ydnote_dir", "exclude_dirs", "smms_secret_token", "is_relative_path"}
if key_list != set(config_dict.keys()):
return (
{},
"请检查「config.json」的 key 是否分别为 local_dir, ydnote_dir, smms_secret_token, is_relative_path",
"请检查「config.json」的 key 是否分别为 local_dir, ydnote_dir, exclude_dirs, smms_secret_token, is_relative_path",
)
return config_dict, ""

Expand Down Expand Up @@ -137,6 +138,7 @@ def get_ydnote_dir_id(self) -> Tuple[str, str]:
if error_msg:
return "", error_msg
self.root_local_dir = local_dir
self.exclude_dirs = config_dict['exclude_dirs']
self.youdaonote_api = YoudaoNoteApi()
error_msg = self.youdaonote_api.login_by_cookies()
logging.info("本次使用 Cookies 登录")
Expand Down Expand Up @@ -224,17 +226,26 @@ def pull_dir_by_id_recursively(self, dir_id, local_dir):
raise KeyError("有道云笔记修改了接口地址,此脚本暂时不能使用!请提 issue")
for entry in entries:
file_entry = entry["fileEntry"]
id = file_entry["id"]
dir_id = file_entry["id"]
name = file_entry["name"]
if file_entry["dir"]:
sub_dir = os.path.join(local_dir, name).replace("\\", "/")
# 排除不需要导出的文件夹(一级目录)
is_excluded = False
for item in self.exclude_dirs:
if name.find(item) != -1:
logging.info("文件夹 [%s] 已排除,不需要导出", name)
is_excluded = True
break
if is_excluded:
continue
if not os.path.exists(sub_dir):
os.mkdir(sub_dir)
self.pull_dir_by_id_recursively(id, sub_dir)
self.pull_dir_by_id_recursively(dir_id, sub_dir)
else:
modify_time = file_entry["modifyTimeForSort"]
create_time = file_entry["createTimeForSort"]
self._add_or_update_file(id, name, local_dir, modify_time, create_time)
self._add_or_update_file(dir_id, name, local_dir, modify_time, create_time)

def _add_or_update_file(
self, file_id, file_name, local_dir, modify_time, create_time
Expand Down
1 change: 1 addition & 0 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ def test_covert_config_json(self):
"local_dir": "",
"ydnote_dir": "",
"smms_secret_token": "",
"exclude_dirs",
"is_relative_path": true
}
"""
Expand Down