diff --git a/core/image.py b/core/image.py index b5f711b..22f3f4d 100644 --- a/core/image.py +++ b/core/image.py @@ -48,17 +48,13 @@ def migration_ydnote_url(self, file_path): # 图片 image_urls = REGEX_IMAGE_URL.findall(content) if len(image_urls) > 0: - logging.info( - "正在转换有道云笔记「{}」中的有道云图片链接...".format(file_path) - ) + logging.info("正在转换有道云笔记「{}」中的有道云图片链接...".format(file_path)) for image_url in image_urls: try: image_path = self._get_new_image_path(file_path, image_url) except Exception as error: logging.info( - "下载图片「{}」可能失败!请检查图片!错误提示:{}".format( - image_url, format(error) - ) + "下载图片「{}」可能失败!请检查图片!错误提示:{}".format(image_url, format(error)) ) if image_url == image_path: continue @@ -73,9 +69,7 @@ def migration_ydnote_url(self, file_path): # 附件 attach_name_and_url_list = REGEX_ATTACH.findall(content) if len(attach_name_and_url_list) > 0: - logging.info( - "正在转换有道云笔记「{}」中的有道云附件链接...".format(file_path) - ) + logging.info("正在转换有道云笔记「{}」中的有道云附件链接...".format(file_path)) for attach_name_and_url in attach_name_and_url_list: attach_url = attach_name_and_url[1] attach_path = self._download_ydnote_url( @@ -128,9 +122,7 @@ def _download_ydnote_url(self, file_path, url, attach_name=None) -> str: try: response = self.youdaonote_api.http_get(url) except requests.exceptions.ProxyError as err: - error_msg = "网络错误,「{}」下载失败。错误提示:{}".format( - url, format(err) - ) + error_msg = "网络错误,「{}」下载失败。错误提示:{}".format(url, format(err)) logging.info(error_msg) return "" @@ -176,9 +168,15 @@ def _download_ydnote_url(self, file_path, url, attach_name=None) -> str: # 请求后的真实的 URL 中才有东西 realUrl = parse.parse_qs(urlparse(response.url).query) - + if realUrl: - filename = realUrl.get("filename")[0] if realUrl.get("filename") else realUrl.get("download")[0] if realUrl.get("download") else "" + filename = ( + realUrl.get("filename")[0] + if realUrl.get("filename") + else realUrl.get("download")[0] + if realUrl.get("download") + else "" + ) file_name = file_basename + filename else: file_name = "".join([file_basename, file_suffix]) @@ -187,9 +185,7 @@ def _download_ydnote_url(self, file_path, url, attach_name=None) -> str: try: with open(local_file_path, "wb") as f: f.write(response.content) # response.content 本身就为字节类型 - logging.info( - "已将{}「{}」转换为「{}」".format(file_type, url, local_file_path) - ) + logging.info("已将{}「{}」转换为「{}」".format(file_type, url, local_file_path)) except: error_msg = "{} {}有误!".format(url, file_type) logging.info(error_msg) @@ -228,9 +224,7 @@ def upload_to_smms(youdaonote_api, image_url, smms_secret_token) -> Tuple[str, s try: smfile = youdaonote_api.http_get(image_url).content except: - error_msg = "下载「{}」失败!图片可能已失效,可浏览器登录有道云笔记后,查看图片是否能正常加载".format( - image_url - ) + error_msg = "下载「{}」失败!图片可能已失效,可浏览器登录有道云笔记后,查看图片是否能正常加载".format(image_url) return "", error_msg files = {"smfile": smfile} upload_api_url = "https://sm.ms/api/v2/upload" @@ -263,7 +257,9 @@ def upload_to_smms(youdaonote_api, image_url, smms_secret_token) -> Tuple[str, s if res_json.get("code") == "flood": return "", error_msg - error_msg = "上传「{}」到 SM.MS 失败,请检查图片 url 或 smms_secret_token({})是否正确!将下载图片到本地".format( - image_url, smms_secret_token + error_msg = ( + "上传「{}」到 SM.MS 失败,请检查图片 url 或 smms_secret_token({})是否正确!将下载图片到本地".format( + image_url, smms_secret_token + ) ) return "", error_msg diff --git a/core/log.py b/core/log.py index 782ac66..5b870fc 100644 --- a/core/log.py +++ b/core/log.py @@ -11,7 +11,7 @@ def init_logging(): log_dir = "logs" if not os.path.exists(log_dir): os.mkdir(log_dir) - + logging.basicConfig( handlers=[ logging.FileHandler( diff --git a/pull.py b/pull.py index 31158fe..7e452f8 100644 --- a/pull.py +++ b/pull.py @@ -97,9 +97,7 @@ def _check_local_dir(self, local_dir, test_default_dir=None) -> Tuple[str, str]: try: os.mkdir(local_dir) except: - return "", "请检查「{}」上层文件夹是否存在,并使用绝对路径!".format( - local_dir - ) + return "", "请检查「{}」上层文件夹是否存在,并使用绝对路径!".format(local_dir) return local_dir, "" def _get_ydnote_dir_id(self, ydnote_dir) -> Tuple[str, str]: @@ -230,7 +228,9 @@ def pull_dir_by_id_recursively(self, dir_id, local_dir): create_time = file_entry["createTimeForSort"] self._add_or_update_file(id, name, local_dir, modify_time, create_time) - def _add_or_update_file(self, file_id, file_name, local_dir, modify_time, create_time): + def _add_or_update_file( + self, file_id, file_name, local_dir, modify_time, create_time + ): """ 新增或更新文件 :param file_id: @@ -245,18 +245,24 @@ def _add_or_update_file(self, file_id, file_name, local_dir, modify_time, create original_file_path = os.path.join(local_dir, file_name).replace( "\\", "/" ) # 原后缀路径 - + # 所有类型文件均下载,不做处理 file_type = self._judge_type(file_id, youdao_file_suffix) - + # 「文档」类型本地文件均已 .md 结尾 - local_file_path = os.path.join( - local_dir, "".join([os.path.splitext(file_name)[0], MARKDOWN_SUFFIX]) - ).replace("\\", "/") if file_type != FileType.OTHER else original_file_path - + local_file_path = ( + os.path.join( + local_dir, "".join([os.path.splitext(file_name)[0], MARKDOWN_SUFFIX]) + ).replace("\\", "/") + if file_type != FileType.OTHER + else original_file_path + ) + # 如果有有道云笔记是「文档」类型,则提示类型 - tip = ",云笔记原格式为 {}".format(file_type.name) if file_type != FileType.OTHER else "" - + tip = ( + ",云笔记原格式为 {}".format(file_type.name) if file_type != FileType.OTHER else "" + ) + file_action = self._get_file_action(local_file_path, modify_time) if file_action == FileActionEnum.CONTINUE: return @@ -272,9 +278,11 @@ def _add_or_update_file(self, file_id, file_name, local_dir, modify_time, create youdao_file_suffix, ) if file_action == FileActionEnum.CONTINUE: - logging.debug('{}「{}」{}'.format(file_action.value, local_file_path, tip)) + logging.debug( + "{}「{}」{}".format(file_action.value, local_file_path, tip) + ) else: - logging.info('{}「{}」{}'.format(file_action.value, local_file_path, tip)) + logging.info("{}「{}」{}".format(file_action.value, local_file_path, tip)) # 本地文件时间设置为有道云笔记的时间 if platform.system() == "Windows": @@ -311,9 +319,7 @@ def _pull_file( try: YoudaoNoteConvert.covert_xml_to_markdown(file_path) except ET.ParseError: - logging.info( - "此 note 笔记应该为 17 年以前新建,格式为 html,将转换为 Markdown ..." - ) + logging.info("此 note 笔记应该为 17 年以前新建,格式为 html,将转换为 Markdown ...") YoudaoNoteConvert.covert_html_to_markdown(file_path) except Exception as e: logging.info("note 笔记转换 MarkDown 失败,将跳过", repr(e)) @@ -327,6 +333,7 @@ def _pull_file( ) imagePull.migration_ydnote_url(local_file_path) + if __name__ == "__main__": log.init_logging() @@ -342,17 +349,15 @@ def _pull_file( youdaonote_pull.pull_dir_by_id_recursively( ydnote_dir_id, youdaonote_pull.root_local_dir ) - except requests.exceptions.ProxyError as proxyErr: + except requests.exceptions.ProxyError: logging.info( "请检查网络代理设置;也有可能是调用有道云笔记接口次数达到限制,请等待一段时间后重新运行脚本,若一直失败,可删除「cookies.json」后重试" ) traceback.print_exc() logging.info("已终止执行") sys.exit(1) - except requests.exceptions.ConnectionError as connectionErr: - logging.info( - "网络错误,请检查网络是否正常连接。若突然执行中断,可忽略此错误,重新运行脚本" - ) + except requests.exceptions.ConnectionError: + logging.info("网络错误,请检查网络是否正常连接。若突然执行中断,可忽略此错误,重新运行脚本") traceback.print_exc() logging.info("已终止执行") sys.exit(1) diff --git a/test/test.py b/test/test.py index 0fa5843..951f3eb 100644 --- a/test/test.py +++ b/test/test.py @@ -3,9 +3,9 @@ from __future__ import absolute_import import os -import unittest -from unittest.mock import patch, mock_open, Mock import sys +import unittest +from unittest.mock import Mock, mock_open, patch sys.path.append(os.path.join(os.path.dirname(__file__), "..")) @@ -13,7 +13,6 @@ from core.covert import YoudaoNoteConvert from pull import YoudaoNotePull - # 使用 test_cookies.json 作为 cookies 地址,避免 cookies.json 数据在运行测试用例时被错误覆盖 TEST_COOKIES_PATH = "test_cookies.json" @@ -60,9 +59,7 @@ def test_cookies_login(self): "builtins.open", mock_open(read_data=cookies_json_str.encode("utf-8")) ): message = youdaonote_api.login_by_cookies() - self.assertEqual( - message, "转换「{}」为字典时出现错误".format(self.TEST_COOKIES_PATH) - ) + self.assertEqual(message, "转换「{}」为字典时出现错误".format(self.TEST_COOKIES_PATH)) # 如果 cookies 格式正确,但少了 YNOTE_CSTK。期待:登录失败 cookies_json_str = """{"cookies": [ @@ -211,7 +208,7 @@ def test_html_to_markdown(self): sudo perl -pi -e 's|u0‰Ø|ë0‰Ø|' /System/Library/Extensions/AppleRTC.kext/Contents/MacOS/AppleRTC\n""" self.assertEqual(new_content, expected_content) - + def test_covert_json_to_markdown_content(self): """ 测试 json 转换 markdown