|
@@ -0,0 +1,295 @@
|
|
|
|
|
+#!/bin/bash
|
|
|
|
|
+# YouTube Predownloader Installation Script
|
|
|
|
|
+# This script sets up the YouTube predownloader as a systemd service
|
|
|
|
|
+
|
|
|
|
|
+set -e # Exit on any error
|
|
|
|
|
+
|
|
|
|
|
+# Colors for output
|
|
|
|
|
+RED='\033[0;31m'
|
|
|
|
|
+GREEN='\033[0;32m'
|
|
|
|
|
+YELLOW='\033[1;33m'
|
|
|
|
|
+BLUE='\033[0;34m'
|
|
|
|
|
+NC='\033[0m' # No Color
|
|
|
|
|
+
|
|
|
|
|
+# Configuration
|
|
|
|
|
+SERVICE_NAME="youtube-predownloader"
|
|
|
|
|
+INSTALL_DIR="/opt/youtube-predownloader"
|
|
|
|
|
+SERVICE_USER="youtube-dl"
|
|
|
|
|
+VENV_DIR="$INSTALL_DIR/venv"
|
|
|
|
|
+LOG_DIR="/var/log/youtube-predownloader"
|
|
|
|
|
+
|
|
|
|
|
+echo -e "${BLUE}🎬 YouTube Predownloader Installation Script${NC}"
|
|
|
|
|
+echo "=============================================="
|
|
|
|
|
+
|
|
|
|
|
+# Check if running as root
|
|
|
|
|
+if [[ $EUID -ne 0 ]]; then
|
|
|
|
|
+ echo -e "${RED}❌ This script must be run as root (use sudo)${NC}"
|
|
|
|
|
+ exit 1
|
|
|
|
|
+fi
|
|
|
|
|
+
|
|
|
|
|
+echo -e "${YELLOW}📋 Installation Configuration:${NC}"
|
|
|
|
|
+echo " Service Name: $SERVICE_NAME"
|
|
|
|
|
+echo " Install Directory: $INSTALL_DIR"
|
|
|
|
|
+echo " Service User: $SERVICE_USER"
|
|
|
|
|
+echo " Virtual Environment: $VENV_DIR"
|
|
|
|
|
+echo " Log Directory: $LOG_DIR"
|
|
|
|
|
+echo
|
|
|
|
|
+
|
|
|
|
|
+# Prompt for confirmation
|
|
|
|
|
+read -p "Continue with installation? (y/N): " -n 1 -r
|
|
|
|
|
+echo
|
|
|
|
|
+if [[ ! $REPLY =~ ^[Yy]$ ]]; then
|
|
|
|
|
+ echo -e "${YELLOW}⚠️ Installation cancelled${NC}"
|
|
|
|
|
+ exit 0
|
|
|
|
|
+fi
|
|
|
|
|
+
|
|
|
|
|
+# Install system dependencies
|
|
|
|
|
+echo -e "${BLUE}📦 Installing system dependencies...${NC}"
|
|
|
|
|
+apt update
|
|
|
|
|
+apt install -y python3 python3-venv python3-pip ffmpeg git curl
|
|
|
|
|
+
|
|
|
|
|
+# Create service user
|
|
|
|
|
+echo -e "${BLUE}👤 Creating service user...${NC}"
|
|
|
|
|
+if ! id "$SERVICE_USER" &>/dev/null; then
|
|
|
|
|
+ useradd --system --shell /bin/false --home-dir $INSTALL_DIR --create-home $SERVICE_USER
|
|
|
|
|
+ echo -e "${GREEN}✅ Created user: $SERVICE_USER${NC}"
|
|
|
|
|
+else
|
|
|
|
|
+ echo -e "${YELLOW}⚠️ User $SERVICE_USER already exists${NC}"
|
|
|
|
|
+fi
|
|
|
|
|
+
|
|
|
|
|
+# Create installation directory
|
|
|
|
|
+echo -e "${BLUE}📁 Setting up installation directory...${NC}"
|
|
|
|
|
+mkdir -p $INSTALL_DIR
|
|
|
|
|
+mkdir -p $LOG_DIR
|
|
|
|
|
+chown $SERVICE_USER:$SERVICE_USER $INSTALL_DIR
|
|
|
|
|
+chown $SERVICE_USER:$SERVICE_USER $LOG_DIR
|
|
|
|
|
+
|
|
|
|
|
+# Copy files to installation directory
|
|
|
|
|
+echo -e "${BLUE}📋 Copying application files...${NC}"
|
|
|
|
|
+if [ -f "main.py" ]; then
|
|
|
|
|
+ cp *.py $INSTALL_DIR/
|
|
|
|
|
+ if [ -f "client_secret.json" ]; then
|
|
|
|
|
+ cp client_secret.json $INSTALL_DIR/
|
|
|
|
|
+ fi
|
|
|
|
|
+ if [ -f "service_account.json" ]; then
|
|
|
|
|
+ cp service_account.json $INSTALL_DIR/
|
|
|
|
|
+ fi
|
|
|
|
|
+ if [ -f "*.pickle" ]; then
|
|
|
|
|
+ cp *.pickle $INSTALL_DIR/ 2>/dev/null || true
|
|
|
|
|
+ fi
|
|
|
|
|
+ chown -R $SERVICE_USER:$SERVICE_USER $INSTALL_DIR
|
|
|
|
|
+ echo -e "${GREEN}✅ Application files copied${NC}"
|
|
|
|
|
+else
|
|
|
|
|
+ echo -e "${RED}❌ main.py not found in current directory${NC}"
|
|
|
|
|
+ exit 1
|
|
|
|
|
+fi
|
|
|
|
|
+
|
|
|
|
|
+# Create virtual environment
|
|
|
|
|
+echo -e "${BLUE}🐍 Creating Python virtual environment...${NC}"
|
|
|
|
|
+sudo -u $SERVICE_USER python3 -m venv $VENV_DIR
|
|
|
|
|
+echo -e "${GREEN}✅ Virtual environment created${NC}"
|
|
|
|
|
+
|
|
|
|
|
+# Install Python dependencies
|
|
|
|
|
+echo -e "${BLUE}📦 Installing Python dependencies...${NC}"
|
|
|
|
|
+cat > $INSTALL_DIR/requirements.txt << 'EOF'
|
|
|
|
|
+google-auth-oauthlib>=1.2.0
|
|
|
|
|
+google-api-python-client>=2.100.0
|
|
|
|
|
+google-auth>=2.15.0
|
|
|
|
|
+yt-dlp>=2023.7.6
|
|
|
|
|
+paho-mqtt>=2.0.0
|
|
|
|
|
+psutil>=5.9.0
|
|
|
|
|
+EOF
|
|
|
|
|
+
|
|
|
|
|
+sudo -u $SERVICE_USER $VENV_DIR/bin/pip install --upgrade pip
|
|
|
|
|
+sudo -u $SERVICE_USER $VENV_DIR/bin/pip install -r $INSTALL_DIR/requirements.txt
|
|
|
|
|
+echo -e "${GREEN}✅ Python dependencies installed${NC}"
|
|
|
|
|
+
|
|
|
|
|
+# Create configuration file
|
|
|
|
|
+echo -e "${BLUE}⚙️ Creating configuration file...${NC}"
|
|
|
|
|
+cat > $INSTALL_DIR/config.py << 'EOF'
|
|
|
|
|
+# YouTube Predownloader Configuration
|
|
|
|
|
+
|
|
|
|
|
+# Output directory for downloaded videos
|
|
|
|
|
+OUTPUT_DIR = "/media/youtube" # Change this to your desired output directory
|
|
|
|
|
+
|
|
|
|
|
+# Schedule settings
|
|
|
|
|
+SCHEDULE_START_TIME = "00:00" # Start time (24-hour format)
|
|
|
|
|
+SCHEDULE_END_TIME = "07:00" # End time (24-hour format)
|
|
|
|
|
+RUN_ONCE_PER_DAY = True # Only run once per day
|
|
|
|
|
+
|
|
|
|
|
+# YouTube API settings
|
|
|
|
|
+DAYS_BACK = 5 # Videos published in the last N days
|
|
|
|
|
+DAYS_TO_KEEP = 10 # Delete videos older than N days
|
|
|
|
|
+POPULAR_VIDEOS_MAX_RESULTS = 10
|
|
|
|
|
+REGION_CODES = ["US", "GB", "LU", "LK"]
|
|
|
|
|
+
|
|
|
|
|
+# Conversion settings
|
|
|
|
|
+MAX_CONCURRENT_CONVERSIONS = 2
|
|
|
|
|
+CONVERSION_QUEUE_SIZE = 50
|
|
|
|
|
+
|
|
|
|
|
+# Home Assistant MQTT settings (optional)
|
|
|
|
|
+HOMEASSISTANT_ENABLED = True
|
|
|
|
|
+MQTT_BROKER = "192.168.1.100" # Your Home Assistant IP
|
|
|
|
|
+MQTT_PORT = 1883
|
|
|
|
|
+MQTT_USERNAME = "" # Set if authentication required
|
|
|
|
|
+MQTT_PASSWORD = "" # Set if authentication required
|
|
|
|
|
+EOF
|
|
|
|
|
+
|
|
|
|
|
+chown $SERVICE_USER:$SERVICE_USER $INSTALL_DIR/config.py
|
|
|
|
|
+echo -e "${GREEN}✅ Configuration file created${NC}"
|
|
|
|
|
+
|
|
|
|
|
+# Create systemd service file
|
|
|
|
|
+echo -e "${BLUE}🔧 Creating systemd service...${NC}"
|
|
|
|
|
+cat > /etc/systemd/system/$SERVICE_NAME.service << EOF
|
|
|
|
|
+[Unit]
|
|
|
|
|
+Description=YouTube Predownloader Service
|
|
|
|
|
+After=network.target network-online.target
|
|
|
|
|
+Wants=network-online.target
|
|
|
|
|
+
|
|
|
|
|
+[Service]
|
|
|
|
|
+Type=simple
|
|
|
|
|
+User=$SERVICE_USER
|
|
|
|
|
+Group=$SERVICE_USER
|
|
|
|
|
+WorkingDirectory=$INSTALL_DIR
|
|
|
|
|
+Environment=PATH=$VENV_DIR/bin
|
|
|
|
|
+ExecStart=$VENV_DIR/bin/python main.py
|
|
|
|
|
+Restart=on-failure
|
|
|
|
|
+RestartSec=60
|
|
|
|
|
+StandardOutput=append:$LOG_DIR/youtube-predownloader.log
|
|
|
|
|
+StandardError=append:$LOG_DIR/youtube-predownloader.log
|
|
|
|
|
+
|
|
|
|
|
+# Security settings
|
|
|
|
|
+NoNewPrivileges=true
|
|
|
|
|
+PrivateTmp=true
|
|
|
|
|
+ProtectSystem=strict
|
|
|
|
|
+ProtectHome=true
|
|
|
|
|
+ReadWritePaths=$INSTALL_DIR $LOG_DIR /media
|
|
|
|
|
+ProtectKernelTunables=true
|
|
|
|
|
+ProtectKernelModules=true
|
|
|
|
|
+ProtectControlGroups=true
|
|
|
|
|
+
|
|
|
|
|
+[Install]
|
|
|
|
|
+WantedBy=multi-user.target
|
|
|
|
|
+EOF
|
|
|
|
|
+
|
|
|
|
|
+echo -e "${GREEN}✅ Systemd service created${NC}"
|
|
|
|
|
+
|
|
|
|
|
+# Create log rotation
|
|
|
|
|
+echo -e "${BLUE}📜 Setting up log rotation...${NC}"
|
|
|
|
|
+cat > /etc/logrotate.d/$SERVICE_NAME << EOF
|
|
|
|
|
+$LOG_DIR/*.log {
|
|
|
|
|
+ daily
|
|
|
|
|
+ missingok
|
|
|
|
|
+ rotate 30
|
|
|
|
|
+ compress
|
|
|
|
|
+ delaycompress
|
|
|
|
|
+ notifempty
|
|
|
|
|
+ create 644 $SERVICE_USER $SERVICE_USER
|
|
|
|
|
+ postrotate
|
|
|
|
|
+ systemctl reload $SERVICE_NAME 2>/dev/null || true
|
|
|
|
|
+ endscript
|
|
|
|
|
+}
|
|
|
|
|
+EOF
|
|
|
|
|
+
|
|
|
|
|
+echo -e "${GREEN}✅ Log rotation configured${NC}"
|
|
|
|
|
+
|
|
|
|
|
+# Create output directory
|
|
|
|
|
+echo -e "${BLUE}📁 Creating output directory...${NC}"
|
|
|
|
|
+OUTPUT_DIR_DEFAULT="/media/youtube"
|
|
|
|
|
+mkdir -p $OUTPUT_DIR_DEFAULT
|
|
|
|
|
+chown $SERVICE_USER:$SERVICE_USER $OUTPUT_DIR_DEFAULT
|
|
|
|
|
+echo -e "${GREEN}✅ Output directory created: $OUTPUT_DIR_DEFAULT${NC}"
|
|
|
|
|
+
|
|
|
|
|
+# Set up systemd service
|
|
|
|
|
+echo -e "${BLUE}🔧 Enabling systemd service...${NC}"
|
|
|
|
|
+systemctl daemon-reload
|
|
|
|
|
+systemctl enable $SERVICE_NAME
|
|
|
|
|
+echo -e "${GREEN}✅ Service enabled${NC}"
|
|
|
|
|
+
|
|
|
|
|
+# Create management script
|
|
|
|
|
+echo -e "${BLUE}🛠️ Creating management script...${NC}"
|
|
|
|
|
+cat > /usr/local/bin/youtube-predownloader << 'EOF'
|
|
|
|
|
+#!/bin/bash
|
|
|
|
|
+# YouTube Predownloader Management Script
|
|
|
|
|
+
|
|
|
|
|
+SERVICE_NAME="youtube-predownloader"
|
|
|
|
|
+INSTALL_DIR="/opt/youtube-predownloader"
|
|
|
|
|
+LOG_DIR="/var/log/youtube-predownloader"
|
|
|
|
|
+
|
|
|
|
|
+case "$1" in
|
|
|
|
|
+ start)
|
|
|
|
|
+ echo "Starting YouTube Predownloader..."
|
|
|
|
|
+ systemctl start $SERVICE_NAME
|
|
|
|
|
+ ;;
|
|
|
|
|
+ stop)
|
|
|
|
|
+ echo "Stopping YouTube Predownloader..."
|
|
|
|
|
+ systemctl stop $SERVICE_NAME
|
|
|
|
|
+ ;;
|
|
|
|
|
+ restart)
|
|
|
|
|
+ echo "Restarting YouTube Predownloader..."
|
|
|
|
|
+ systemctl restart $SERVICE_NAME
|
|
|
|
|
+ ;;
|
|
|
|
|
+ status)
|
|
|
|
|
+ systemctl status $SERVICE_NAME
|
|
|
|
|
+ ;;
|
|
|
|
|
+ logs)
|
|
|
|
|
+ journalctl -u $SERVICE_NAME -f
|
|
|
|
|
+ ;;
|
|
|
|
|
+ logs-file)
|
|
|
|
|
+ tail -f $LOG_DIR/youtube-predownloader.log
|
|
|
|
|
+ ;;
|
|
|
|
|
+ config)
|
|
|
|
|
+ nano $INSTALL_DIR/config.py
|
|
|
|
|
+ echo "Configuration updated. Restart service to apply changes."
|
|
|
|
|
+ ;;
|
|
|
|
|
+ update)
|
|
|
|
|
+ echo "Updating yt-dlp..."
|
|
|
|
|
+ sudo -u youtube-dl $INSTALL_DIR/venv/bin/pip install --upgrade yt-dlp
|
|
|
|
|
+ echo "Restarting service..."
|
|
|
|
|
+ systemctl restart $SERVICE_NAME
|
|
|
|
|
+ ;;
|
|
|
|
|
+ *)
|
|
|
|
|
+ echo "Usage: $0 {start|stop|restart|status|logs|logs-file|config|update}"
|
|
|
|
|
+ echo ""
|
|
|
|
|
+ echo "Commands:"
|
|
|
|
|
+ echo " start - Start the service"
|
|
|
|
|
+ echo " stop - Stop the service"
|
|
|
|
|
+ echo " restart - Restart the service"
|
|
|
|
|
+ echo " status - Show service status"
|
|
|
|
|
+ echo " logs - Show live system logs"
|
|
|
|
|
+ echo " logs-file - Show live application logs"
|
|
|
|
|
+ echo " config - Edit configuration"
|
|
|
|
|
+ echo " update - Update yt-dlp"
|
|
|
|
|
+ exit 1
|
|
|
|
|
+ ;;
|
|
|
|
|
+esac
|
|
|
|
|
+EOF
|
|
|
|
|
+
|
|
|
|
|
+chmod +x /usr/local/bin/youtube-predownloader
|
|
|
|
|
+echo -e "${GREEN}✅ Management script created: youtube-predownloader${NC}"
|
|
|
|
|
+
|
|
|
|
|
+echo
|
|
|
|
|
+echo -e "${GREEN}🎉 Installation completed successfully!${NC}"
|
|
|
|
|
+echo
|
|
|
|
|
+echo -e "${BLUE}📋 Next Steps:${NC}"
|
|
|
|
|
+echo "1. Edit configuration: youtube-predownloader config"
|
|
|
|
|
+echo "2. Add your YouTube credentials (client_secret.json or service_account.json)"
|
|
|
|
|
+echo "3. Start the service: youtube-predownloader start"
|
|
|
|
|
+echo "4. Check status: youtube-predownloader status"
|
|
|
|
|
+echo "5. View logs: youtube-predownloader logs"
|
|
|
|
|
+echo
|
|
|
|
|
+echo -e "${BLUE}🛠️ Management Commands:${NC}"
|
|
|
|
|
+echo " youtube-predownloader start - Start service"
|
|
|
|
|
+echo " youtube-predownloader stop - Stop service"
|
|
|
|
|
+echo " youtube-predownloader restart - Restart service"
|
|
|
|
|
+echo " youtube-predownloader status - Check status"
|
|
|
|
|
+echo " youtube-predownloader logs - View logs"
|
|
|
|
|
+echo " youtube-predownloader config - Edit settings"
|
|
|
|
|
+echo " youtube-predownloader update - Update yt-dlp"
|
|
|
|
|
+echo
|
|
|
|
|
+echo -e "${YELLOW}⚠️ Important:${NC}"
|
|
|
|
|
+echo "- Edit $INSTALL_DIR/config.py to customize settings"
|
|
|
|
|
+echo "- Place your YouTube API credentials in $INSTALL_DIR/"
|
|
|
|
|
+echo "- Service will run continuously and handle scheduling internally"
|
|
|
|
|
+echo "- Logs are available at $LOG_DIR/youtube-predownloader.log"
|
|
|
|
|
+echo
|