zhangliang 2 semanas atrás
pai
commit
b10be1f50e
7 arquivos alterados com 503 adições e 5 exclusões
  1. 10 0
      .dockerignore
  2. 322 0
      .gitignore
  3. 42 0
      Dockerfile
  4. 9 5
      config.py
  5. 10 0
      docker-compose.yml
  6. 62 0
      docker-run.sh
  7. 48 0
      manage.sh

+ 10 - 0
.dockerignore

@@ -0,0 +1,10 @@
+.git
+.gitignore
+README.md
+*.log
+venv/
+__pycache__/
+*.pyc
+.DS_Store
+docker-run.sh
+manage.sh

+ 322 - 0
.gitignore

@@ -0,0 +1,322 @@
+# Created by https://www.toptal.com/developers/gitignore/api/python,node
+# Edit at https://www.toptal.com/developers/gitignore?templates=python,node
+
+### Node ###
+# Logs
+logs
+*.log
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+lerna-debug.log*
+.pnpm-debug.log*
+
+# Diagnostic reports (https://nodejs.org/api/report.html)
+report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
+
+# Runtime data
+pids
+*.pid
+*.seed
+*.pid.lock
+
+# Directory for instrumented libs generated by jscoverage/JSCover
+lib-cov
+
+# Coverage directory used by tools like istanbul
+coverage
+*.lcov
+
+# nyc test coverage
+.nyc_output
+
+# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
+.grunt
+
+# Bower dependency directory (https://bower.io/)
+bower_components
+
+# node-waf configuration
+.lock-wscript
+
+# Compiled binary addons (https://nodejs.org/api/addons.html)
+build/Release
+
+# Dependency directories
+node_modules/
+jspm_packages/
+
+# Snowpack dependency directory (https://snowpack.dev/)
+web_modules/
+
+# TypeScript cache
+*.tsbuildinfo
+
+# Optional npm cache directory
+.npm
+
+# Optional eslint cache
+.eslintcache
+
+# Optional stylelint cache
+.stylelintcache
+
+# Microbundle cache
+.rpt2_cache/
+.rts2_cache_cjs/
+.rts2_cache_es/
+.rts2_cache_umd/
+
+# Optional REPL history
+.node_repl_history
+
+# Output of 'npm pack'
+*.tgz
+
+# Yarn Integrity file
+.yarn-integrity
+
+# dotenv environment variable files
+.env
+.env.development.local
+.env.test.local
+.env.production.local
+.env.local
+
+# parcel-bundler cache (https://parceljs.org/)
+.cache
+.parcel-cache
+
+# Next.js build output
+.next
+out
+
+# Nuxt.js build / generate output
+.nuxt
+dist
+
+# Gatsby files
+.cache/
+# Comment in the public line in if your project uses Gatsby and not Next.js
+# https://nextjs.org/blog/next-9-1#public-directory-support
+# public
+
+# vuepress build output
+.vuepress/dist
+
+# vuepress v2.x temp and cache directory
+.temp
+
+# Docusaurus cache and generated files
+.docusaurus
+
+# Serverless directories
+.serverless/
+
+# FuseBox cache
+.fusebox/
+
+# DynamoDB Local files
+.dynamodb/
+
+# TernJS port file
+.tern-port
+
+# Stores VSCode versions used for testing VSCode extensions
+.vscode-test
+
+# yarn v2
+.yarn/cache
+.yarn/unplugged
+.yarn/build-state.yml
+.yarn/install-state.gz
+.pnp.*
+
+### Node Patch ###
+# Serverless Webpack directories
+.webpack/
+
+# Optional stylelint cache
+
+# SvelteKit build / generate output
+.svelte-kit
+
+### Python ###
+# Byte-compiled / optimized / DLL files
+__pycache__/
+*.py[cod]
+*$py.class
+
+# C extensions
+*.so
+
+# Distribution / packaging
+.Python
+build/
+develop-eggs/
+dist/
+downloads/
+eggs/
+.eggs/
+lib/
+lib64/
+parts/
+sdist/
+var/
+wheels/
+share/python-wheels/
+*.egg-info/
+.installed.cfg
+*.egg
+MANIFEST
+
+# PyInstaller
+#  Usually these files are written by a python script from a template
+#  before PyInstaller builds the exe, so as to inject date/other infos into it.
+*.manifest
+*.spec
+
+# Installer logs
+pip-log.txt
+pip-delete-this-directory.txt
+
+# Unit test / coverage reports
+htmlcov/
+.tox/
+.nox/
+.coverage
+.coverage.*
+nosetests.xml
+coverage.xml
+*.cover
+*.py,cover
+.hypothesis/
+.pytest_cache/
+cover/
+
+# Translations
+*.mo
+*.pot
+
+# Django stuff:
+local_settings.py
+db.sqlite3
+db.sqlite3-journal
+
+# Flask stuff:
+instance/
+.webassets-cache
+
+# Scrapy stuff:
+.scrapy
+
+# Sphinx documentation
+docs/_build/
+
+# PyBuilder
+.pybuilder/
+target/
+
+# Jupyter Notebook
+.ipynb_checkpoints
+
+# IPython
+profile_default/
+ipython_config.py
+
+# pyenv
+#   For a library or package, you might want to ignore these files since the code is
+#   intended to run in multiple environments; otherwise, check them in:
+# .python-version
+
+# pipenv
+#   According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
+#   However, in case of collaboration, if having platform-specific dependencies or dependencies
+#   having no cross-platform support, pipenv may install dependencies that don't work, or not
+#   install all needed dependencies.
+#Pipfile.lock
+
+# poetry
+#   Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
+#   This is especially recommended for binary packages to ensure reproducibility, and is more
+#   commonly ignored for libraries.
+#   https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
+#poetry.lock
+
+# pdm
+#   Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
+#pdm.lock
+#   pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
+#   in version control.
+#   https://pdm.fming.dev/#use-with-ide
+.pdm.toml
+
+# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
+__pypackages__/
+
+# Celery stuff
+celerybeat-schedule
+celerybeat.pid
+
+# SageMath parsed files
+*.sage.py
+
+# Environments
+.venv
+env/
+venv/
+ENV/
+env.bak/
+venv.bak/
+
+# Spyder project settings
+.spyderproject
+.spyproject
+
+# Rope project settings
+.ropeproject
+
+# mkdocs documentation
+/site
+
+# mypy
+.mypy_cache/
+.dmypy.json
+dmypy.json
+
+# Pyre type checker
+.pyre/
+
+# pytype static type analyzer
+.pytype/
+
+# Cython debug symbols
+cython_debug/
+
+# PyCharm
+#  JetBrains specific template is maintained in a separate JetBrains.gitignore that can
+#  be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
+#  and can be added to the global gitignore or merged into this file.  For a more nuclear
+#  option (not recommended) you can uncomment the following to ignore the entire idea folder.
+.idea/
+
+# VScode
+.vscode/
+
+### Python Patch ###
+# Poetry local configuration file - https://python-poetry.org/docs/configuration/#local-configuration
+poetry.toml
+
+# ruff
+.ruff_cache/
+
+# LSP config files
+pyrightconfig.json
+
+# End of https://www.toptal.com/developers/gitignore/api/python,node
+
+cache/
+
+.DS_Store
+
+test/

+ 42 - 0
Dockerfile

@@ -0,0 +1,42 @@
+FROM python:3.10-slim
+
+# 设置工作目录
+WORKDIR /app
+
+# 安装系统依赖(Playwright 所需)
+RUN apt-get update && apt-get install -y \
+    wget \
+    gnupg \
+    libnss3 \
+    libnspr4 \
+    libatk1.0-0 \
+    libatk-bridge2.0-0 \
+    libcups2 \
+    libdrm2 \
+    libdbus-1-3 \
+    libxcb1 \
+    libxdamage1 \
+    libxext6 \
+    libxfixes3 \
+    libxrandr2 \
+    libxcomposite1 \
+    libx11-xcb1 \
+    libxkbcommon0 \
+    libxshmfence1 \
+    && rm -rf /var/lib/apt/lists/*
+
+# 复制项目文件
+COPY requirements.txt .
+COPY *.py ./
+
+# 安装 Python 依赖
+RUN pip install --no-cache-dir -r requirements.txt
+
+# 安装 Playwright 浏览器
+RUN playwright install chromium
+
+# 设置环境变量
+ENV PYTHONUNBUFFERED=1
+
+# 启动应用
+CMD ["python", "run.py"]

+ 9 - 5
config.py

@@ -1,14 +1,18 @@
 # 双任务独立配置
 
 # Redis 配置
+
+import os
+
+# Redis 配置 - 支持环境变量
 REDIS_CONFIG = {
-    'host': 'r-t4n023zec9wyjeer0spd.redis.singapore.rds.aliyuncs.com',
-    'port': 6379,
-    'db': 15,
-    'username': 'denet_crawler',
-    'password': 'denet_crawler2023'
+    "host": os.getenv("REDIS_HOST", "localhost"),
+    "port": int(os.getenv("REDIS_PORT", 6379)),
+    "db": int(os.getenv("REDIS_DB", 0)),
+    "password": os.getenv("REDIS_PASSWORD", None)
 }
 
+
 # 双任务独立配置
 TASKS_CONFIG = {
     # 搜索 Cookie 任务

+ 10 - 0
docker-compose.yml

@@ -0,0 +1,10 @@
+version: '3.8'
+
+services:
+  cookie-job:
+    build: .
+    env_file:
+      - .env
+    volumes:
+      - ./logs:/app/logs
+    restart: unless-stopped

+ 62 - 0
docker-run.sh

@@ -0,0 +1,62 @@
+#!/bin/bash
+# docker-run.sh - Docker 启动脚本(连接远程 Redis)
+
+echo "🐳 使用 Docker 启动抖音 Cookie 生成器(连接远程 Redis)"
+
+# 检查 Docker 是否安装
+if ! command -v docker &> /dev/null; then
+    echo "❌ Docker 未安装,请先安装 Docker"
+    exit 1
+fi
+
+# 检查 docker-compose 是否安装
+if ! command -v docker-compose &> /dev/null; then
+    echo "❌ docker-compose 未安装,请先安装 docker-compose"
+    exit 1
+fi
+
+# 创建日志目录
+mkdir -p logs
+
+# 测试远程 Redis 连接
+echo "🔗 测试远程 Redis 连接..."
+docker run --rm -it \
+  -e REDIS_HOST=r-t4n023zec9wyjeer0spd.redis.singapore.rds.aliyuncs.com \
+  -e REDIS_PORT=6379 \
+  -e REDIS_DB=15 \
+  -e REDIS_USERNAME=denet_crawler \
+  -e REDIS_PASSWORD=denet_crawler2023 \
+  python:3.10-slim \
+  python -c "
+import redis
+try:
+    r = redis.Redis(
+        host='r-t4n023zec9wyjeer0spd.redis.singapore.rds.aliyuncs.com',
+        port=6379,
+        db=15,
+        username='denet_crawler',
+        password='denet_crawler2023',
+        ssl=True,
+        ssl_cert_reqs=None
+    )
+    r.ping()
+    print('✅ Redis 连接成功')
+except Exception as e:
+    print(f'❌ Redis 连接失败: {e}')
+    exit(1)
+"
+
+if [ $? -ne 0 ]; then
+    echo "❌ Redis 连接测试失败,请检查网络和配置"
+    exit 1
+fi
+
+# 构建并启动服务
+echo "🚀 构建和启动 Docker 服务..."
+docker-compose up -d --build
+
+echo "✅ 服务已启动"
+echo "🔗 使用远程 Redis: r-t4n023zec9wyjeer0spd.redis.singapore.rds.aliyuncs.com"
+echo "📊 查看日志: docker-compose logs -f cookie-job"
+echo "🛑 停止服务: docker-compose down"
+echo "🔧 进入容器: docker-compose exec cookie-job bash"

+ 48 - 0
manage.sh

@@ -0,0 +1,48 @@
+#!/bin/bash
+# manage.sh - Docker 服务管理脚本
+
+case $1 in
+    start)
+        docker-compose up -d
+        echo "✅ 服务已启动"
+        ;;
+    stop)
+        docker-compose down
+        echo "✅ 服务已停止"
+        ;;
+    restart)
+        docker-compose restart
+        echo "✅ 服务已重启"
+        ;;
+    status)
+        docker-compose ps
+        ;;
+    logs)
+        docker-compose logs -f cookie-generator
+        ;;
+    build)
+        docker-compose build --no-cache
+        echo "✅ 镜像重建完成"
+        ;;
+    shell)
+        docker-compose exec cookie-generator bash
+        ;;
+    update)
+        git pull
+        docker-compose build --no-cache
+        docker-compose up -d
+        echo "✅ 更新完成"
+        ;;
+    *)
+        echo "用法: $0 {start|stop|restart|status|logs|build|shell|update}"
+        echo ""
+        echo "  start    - 启动服务"
+        echo "  stop     - 停止服务"
+        echo "  restart  - 重启服务"
+        echo "  status   - 查看状态"
+        echo "  logs     - 查看日志"
+        echo "  build    - 重建镜像"
+        echo "  shell    - 进入容器"
+        echo "  update   - 更新代码并重启"
+        ;;
+esac