mirror of
https://github.com/DrewThomasson/ebook2audiobook.git
synced 2026-01-10 06:18:02 -05:00
following code conversion fixes
This commit is contained in:
11
app.py
11
app.py
@@ -13,9 +13,9 @@ script_mode = NATIVE
|
||||
def check_python_version():
|
||||
current_version = sys.version_info[:2] # (major, minor)
|
||||
if current_version < min_python_version or current_version > max_python_version:
|
||||
error = f"""\033[33m********** Error: Your OS Python version is not compatible! (current: {current_version[0]}.{current_version[1]})
|
||||
error = f"""{color_yellow_start}********** Error: Your OS Python version is not compatible! (current: {current_version[0]}.{current_version[1]})
|
||||
Please create a virtual python environment verrsion {min_python_version[0]}.{min_python_version[1]} or {max_python_version[0]}.{max_python_version[1]}
|
||||
with conda or python -v venv **********\033[0m"""
|
||||
with conda or python -v venv **********{color_yellow_end}"""
|
||||
print(error)
|
||||
return False
|
||||
else:
|
||||
@@ -44,7 +44,6 @@ def check_and_install_requirements(file_path):
|
||||
print("\nInstalling missing packages...")
|
||||
try:
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install", "--upgrade", "pip"] + missing_packages)
|
||||
subprocess.check_call([sys.executable, "-m", "pip", "install"] + missing_packages)
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Failed to install packages: {e}")
|
||||
return False
|
||||
@@ -64,7 +63,7 @@ def check_dictionary():
|
||||
|
||||
if not os.path.isdir(dictionary_path):
|
||||
try:
|
||||
print(f"\033[33m*** No default dictionary found! trying to download it... ***\033[0m")
|
||||
print(f"{color_yellow_start}*** No default dictionary found! trying to download it... ***{color_yellow_end}")
|
||||
subprocess.run(["python", "-m", "unidic", "download"], check=True)
|
||||
print("Successfully downloaded UniDic.")
|
||||
return True
|
||||
@@ -83,7 +82,7 @@ def check_dictionary():
|
||||
return True
|
||||
|
||||
try:
|
||||
print(f"\033[33m*** default spacy model is missing! trying to download it... ***\033[0m")
|
||||
print(f"{color_yellow_start}*** default spacy model is missing! trying to download it... ***{color_yellow_end}")
|
||||
subprocess.run(["python", "-m", "spacy", "download", required_model], check=True)
|
||||
return True
|
||||
except subprocess.CalledProcessError as e:
|
||||
@@ -170,7 +169,7 @@ Linux/Mac:
|
||||
|
||||
# Check if the port is already in use to prevent multiple launches
|
||||
if not args.headless and is_port_in_use(web_interface_port):
|
||||
print(f"\033[33mError: Port {web_interface_port} is already in use. The web interface may already be running.\033[0m")
|
||||
print(f"{color_yellow_start}Error: Port {web_interface_port} is already in use. The web interface may already be running.{color_yellow_end}")
|
||||
sys.exit(1)
|
||||
|
||||
script_mode = args.script_mode if args.script_mode else script_mode
|
||||
|
||||
@@ -8,72 +8,68 @@ set "NATIVE=native"
|
||||
set "DOCKER_UTILS=docker_utils"
|
||||
set "FULL_DOCKER=full_docker"
|
||||
|
||||
set "DOCKER_IMG=utils"
|
||||
|
||||
REM Define paths
|
||||
set "MINICONDA_URL=https://repo.anaconda.com/miniconda/Miniconda3-latest-Windows-x86_64.exe"
|
||||
set "MINICONDA_INSTALLER=%TEMP%\Miniconda3-latest-Windows-x86_64.exe"
|
||||
set "CONDA_PATH=%USERPROFILE%\miniconda3\bin"
|
||||
set "PYTHON_ENV_DOCKER_UTILS=.\python_env"
|
||||
set "DOCKER_UTILS_NAME=utils"
|
||||
set "PYTHON_INSTALL_ENV=python_env"
|
||||
set "CONDA_PATH=%USERPROFILE%\miniconda3\bin"
|
||||
set "INSTALL_DIR=%USERPROFILE%\miniconda3"
|
||||
set "CONDA_STATUS=0"
|
||||
set "PATH=%CONDA_PATH%;%PATH%"
|
||||
set "PYTHON_ENV_NATIVE=native_env"
|
||||
set "PYTHON_ENV_DOCKER_UTILS=python_env"
|
||||
|
||||
REM List of programs to check
|
||||
set "REQUIRED_PROGRAMS=calibre ffmpeg"
|
||||
|
||||
REM Initialize the return code
|
||||
set "RETURN_CODE=0"
|
||||
|
||||
REM Loop through the list of programs and check if they are installed
|
||||
set "PROGRAMS_STATUS=0"
|
||||
for %%p in (%REQUIRED_PROGRAMS%) do (
|
||||
where %%p >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
echo %%p is not installed
|
||||
set RETURN_CODE=1
|
||||
goto after_check
|
||||
echo %%p is not installed. Install %%p manually or with install.bat with Administrator level.
|
||||
set PROGRAMS_STATUS=1
|
||||
goto check_external_programs
|
||||
)
|
||||
)
|
||||
|
||||
:after_check
|
||||
REM Check the result of the program check and display appropriate messages
|
||||
if %RETURN_CODE%==0 (
|
||||
echo All required programs are installed.
|
||||
:check_external_programs
|
||||
if %PROGRAMS_STATUS%==0 (
|
||||
REM Set SCRIPT_MODE to NATIVE if all programs are installed
|
||||
set SCRIPT_MODE=%NATIVE%
|
||||
) else (
|
||||
echo Use install.bat as Administrator to install everything needed.!
|
||||
)
|
||||
|
||||
REM Function to check if Docker is installed and running
|
||||
set "RETURN_CODE=0"
|
||||
|
||||
REM Check if Docker is installed by checking if 'docker' command is available
|
||||
set "DOCKER_STATUS=0"
|
||||
where docker >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
echo Docker is not installed.
|
||||
set "RETURN_CODE=1"
|
||||
goto after_check_docker
|
||||
echo %%p Docker is not installed. Install %%p manually or with install.bat with Administrator level.
|
||||
set "DOCKER_STATUS=1"
|
||||
goto check_docker
|
||||
)
|
||||
if %DOCKER_STATUS%==0 (
|
||||
REM Check if Docker service is running
|
||||
for /f "tokens=*" %%i in ('docker info 2^>nul') do (
|
||||
if "%%i"=="" (
|
||||
echo %%p Docker is not running
|
||||
set "DOCKER_STATUS=1"
|
||||
goto check_docker
|
||||
)
|
||||
)
|
||||
) else (
|
||||
echo Docker is installed.
|
||||
echo %%p Use install.bat to install everything needed.
|
||||
)
|
||||
|
||||
REM Check if Docker service is running
|
||||
for /f "tokens=*" %%i in ('docker info 2^>nul') do (
|
||||
if not "%%i"=="" (
|
||||
echo Docker is running.
|
||||
goto after_check_docker
|
||||
)
|
||||
)
|
||||
|
||||
REM If no output from docker info, Docker is not running
|
||||
echo Docker is not running.
|
||||
set "RETURN_CODE=1"
|
||||
|
||||
:after_check_docker
|
||||
REM Continue the script based on Docker status
|
||||
if %RETURN_CODE%==0 (
|
||||
if exist .\python_env (
|
||||
echo Running in docker utils mode
|
||||
for /f "delims=" %%i in ('cd') do set "PYTHON_INSTALL_ENV=%%i\python_env"
|
||||
:check_docker
|
||||
if %DOCKER_STATUS%==0 (
|
||||
if exist %PYTHON_ENV_DOCKER_UTILS% (
|
||||
echo %%p Running in docker utils mode
|
||||
for /f "delims=" %%i in ('cd') do set "PYTHON_ENV_DOCKER_UTILS=%%i\python_env"
|
||||
set "SCRIPT_MODE=%DOCKER_UTILS%"
|
||||
) else (
|
||||
REM Check if the script is running in a Docker container
|
||||
if defined container (
|
||||
echo Running in full docker mode
|
||||
echo %%p Running in full docker mode
|
||||
set "SCRIPT_MODE=%FULL_DOCKER%"
|
||||
) else (
|
||||
set SCRIPT_MODE=%NATIVE%
|
||||
@@ -81,33 +77,55 @@ if %RETURN_CODE%==0 (
|
||||
)
|
||||
)
|
||||
|
||||
REM Check the script mode and handle accordingly
|
||||
if "%SCRIPT_MODE%"=="%NATIVE%" (
|
||||
python app.py --script_mode "%NATIVE%" %*
|
||||
) else if "%SCRIPT_MODE%"=="%DOCKER_UTILS%" (
|
||||
set "CONDA_PATH=%USERPROFILE%\miniconda3\bin"
|
||||
set "PATH=%CONDA_PATH%;%PATH%"
|
||||
|
||||
REM Check if Conda is installed
|
||||
where conda >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
echo Conda is not installed. Please install it first.
|
||||
) else (
|
||||
REM Check if the Docker image exists
|
||||
docker images -q %DOCKER_UTILS_NAME% >nul 2>&1
|
||||
if errorlevel 0 (
|
||||
REM Activate the Conda environment and run the Python app
|
||||
call conda activate %PYTHON_INSTALL_ENV%
|
||||
python app.py --script_mode "docker_utils" %*
|
||||
call conda deactivate
|
||||
where conda >nul 2>&1
|
||||
if errorlevel 1 (
|
||||
echo Miniconda is not installed!
|
||||
set "CONDA_STATUS=1"
|
||||
goto check_conda
|
||||
)
|
||||
:check_conda
|
||||
if %CONDA_STATUS%==1 (
|
||||
if not "%SCRIPT_MODE%"=="%FULL_DOCKER%" (
|
||||
echo Downloading Miniconda installer...!
|
||||
bitsadmin /transfer "MinicondaDownload" %MINICONDA_URL% "%MINICONDA_INSTALLER%"
|
||||
|
||||
echo Installing Miniconda...!
|
||||
"%MINICONDA_INSTALLER%" /InstallationType=JustMe /RegisterPython=0 /AddToPath=1 /S /D=%INSTALL_DIR%
|
||||
|
||||
REM Verify installation by checking if conda.bat exists
|
||||
if exist "%INSTALL_DIR%\condabin\conda.bat" (
|
||||
echo Miniconda installed successfully.!
|
||||
set "CONDA_STATUS=0"
|
||||
) else (
|
||||
echo Docker image '%DOCKER_UTILS_NAME%' not found. Please build or pull the image.
|
||||
echo !ESC![31mMiniconda installation failed.!
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
REM Check the script mode and handle accordingly
|
||||
set "PATH=%INSTALL_DIR%\condabin;%PATH%"
|
||||
if "%SCRIPT_MODE%"=="%NATIVE%" (
|
||||
if %CONDA_STATUS%==0 (
|
||||
call conda create --name %PYTHON_ENV_NATIVE% python=3.11 -y
|
||||
call conda activate %PYTHON_ENV_NATIVE%
|
||||
python app.py --script_mode "%NATIVE%" %*
|
||||
call conda deactivate
|
||||
)
|
||||
) else if "%SCRIPT_MODE%"=="%DOCKER_UTILS%" (
|
||||
REM Check if the Docker image exists
|
||||
docker images -q %DOCKER_IMG% >nul 2>&1
|
||||
if errorlevel 0 (
|
||||
call conda create --prefix .\%PYTHON_ENV_DOCKER_UTILS% python=3.11 -y
|
||||
call conda activate .\%PYTHON_ENV_DOCKER_UTILS%
|
||||
python app.py --script_mode "docker_utils" %*
|
||||
call conda deactivate
|
||||
) else (
|
||||
echo Docker image '%DOCKER_IMG%' not found. Please build or pull the image.
|
||||
)
|
||||
) else if "%SCRIPT_MODE%"=="%FULL_DOCKER%" (
|
||||
python app.py --script_mode "%FULL_DOCKER%" %*
|
||||
) else (
|
||||
echo ebook2audiobook is not correctly installed. Try running install.bat again.
|
||||
echo %%p ebook2audiobook is not correctly installed. Try running the installation from install.bat
|
||||
)
|
||||
|
||||
pause REM Keep the terminal open after the script finishes
|
||||
pause
|
||||
|
||||
@@ -78,6 +78,7 @@ elif [ "$SCRIPT_MODE" = "$DOCKER_UTILS" ]; then
|
||||
elif command -v docker &> /dev/null; then
|
||||
if [[ "$(docker images -q $DOCKER_UTILS_NAME 2> /dev/null)" != "" ]]; then
|
||||
source $(conda info --base)/etc/profile.d/conda.sh
|
||||
conda create --prefix $PYTHON_ENV_DOCKER_UTILS python=3.11 -y
|
||||
conda activate $PYTHON_INSTALL_ENV
|
||||
python app.py "$@"
|
||||
python app.py --script_mode "docker_utils" $PARAMS
|
||||
|
||||
@@ -21,4 +21,7 @@ audiobooks_dir = os.path.abspath("./audiobooks")
|
||||
processes_dir = os.path.abspath("./tmp")
|
||||
|
||||
supported_ebook_formats = ['.epub', '.mobi', '.azw3', 'fb2', 'lrf', 'rb', 'snb', 'tcr', '.pdf', '.txt', '.rtf', '.docx', '.html', '.odt', '.azw']
|
||||
final_format = "m4b"
|
||||
final_format = "m4b"
|
||||
|
||||
color_yellow_start = "" if os.name == "nt" else "\\033[33m"
|
||||
color_yellow_end = "" if os.name == "nt" else "\\033[0m"
|
||||
@@ -96,8 +96,8 @@ def check_program_installed(program_name, command, options):
|
||||
subprocess.run([command, options], check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
return True, None
|
||||
except FileNotFoundError:
|
||||
e = f"""\033[33m********** Error: {program_name} is not installed! if your OS calibre package version
|
||||
is not compatible you still can install ebook2audiobook via install.sh (linux/mac) or install.bat (windows) **********\033[0m"""
|
||||
e = f"""{color_yellow_start}********** Error: {program_name} is not installed! if your OS calibre package version
|
||||
is not compatible you still can install ebook2audiobook via install.sh (linux/mac) or install.bat (windows) **********{color_yellow_end}"""
|
||||
raise DependencyError(e)
|
||||
except subprocess.CalledProcessError:
|
||||
e = f"Error: There was an issue running {program_name}."
|
||||
@@ -113,7 +113,7 @@ def remove_conflict_pkg(pkg):
|
||||
break
|
||||
if package_location is not None:
|
||||
try:
|
||||
print(f"\033[33m*** {pkg} is in conflict with an external OS python library, trying to solve it....***\033[0m")
|
||||
print(f"{color_yellow_start}*** {pkg} is in conflict with an external OS python library, trying to solve it....***{color_yellow_end}")
|
||||
result = subprocess.run(["pip", 'uninstall', pkg, '-y'], env={}, stdout=subprocess.PIPE, text=True, check=True)
|
||||
except subprocess.CalledProcessError as e:
|
||||
raise DependencyError(e)
|
||||
@@ -1160,4 +1160,4 @@ def web_interface(mode, share, ui_needed):
|
||||
if local_ip != "127.0.0.1":
|
||||
print(f"* Running on local URL: http://127.0.0.1:{web_interface_port}")
|
||||
|
||||
demo.launch(server_name=local_ip, server_port=web_interface_port, share=share)
|
||||
demo.launch(server_name="0.0.0.0", server_port=web_interface_port, share=share)
|
||||
Reference in New Issue
Block a user