diff --git a/core/api.py b/core/api.py index 7677a1d..ad1a82e 100644 --- a/core/api.py +++ b/core/api.py @@ -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 封装 @@ -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: diff --git a/core/log.py b/core/log.py index 5b870fc..fa12fa7 100644 --- a/core/log.py +++ b/core/log.py @@ -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, diff --git a/pull.py b/pull.py index ecebebd..74316ca 100644 --- a/pull.py +++ b/pull.py @@ -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 @@ -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") @@ -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、如果文件以 `