diff --git a/douyinliverecorder/__init__.py b/douyinliverecorder/__init__.py index 8e6d71c..173195a 100644 --- a/douyinliverecorder/__init__.py +++ b/douyinliverecorder/__init__.py @@ -1,5 +1,14 @@ +import os +import sys from pathlib import Path +from .initializer import check_node current_file_path = Path(__file__).resolve() current_dir = current_file_path.parent JS_SCRIPT_PATH = current_dir / 'javascript' + +execute_dir = os.path.split(os.path.realpath(sys.argv[0]))[0] +node_execute_dir = Path(execute_dir) / 'node' +current_env_path = os.environ.get('PATH') +os.environ['PATH'] = str(node_execute_dir) + os.pathsep + current_env_path +check_node() diff --git a/douyinliverecorder/initializer.py b/douyinliverecorder/initializer.py index c7e39f0..8bc790a 100644 --- a/douyinliverecorder/initializer.py +++ b/douyinliverecorder/initializer.py @@ -10,6 +10,8 @@ import os import subprocess import sys import platform +import zipfile +from pathlib import Path import requests import re import distro @@ -17,6 +19,16 @@ from tqdm import tqdm from .logger import logger current_platform = platform.system() +execute_dir = os.path.split(os.path.realpath(sys.argv[0]))[0] +current_env_path = os.environ.get('PATH') + + +def unzip_file(zip_path, extract_to): + if not os.path.exists(extract_to): + os.makedirs(extract_to) + + with zipfile.ZipFile(zip_path, 'r') as zip_ref: + zip_ref.extractall(extract_to) def install_nodejs_windows(): @@ -25,18 +37,20 @@ def install_nodejs_windows(): logger.debug("Installing the stable version of Node.js for Windows...") response = requests.get('https://nodejs.cn/download/') if response.status_code == 200: - match = re.search('href="(https://npmmirror.com/mirrors/node/v.*?/node-v.*?.msi)"> callable: @functools.wraps(func) @@ -20,15 +17,7 @@ def trace_error_decorator(func: callable) -> callable: try: return func(*args, **kwargs) except execjs.ProgramError: - global is_install_node - if not is_install_node: - is_install_node = True - logger.warning('Failed to execute JS code. Please check if the Node.js environment') - from .initializer import check_node - is_install_node = check_node() - if is_install_node: - time.sleep(3) - os._exit(0) + logger.warning('Failed to execute JS code. Please check if the Node.js environment') except Exception as e: error_line = traceback.extract_tb(e.__traceback__)[-1].lineno error_info = f"错误信息: type: {type(e).__name__}, {str(e)} in function {func.__name__} at line: {error_line}" @@ -119,4 +108,4 @@ def remove_emojis(text, replace_text=r''): "]+", flags=re.UNICODE ) - return emoji_pattern.sub(replace_text, text) + return emoji_pattern.sub(replace_text, text) \ No newline at end of file