Files
skipredictor/start.sh
T

140 lines
4.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# Ski Predictor - Setup and start script
# Usage: ./start.sh [--bot] [--cron] [--collect-now]
#
# --bot Start the Telegram bot (requires TELEGRAM_BOT_TOKEN)
# --cron Install the daily cron job (22:00)
# --collect-now Run the data collection pipeline immediately
#
# First run: ./start.sh --cron --collect-now
# With bot: ./start.sh --cron --collect-now --bot
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VENV="$SCRIPT_DIR/.venv"
PYTHON="$VENV/bin/python"
DATA_DIR="$SCRIPT_DIR/data"
ENV_FILE="$SCRIPT_DIR/.env"
# ---------------------------------------------------------------
# Colors
# ---------------------------------------------------------------
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
info() { echo -e "${GREEN}[INFO]${NC} $1"; }
warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
error() { echo -e "${RED}[ERROR]${NC} $1"; }
# ---------------------------------------------------------------
# 1. Create virtualenv and install dependencies
# ---------------------------------------------------------------
if [ ! -d "$VENV" ]; then
info "Creating virtualenv..."
python3 -m venv "$VENV"
fi
info "Installing dependencies..."
"$VENV/bin/pip" install -q -r "$SCRIPT_DIR/requirements.txt"
"$VENV/bin/pip" install -q pytest
# ---------------------------------------------------------------
# 2. Create data directories
# ---------------------------------------------------------------
mkdir -p "$DATA_DIR/raw" "$DATA_DIR/processed" "$SCRIPT_DIR/ml/models"
# ---------------------------------------------------------------
# 3. Parse arguments
# ---------------------------------------------------------------
DO_BOT=false
DO_CRON=false
DO_COLLECT=false
for arg in "$@"; do
case "$arg" in
--bot) DO_BOT=true ;;
--cron) DO_CRON=true ;;
--collect-now) DO_COLLECT=true ;;
*) error "Unknown argument: $arg"; exit 1 ;;
esac
done
# ---------------------------------------------------------------
# 3.5 Load environment from .env when present
# ---------------------------------------------------------------
if [ -f "$ENV_FILE" ]; then
info "Loading environment from $ENV_FILE"
set -a
# shellcheck disable=SC1090
. "$ENV_FILE"
set +a
fi
# ---------------------------------------------------------------
# 4. Install cron job
# ---------------------------------------------------------------
if [ "$DO_CRON" = true ]; then
CRON_CMD="0 22 * * * \"$PYTHON\" \"$SCRIPT_DIR/scripts/collect_daily_data.py\" >> \"$DATA_DIR/pipeline.log\" 2>&1"
if crontab -l 2>/dev/null | grep -qF "collect_daily_data.py"; then
warn "Cron job already exists, skipping."
else
(crontab -l 2>/dev/null; echo "$CRON_CMD") | crontab -
info "Cron job installed (daily at 22:00)."
fi
fi
# ---------------------------------------------------------------
# 5. Run initial data collection
# ---------------------------------------------------------------
if [ "$DO_COLLECT" = true ]; then
info "Running initial data collection..."
"$PYTHON" "$SCRIPT_DIR/scripts/collect_daily_data.py" 2>&1 | tee -a "$DATA_DIR/pipeline.log"
info "Data collection complete."
fi
# ---------------------------------------------------------------
# 6. Start Telegram bot
# ---------------------------------------------------------------
if [ "$DO_BOT" = true ]; then
if [ -z "${TELEGRAM_BOT_TOKEN:-}" ]; then
error "TELEGRAM_BOT_TOKEN not set. Export it first:"
echo " export TELEGRAM_BOT_TOKEN='your_token_here'"
exit 1
fi
# Kill existing bot process if running
if pgrep -f "bot/telegram_bot.py|bot\\.telegram_bot" > /dev/null 2>&1; then
warn "Killing existing bot process..."
pkill -f "bot/telegram_bot.py|bot\\.telegram_bot" || true
sleep 1
fi
info "Starting Telegram bot..."
: > "$DATA_DIR/bot.log"
nohup env PYTHONPATH="$SCRIPT_DIR" "$PYTHON" -m bot.telegram_bot \
</dev/null >> "$DATA_DIR/bot.log" 2>&1 &
BOT_PID=$!
info "Bot started (PID: $BOT_PID). Logs: $DATA_DIR/bot.log"
fi
# ---------------------------------------------------------------
# Summary
# ---------------------------------------------------------------
echo ""
info "=== Ski Predictor ==="
echo " Project: $SCRIPT_DIR"
echo " Python: $PYTHON"
echo " Data: $DATA_DIR"
if [ "$DO_CRON" = true ]; then
echo " Cron: Daily at 22:00"
fi
if [ "$DO_BOT" = true ]; then
echo " Bot PID: $BOT_PID"
fi
echo ""
info "Done."