From 147dbcb71dd0a31194b6768d8578c9921d42913a Mon Sep 17 00:00:00 2001 From: Evil0ctal Date: Sat, 27 Apr 2024 15:36:00 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B:=20=E4=BF=AE=E5=A4=8D=E9=83=A8?= =?UTF-8?q?=E7=BD=B2=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 43 ++++++--- README.md | 107 +++++++++++++++------- bash/install.sh | 46 ++++++---- bash/update.sh | 41 ++++++--- config.yaml | 2 +- daemon/Douyin_TikTok_Download_API.service | 6 +- docker-compose.yml | 2 +- start.py | 2 +- start.sh | 10 +- 9 files changed, 179 insertions(+), 80 deletions(-) diff --git a/Dockerfile b/Dockerfile index 39008e4..6bc555f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,22 +1,39 @@ +# Use the official Ubuntu base image FROM ubuntu:jammy -MAINTAINER jwstar +LABEL maintainer="Evil0ctal" + +# Set non-interactive frontend (useful for Docker builds) ENV DEBIAN_FRONTEND=noninteractive -RUN apt-get -y update \ - && apt-get install -y --no-install-recommends \ - python3.11 python3-pip python3.11-dev +# Update the package list and install Python and pip +RUN apt-get update && apt-get install -y --no-install-recommends \ + python3.11 \ + python3-pip \ + python3.11-dev \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* -# Using Aliyun pipy mirror -RUN pip3 install -i https://mirrors.aliyun.com/pypi/simple/ -U pip -RUN pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/ - -COPY . /app +# Set a working directory WORKDIR /app -RUN pip3 --no-cache-dir install --user -r /app/requirements.txt +# Copy the application source code to the container +COPY . /app -RUN chmod +x start.sh && \ - apt-get autoremove -y \ - && apt-get remove -y python3-pip +# Install virtualenv +RUN pip3 install -i https://mirrors.aliyun.com/pypi/simple/ -U pip \ + && pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/ \ + && pip3 install virtualenv +# Create and activate virtual environment +# Using a virtual environment prevents conflicts between the app's dependencies and the system +RUN python3.11 -m virtualenv venv +ENV PATH="/app/venv/bin:$PATH" + +# Install dependencies in the virtual environment +RUN pip install --no-cache-dir -r requirements.txt + +# Make the start script executable +RUN chmod +x start.sh + +# Command to run on container start CMD ["./start.sh"] diff --git a/README.md b/README.md index 6b3b1e9..9eb8fc5 100644 --- a/README.md +++ b/README.md @@ -260,7 +260,7 @@ https://www.tiktok.com/@evil0ctal/video/7156033831819037994 > 💡提示:最好将本项目部署至美国地区的服务器,否则可能会出现奇怪的BUG。 -推荐大家使用[Digitalocean](https://www.digitalocean.com/)的服务器,主要是因为免费。 +推荐大家使用[Digitalocean](https://www.digitalocean.com/)的服务器,因为可以白嫖。 使用我的邀请链接注册,你可以获得$200的credit,当你在上面消费$25时,我也可以获得$25的奖励。 @@ -270,6 +270,8 @@ https://www.tiktok.com/@evil0ctal/video/7156033831819037994 > 使用脚本一键部署本项目 +- 本项目提供了一键部署脚本,可以在服务器上快速部署本项目。 +- 脚本是在Ubuntu 20.04 LTS上测试的,其他系统可能会有问题,如果有问题请自行解决。 - 使用wget命令下载[install.sh](https://raw.githubusercontent.com/Evil0ctal/Douyin_TikTok_Download_API/main/bash/install.sh)至服务器并运行 ``` @@ -278,60 +280,103 @@ wget -O install.sh https://raw.githubusercontent.com/Evil0ctal/Douyin_TikTok_Dow > 开启/停止服务 -- `systemctl start/stop Douyin_TikTok_Download_API.service` +- 使用以下命令来控制服务的运行或停止: + - `sudo systemctl start Douyin_TikTok_Download_API.service` + - `sudo systemctl stop Douyin_TikTok_Download_API.service` > 开启/关闭开机自动运行 -- `systemctl enable/disable Douyin_TikTok_Download_API.service` +- 使用以下命令来设置服务开机自动运行或取消开机自动运行: + - `sudo systemctl enable Douyin_TikTok_Download_API.service` + - `sudo systemctl disable Douyin_TikTok_Download_API.service` > 更新项目 +- 项目更新时,确保更新脚本在虚拟环境中执行,更新所有依赖。进入项目bash目录并运行update.sh: - `cd /www/wwwroot/Douyin_TikTok_Download_API/bash && sudo bash update.sh` ## 💽部署(方式二 Docker) -> 💡Docker Image repo: [Docker Hub](https://hub.docker.com/repository/docker/evil0ctal/douyin_tiktok_download_api) +> 💡提示:Docker部署是最简单的部署方式,适合不熟悉Linux的用户,这种方法适合保证环境一致性、隔离性和快速设置。 +> 请使用能正常访问Douyin或TikTok的服务器,否则可能会出现奇怪的BUG。 -- 安装docker +### 准备工作 -```yaml -curl -fsSL get.docker.com -o get-docker.sh&&sh get-docker.sh &&systemctl enable docker&&systemctl start docker +开始之前,请确保您的系统已安装Docker。如果还未安装Docker,可以从[Docker官方网站](https://www.docker.com/products/docker-desktop/)下载并安装。 + +### 步骤1:拉取Docker镜像 + +首先,从Docker Hub拉取最新的Douyin_TikTok_Download_API镜像。 + +```bash +docker pull evil0ctal/douyin_tiktok_download_api:latest ``` -- 留下config.ini和docker-compose.yml文件即可 -- 运行命令,让容器在后台运行 +如果需要,可以替换`latest`为你需要部署的具体版本标签。 -```yaml -docker-compose up -d +### 步骤2:运行Docker容器 + +拉取镜像后,您可以从此镜像启动一个容器。以下是运行容器的命令,包括基本配置: + +```bash +docker run -d --name douyin_tiktok_api -p 80:80 evil0ctal/douyin_tiktok_download_api ``` -- 查看容器日志 +这个命令的每个部分作用如下: -```yaml -docker logs -f douyin_tiktok_download_api +* `-d`:在后台运行容器(分离模式)。 +* `--name douyin_tiktok_api `:将容器命名为`douyin_tiktok_api `。 +* `-p 80:80`:将主机上的80端口映射到容器的80端口。根据您的配置或端口可用性调整端口号。 +* `evil0ctal/douyin_tiktok_download_api`:要使用的Docker镜像名称。 + +### 步骤3:验证容器是否运行 + +使用以下命令检查您的容器是否正在运行: + +```bash +docker ps ``` -- 删除容器 +这将列出所有活动容器。查找`douyin_tiktok_api `以确认其正常运行。 -```yaml -docker rm -f douyin_tiktok_download_api +### 步骤4:访问应用程序 + +容器运行后,您应该能够通过`http://localhost`或API客户端访问Douyin_TikTok_Download_API。如果配置了不同的端口或从远程位置访问,请调整URL。 + +### 可选:自定义Docker命令 + +对于更高级的部署,您可能希望自定义Docker命令,包括环境变量、持久数据的卷挂载或其他Docker参数。这是一个示例: + +```bash +docker run -d --name douyin_tiktok_api -p 80:80 \ + -v /path/to/your/data:/data \ + -e MY_ENV_VAR=my_value \ + evil0ctal/douyin_tiktok_download_api ``` -- 更新 +* `-v /path/to/your/data:/data`:将主机上的`/path/to/your/data`目录挂载到容器的`/data`目录,用于持久化或共享数据。 +* `-e MY_ENV_VAR=my_value`:在容器内设置环境变量`MY_ENV_VAR`,其值为`my_value`。 -```yaml -docker-compose pull && docker-compose down && docker-compose up -d +### 配置文件修改 + +项目的大部分配置可以在以下几个目录中的`config.yaml`文件进行修改: + +* `/crawlers/douyin/web/config.yaml` +* `/crawlers/tiktok/web/config.yaml` +* `/crawlers/tiktok/app/config.yaml` + +### 步骤5:停止并移除容器 + +需要停止和移除容器时,使用以下命令: + +```bash +# Stop +docker stop douyin_tiktok_api + +# Remove +docker rm douyin_tiktok_api ``` -## ❤️ 贡献者 - -[![](https://github.com/Evil0ctal.png?size=50)](https://github.com/Evil0ctal) -[![](https://github.com/jw-star.png?size=50)](https://github.com/jw-star) -[![](https://github.com/Jeffrey-deng.png?size=50)](https://github.com/Jeffrey-deng) -[![](https://github.com/chris-ss.png?size=50)](https://github.com/chris-ss) -[![](https://github.com/weixuan00.png?size=50)](https://github.com/weixuan00) -[![](https://github.com/Tairraos.png?size=50)](https://github.com/Tairraos) - ## 📸截图 ***API速度测试(对比官方API)*** @@ -372,8 +417,8 @@ Web main interface: [![Star History Chart](https://api.star-history.com/svg?repos=Evil0ctal/Douyin_TikTok_Download_API&type=Timeline)](https://star-history.com/#Evil0ctal/Douyin_TikTok_Download_API&Timeline) -[MIT License](https://github.com/Evil0ctal/Douyin_TikTok_Download_API/blob/Stable/LICENSE) +[Apache-2.0 license](https://github.com/Evil0ctal/Douyin_TikTok_Download_API/blob/Stable/LICENSE) > Start: 2021/11/06 + > GitHub: [@Evil0ctal](https://github.com/Evil0ctal) -> Contact: Evil0ctal1985@gmail.com diff --git a/bash/install.sh b/bash/install.sh index 7a1564a..1a6bbab 100644 --- a/bash/install.sh +++ b/bash/install.sh @@ -1,38 +1,52 @@ #!/bin/bash -echo 'installing Git...' +# Set script to exit on any errors. +set -e -apt-get install git +echo 'Updating package lists...' +sudo apt-get update -echo 'installing Python3...' +echo 'Installing Git...' +sudo apt-get install -y git -apt install python3 +echo 'Installing Python3...' +sudo apt install -y python3 -echo 'installing PIP3...' +echo 'Installing PIP3...' +sudo apt install -y python3-pip -apt install python3-pip +echo 'Installing Virtualenv...' +sudo pip3 install virtualenv echo 'Creating path: /www/wwwroot' - -mkdir -p /www/wwwroot +sudo mkdir -p /www/wwwroot cd /www/wwwroot || exit echo 'Cloning Douyin_TikTok_Download_API.git from Github!' - -git clone https://github.com/Evil0ctal/Douyin_TikTok_Download_API.git +sudo git clone https://github.com/Evil0ctal/Douyin_TikTok_Download_API.git cd Douyin_TikTok_Download_API/ || exit -sudo pip install -r requirements.txt --break-system-packages +echo 'Creating a virtual environment' +virtualenv venv -echo 'Add Douyin_TikTok_Download_API to system service' +echo 'Activating the virtual environment' +source venv/bin/activate -cp /www/wwwroot/Douyin_TikTok_Download_API/daemon/* /etc/systemd/system/ +echo 'Installing dependencies from requirements.txt' +pip install -r requirements.txt -systemctl enable Douyin_TikTok_Download_API.service +echo 'Deactivating the virtual environment' +deactivate + +echo 'Adding Douyin_TikTok_Download_API to system service' +sudo cp daemon/* /etc/systemd/system/ + +echo 'Enabling Douyin_TikTok_Download_API service' +sudo systemctl enable Douyin_TikTok_Download_API.service echo 'Starting Douyin_TikTok_Download_API service' +sudo systemctl start Douyin_TikTok_Download_API.service -systemctl start Douyin_TikTok_Download_API.service - +echo 'Douyin_TikTok_Download_API installation complete!' diff --git a/bash/update.sh b/bash/update.sh index a04f8a0..47b8ac3 100644 --- a/bash/update.sh +++ b/bash/update.sh @@ -1,16 +1,31 @@ #!/bin/bash +# Ask for confirmation to proceed with the update read -r -p "Do you want to update Douyin_TikTok_Download_API? [y/n] " input - case $input in - [yY]) - cd .. - git pull - echo "Restarting Douyin_TikTok_Download_API service" - systemctl restart Douyin_TikTok_Download_API.service - echo "Successfully restarted all services!" - ;; - [nN]| *) - echo "Exiting..." - exit 1 - ;; - esac \ No newline at end of file +case $input in + [yY]) + # Navigate to the project directory or exit if it fails + cd /www/wwwroot/Douyin_TikTok_Download_API || { echo "The directory does not exist."; exit 1; } + + # Pull the latest changes from the repository + git pull + + # Activate the virtual environment + source venv/bin/activate + + # Optionally, update Python dependencies + pip install -r requirements.txt + + # Deactivate the virtual environment + deactivate + + # Restart the service to apply changes + echo "Restarting Douyin_TikTok_Download_API service" + sudo systemctl restart Douyin_TikTok_Download_API.service + echo "Successfully restarted all services!" + ;; + [nN]|*) + echo "Exiting..." + exit 1 + ;; +esac diff --git a/config.yaml b/config.yaml index 64caf3f..fef8f7c 100644 --- a/config.yaml +++ b/config.yaml @@ -24,7 +24,7 @@ Web: # API API: # Network Configuration - Host_IP: 0.0.0.0 # use 127.0.0.1 for local access only | 仅本地访问请使用 + Host_IP: 0.0.0.0 # default IP | 默认IP Host_Port: 80 # default port is 80 | 默认端口为80 Docs_URL: /docs # API documentation URL | API文档URL Redoc_URL: /redoc # API documentation URL | API文档URL diff --git a/daemon/Douyin_TikTok_Download_API.service b/daemon/Douyin_TikTok_Download_API.service index 40b6985..2e48fce 100644 --- a/daemon/Douyin_TikTok_Download_API.service +++ b/daemon/Douyin_TikTok_Download_API.service @@ -1,13 +1,13 @@ [Unit] -Description=Douyin_TikTok_Download_API deamon -After=rc-local.service +Description=Douyin_TikTok_Download_API daemon +After=network.target [Service] Type=simple User=root Group=root WorkingDirectory=/www/wwwroot/Douyin_TikTok_Download_API -ExecStart=python3 start.py +ExecStart=/www/wwwroot/Douyin_TikTok_Download_API/venv/bin/python3 start.py Restart=always [Install] diff --git a/docker-compose.yml b/docker-compose.yml index ddd5455..c32abf3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,7 +6,7 @@ services: # 定义服务列表 network_mode: host # 使用主机网络模式 container_name: douyin_tiktok_download_api # 容器名称 restart: always # 容器退出后总是重启 - volumes: # 挂载卷配置 + volumes: # 挂载卷配置 - ./douyin_tiktok_download_api/douyin_web/config.yaml:/crawlers/douyin/web/config.yaml - ./douyin_tiktok_download_api/tiktok_web/config.yaml:/crawlers/tiktok/web/config.yaml - ./douyin_tiktok_download_api/tiktok_app/config.yaml:/crawlers/tiktok/app/config.yaml diff --git a/start.py b/start.py index 9190f25..9fe9a24 100644 --- a/start.py +++ b/start.py @@ -34,7 +34,7 @@ # ============================================================================== -from app.main import app, Host_IP, Host_Port +from app.main import Host_IP, Host_Port import uvicorn if __name__ == '__main__': diff --git a/start.sh b/start.sh index 213c870..6c40826 100644 --- a/start.sh +++ b/start.sh @@ -1,3 +1,11 @@ #!/bin/sh -python3 start.py +# Activating the virtual environment +# shellcheck disable=SC2039 +source /www/wwwroot/Douyin_TikTok_Download_API/venv/bin/activate + +# Starting the Python application +python start.py + +# Deactivating the virtual environment (optional, since the script is ending) +deactivate