Skip to content

Commit

Permalink
Add get script directory method
Browse files Browse the repository at this point in the history
  • Loading branch information
DeppWang committed Jun 12, 2024
1 parent b625f78 commit 315a308
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
19 changes: 17 additions & 2 deletions core/api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
import json
import os
import sys

import requests


def get_script_directory():
"""获取脚本所在的目录"""
if getattr(sys, "frozen", False):
# 如果是打包后的可执行文件
return os.path.dirname(sys.executable)
else:
# 如果是普通脚本
return "."


class YoudaoNoteApi(object):
"""
有道云笔记 API 封装
Expand Down Expand Up @@ -37,8 +49,11 @@ def __init__(self, cookies_path=None):
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"macOS"',
}

self.cookies_path = cookies_path if cookies_path else "cookies.json"
self.cookies_path = (
cookies_path
if cookies_path
else os.path.join(get_script_directory(), "cookies.json")
)
self.cstk = None

def login_by_cookies(self) -> str:
Expand Down
25 changes: 17 additions & 8 deletions core/log.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import logging
import os
import sys
import time
from datetime import datetime

LOG_FORMAT = "%(asctime)s %(levelname)s %(processName)s-%(threadName)s-%(thread)d %(filename)s:%(lineno)d %(funcName)-10s : %(message)s"
DATE_FORMAT = "%Y/%m/%d %H:%M:%S "


def init_logging():
log_dir = "logs"
if not os.path.exists(log_dir):
os.mkdir(log_dir)
def get_script_directory():
"""获取脚本所在的目录"""
if getattr(sys, "frozen", False):
# 如果是打包后的可执行文件
return os.path.dirname(sys.executable)
else:
# 如果是普通脚本
return "."


def init_logging():
log_dir = os.path.join(get_script_directory(), "logs")
os.makedirs(log_dir, exist_ok=True)
log_filename = os.path.join(
log_dir, f"pull-{datetime.now().strftime('%Y%m%d-%H%M%S')}.log"
)
logging.basicConfig(
handlers=[
logging.FileHandler(
"logs/pull-{}.log".format(int(time.time())), "a", encoding="utf-8"
),
logging.FileHandler(log_filename, "a", encoding="utf-8"),
logging.StreamHandler(sys.stdout),
],
level=logging.INFO,
Expand Down
25 changes: 21 additions & 4 deletions pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,21 @@ class FileActionEnum(Enum):
UPDATE = "更新"


def get_script_directory():
"""获取脚本所在的目录"""
if getattr(sys, "frozen", False):
# 如果是打包后的可执行文件
return os.path.dirname(sys.executable)
else:
# 如果是普通脚本
return os.path.dirname(os.path.abspath(__file__))


class YoudaoNotePull(object):
"""
有道云笔记 Pull 封装
"""

CONFIG_PATH = "config.json"

def __init__(self):
self.root_local_dir = None # 本地文件根目录
self.youdaonote_api = None
Expand All @@ -60,7 +68,12 @@ def _covert_config(self, config_path=None) -> Tuple[dict, str]:
:param config_path: config 文件路径
:return: (config_dict, error_msg)
"""
config_path = config_path if config_path else self.CONFIG_PATH

config_path = (
config_path
if config_path
else os.path.join(get_script_directory(), "config.json")
)
with open(config_path, "rb") as f:
config_str = f.read().decode("utf-8")

Expand Down Expand Up @@ -154,7 +167,11 @@ def _judge_type(self, file_id, youdao_file_suffix) -> Enum:
if youdao_file_suffix == MARKDOWN_SUFFIX:
file_type = FileType.MARKDOWN
return file_type
elif youdao_file_suffix == ".note" or youdao_file_suffix == ".clip" or youdao_file_suffix == "":
elif (
youdao_file_suffix == ".note"
or youdao_file_suffix == ".clip"
or youdao_file_suffix == ""
):
response = self.youdaonote_api.get_file_by_id(file_id)
# 2、如果文件以 `<?xml` 开头
if response.content[:5] == b"<?xml":
Expand Down

0 comments on commit 315a308

Please sign in to comment.