From 34e038784f4b8fc532e54bf62e8d40e38e55b1a2 Mon Sep 17 00:00:00 2001 From: ihmily <114978440+ihmily@users.noreply.github.com> Date: Tue, 22 Oct 2024 05:32:18 +0800 Subject: [PATCH] feat: add nodejs install and optimize log print --- douyinliverecorder/initializer.py | 189 ++++++++++++++++++++++++++++++ douyinliverecorder/logger.py | 18 +++ douyinliverecorder/room.py | 4 +- douyinliverecorder/utils.py | 16 ++- main.py | 2 +- pyproject.toml | 10 +- requirements.txt | 4 +- setup.py | 8 +- 8 files changed, 237 insertions(+), 14 deletions(-) create mode 100644 douyinliverecorder/initializer.py diff --git a/douyinliverecorder/initializer.py b/douyinliverecorder/initializer.py new file mode 100644 index 0000000..c7e39f0 --- /dev/null +++ b/douyinliverecorder/initializer.py @@ -0,0 +1,189 @@ +# -*- coding: utf-8 -*- + +""" +Author: Hmily +GitHub:https://github.com/ihmily +Copyright (c) 2024 by Hmily, All Rights Reserved. +""" + +import os +import subprocess +import sys +import platform +import requests +import re +import distro +from tqdm import tqdm +from .logger import logger + +current_platform = platform.system() + + +def install_nodejs_windows(): + try: + logger.warning("Node.js is not installed.") + 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)"> = (3, 7): + res = wrapper(*args, **kwargs) + else: + res = wrapper(*args, **kwargs) + if not res: + install_nodejs() + res = wrapper(*args, **kwargs) + + if not res: + raise RuntimeError("Node.js is not installed.") + + return func(*args, **kwargs) + + return wrapped_func + + +def check_nodejs_installed(): + try: + result = subprocess.run(['node', '-v'], capture_output=True) + version = result.stdout.strip() + if result.returncode == 0 and version: + return True + except FileNotFoundError: + pass + return False + + +def check_node(): + res = check_nodejs_installed() + if not res: + return install_nodejs() diff --git a/douyinliverecorder/logger.py b/douyinliverecorder/logger.py index c6236dd..a46fa69 100644 --- a/douyinliverecorder/logger.py +++ b/douyinliverecorder/logger.py @@ -3,9 +3,24 @@ import os import sys +custom_format = "{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} - {message}" +os.environ["LOGURU_FORMAT"] = custom_format from loguru import logger script_path = os.path.split(os.path.realpath(sys.argv[0]))[0] + +logger.add( + f"{script_path}/logs/DouyinLiveRecorder.log", + level="DEBUG", + format="{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {name}:{function}:{line} - {message}", + filter=lambda i: i["level"].name == "DEBUG", + serialize=False, + enqueue=True, + retention=1, + rotation="100 KB", + encoding='utf-8' +) + logger.add( f"{script_path}/logs/PlayURL.log", level="INFO", @@ -15,13 +30,16 @@ logger.add( enqueue=True, retention=1, rotation="300 KB", + encoding='utf-8' ) logger.add( f"{script_path}/logs/DouyinLiveRecorder.log", level="WARNING", + format="{time:YYYY-MM-DD HH:mm:ss.SSS} | {level: <8} | {name}:{function}:{line} - {message}", serialize=False, enqueue=True, retention=1, rotation="100 KB", + encoding='utf-8' ) diff --git a/douyinliverecorder/room.py b/douyinliverecorder/room.py index c878c00..86f334b 100644 --- a/douyinliverecorder/room.py +++ b/douyinliverecorder/room.py @@ -2,7 +2,7 @@ """ Author: Hmily -Github:https://github.com/ihmily +GitHub:https://github.com/ihmily Date: 2023-07-17 23:52:05 Update: 2024-10-08 23:35:00 Copyright (c) 2023 by Hmily, All Rights Reserved. @@ -127,4 +127,4 @@ if __name__ == '__main__': room_url = "https://v.douyin.com/iQLgKSj/" _room_id, sec_uid = get_sec_user_id(room_url) web_rid = get_live_room_id(_room_id, sec_uid) - print("return web_rid:", web_rid) \ No newline at end of file + print("return web_rid:", web_rid) diff --git a/douyinliverecorder/utils.py b/douyinliverecorder/utils.py index ca79068..777d6d7 100644 --- a/douyinliverecorder/utils.py +++ b/douyinliverecorder/utils.py @@ -4,18 +4,31 @@ import os import functools import hashlib import re +import time import traceback from typing import Union, Any - +import execjs from .logger import logger import configparser +is_install_node = False + def trace_error_decorator(func: callable) -> callable: @functools.wraps(func) def wrapper(*args: list, **kwargs: dict) -> Any: 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) 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}" @@ -57,7 +70,6 @@ def read_config_value(file_path, section, key) -> Union[str, None]: def update_config(file_path, section, key, new_value) -> None: - config = configparser.ConfigParser() try: diff --git a/main.py b/main.py index dd78368..a9dfd43 100644 --- a/main.py +++ b/main.py @@ -241,7 +241,7 @@ def adjust_max_request(): if pre_max_request != max_request: pre_max_request = max_request - print(f"同一时间访问网络的线程数动态改为 {max_request}") + print(f"\r同一时间访问网络的线程数动态改为 {max_request}") error_window.append(error_count) if len(error_window) > error_window_size: diff --git a/pyproject.toml b/pyproject.toml index 69c2d2c..2cee37f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [project] -requires-python = ">=3.8,<3.13" +requires-python = ">=3.10,<3.13" [build-system] requires = ["poetry-core>=1.0.0"] @@ -7,7 +7,7 @@ build-backend = "poetry.core.masonry.api" [tool.poetry] name = "douyinliverecorder" -version = "3.0.8" +version = "3.0.9" description = "An easy tool for recording live streams" authors = ["Hmily"] license = "MIT" @@ -17,8 +17,10 @@ repository = "https://github.com/ihmily/DouyinLiveRecorder" keywords = ["douyin", "live", "recorder"] [tool.poetry.dependencies] -python = "^3.8" +python = "^3.10" requests = "^2.25.1" PyExecJS = "^1.5.1" loguru = "^0.5.3" -pycryptodome = "^3.10.1" \ No newline at end of file +pycryptodome = "^3.10.1" +distro = "^1.9.0" +tqdm = "^4.66.5" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 7fc0fd1..8726a5c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,6 @@ requests PyExecJS loguru==0.7.2 -pycryptodome==3.20.0 \ No newline at end of file +pycryptodome==3.20.0 +distro==1.9.0 +tqdm==4.66.5 \ No newline at end of file diff --git a/setup.py b/setup.py index e65524c..fb9f987 100644 --- a/setup.py +++ b/setup.py @@ -3,7 +3,7 @@ from setuptools import setup, find_packages setup( name='douyinliverecorder', - version='3.0.8', + version='3.0.9', author='Hmily', description='An easy tool for recording live streams', long_description=open('README.md', encoding='utf-8').read(), @@ -14,15 +14,15 @@ setup( 'requests', 'PyExecJS', 'loguru', - 'pycryptodome' + 'pycryptodome', + 'distro', + 'tqdm' ], classifiers=[ 'Development Status :: 3 - Alpha', 'Intended Audience :: Developers', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3 :: Only', - 'Programming Language :: Python :: 3.8', - 'Programming Language :: Python :: 3.9', 'Programming Language :: Python :: 3.10', 'Programming Language :: Python :: 3.11', 'Programming Language :: Python :: 3.12',