#!/bin/bash
# 青龙面板定时任务脚本 - CryptoAI加密货币分析

# 设置工作目录
WORKDIR="/ql/data/scripts/cryptoai"

# 如果目录不存在则退出
if [ ! -d "$WORKDIR" ]; then
  echo "工作目录不存在，请先将CryptoAI项目上传到青龙容器内的 /ql/data/scripts/cryptoai 路径"
  exit 1
fi

# 进入工作目录
cd "$WORKDIR"

# 设置Python路径
export PYTHONPATH=$WORKDIR

# 设置环境变量
# 数据库配置
export DB_HOST="gz-cynosdbmysql-grp-2j1cnopr.sql.tencentcdb.com"
export DB_PORT="27469"
export DB_USER="root"
export DB_PASSWORD="Aa#223388"
export DB_NAME="cryptoai"

# API配置 - 请替换为自己的API密钥
export BINANCE_API_KEY="HCpeel8g6fsTK2630b7BvGBcS09Z3qfXkLVcAY2JkpaiMm1J6DWRvoQZBQlElDJg"
export BINANCE_API_SECRET="TySs6onlHOTrGzV8fMdDxLKTWWYnQ4rCHVAmjrcHby17acKflmo7xVTWVsbqtxe7"
export BINANCE_TEST_MODE="false"

export DEEPSEEK_API_KEY="sk-9f6b56f08796435d988cf202e37f6ee3"
export DEEPSEEK_MODEL="deepseek-chat"

export OKX_API_KEY="7abe4037-3d93-40d4-a77b-c77f4a1e9490"
export OKX_API_SECRET="654946A2045F44CC2853D47F96C62F4E"
export OKX_PASSPHRASE="Aa@123456"
export OKX_TEST_MODE="false"

# 钉钉配置
export DINGTALK_ENABLED="true"
export DINGTALK_WEBHOOK_URL="https://oapi.dingtalk.com/robot/send?access_token=2278b723cd363bb6f85592c743b59b166e70b9e02a275bb5cedbc33b53a5cbdc"
export DINGTALK_SECRET="your_dingtalk_secret"

# 确保日志目录存在
if [ ! -d "$WORKDIR/logs" ]; then
  mkdir -p "$WORKDIR/logs"
fi

# 记录开始时间
echo "=============================="
echo "开始执行CryptoAI分析: $(date)"
echo "=============================="

# 检查是否有Python虚拟环境
if [ -d "/ql/data/scripts/cryptoai/venv" ]; then
  echo "使用项目虚拟环境"
  source /ql/data/scripts/cryptoai/venv/bin/activate
fi

# 安装编译工具
echo "检查编译工具..."
if ! command -v gcc &> /dev/null; then
  echo "安装基本编译工具..."
  if command -v apk &> /dev/null; then
    # Alpine Linux
    apk add --no-cache gcc g++ musl-dev python3-dev
  elif command -v apt-get &> /dev/null; then
    # Debian/Ubuntu
    apt-get update && apt-get install -y build-essential python3-dev
  elif command -v yum &> /dev/null; then
    # CentOS/RHEL
    yum install -y gcc gcc-c++ python3-devel
  fi
fi

# 检查依赖是否安装
if ! pip list | grep -q "pandas"; then
  echo "安装依赖..."
  # 使用国内镜像源并优先使用预编译的wheel
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary python-binance==1.0.16
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary pandas==2.0.3
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary numpy==1.24.3
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary matplotlib==3.7.1
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary scikit-learn==1.2.2
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary sqlalchemy==2.0.19
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary pymysql==1.1.0
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary cryptography==41.0.3
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary requests==2.31.0
  pip install -i https://mirrors.aliyun.com/pypi/simple/ --prefer-binary pyyaml==6.0.1
fi

# 运行加密货币分析
python run.py --run-once --agent crypto

# 如果需要运行黄金分析，取消下面的注释
# python run.py --run-once --agent gold

# 记录结束时间
echo "=============================="
echo "CryptoAI分析完成: $(date)"
echo "==============================" 