Skip to content

Commit

Permalink
Optimize code style
Browse files Browse the repository at this point in the history
  • Loading branch information
DeppWang committed May 5, 2024
1 parent b83cc95 commit 9f3174f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 52 deletions.
40 changes: 18 additions & 22 deletions core/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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 ""

Expand Down Expand Up @@ -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])
Expand All @@ -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)
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion core/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
49 changes: 27 additions & 22 deletions pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand All @@ -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":
Expand Down Expand Up @@ -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))
Expand All @@ -327,6 +333,7 @@ def _pull_file(
)
imagePull.migration_ydnote_url(local_file_path)


if __name__ == "__main__":
log.init_logging()

Expand All @@ -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)
Expand Down
11 changes: 4 additions & 7 deletions test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
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__), ".."))

from core.api import YoudaoNoteApi
from core.covert import YoudaoNoteConvert
from pull import YoudaoNotePull


# 使用 test_cookies.json 作为 cookies 地址,避免 cookies.json 数据在运行测试用例时被错误覆盖
TEST_COOKIES_PATH = "test_cookies.json"

Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9f3174f

Please sign in to comment.