install.sh 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. #!/bin/bash
  2. # YouTube Predownloader Installation Script
  3. # This script sets up the YouTube predownloader as a systemd service
  4. set -e # Exit on any error
  5. # Colors for output
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[1;33m'
  9. BLUE='\033[0;34m'
  10. NC='\033[0m' # No Color
  11. # Configuration
  12. SERVICE_NAME="youtube-predownloader"
  13. INSTALL_DIR="/opt/youtube-predownloader"
  14. SERVICE_USER="youtube-dl"
  15. VENV_DIR="$INSTALL_DIR/venv"
  16. LOG_DIR="/var/log/youtube-predownloader"
  17. echo -e "${BLUE}🎬 YouTube Predownloader Installation Script${NC}"
  18. echo "=============================================="
  19. # Check if running as root
  20. if [[ $EUID -ne 0 ]]; then
  21. echo -e "${RED}❌ This script must be run as root (use sudo)${NC}"
  22. exit 1
  23. fi
  24. echo -e "${YELLOW}📋 Installation Configuration:${NC}"
  25. echo " Service Name: $SERVICE_NAME"
  26. echo " Install Directory: $INSTALL_DIR"
  27. echo " Service User: $SERVICE_USER"
  28. echo " Virtual Environment: $VENV_DIR"
  29. echo " Log Directory: $LOG_DIR"
  30. echo
  31. # Prompt for confirmation
  32. read -p "Continue with installation? (y/N): " -n 1 -r
  33. echo
  34. if [[ ! $REPLY =~ ^[Yy]$ ]]; then
  35. echo -e "${YELLOW}⚠️ Installation cancelled${NC}"
  36. exit 0
  37. fi
  38. # Install system dependencies
  39. echo -e "${BLUE}📦 Installing system dependencies...${NC}"
  40. apt update
  41. apt install -y python3 python3-venv python3-pip ffmpeg git curl
  42. # Create service user
  43. echo -e "${BLUE}👤 Creating service user...${NC}"
  44. if ! id "$SERVICE_USER" &>/dev/null; then
  45. useradd --system --shell /bin/false --home-dir $INSTALL_DIR --create-home $SERVICE_USER
  46. echo -e "${GREEN}✅ Created user: $SERVICE_USER${NC}"
  47. else
  48. echo -e "${YELLOW}⚠️ User $SERVICE_USER already exists${NC}"
  49. fi
  50. # Create installation directory
  51. echo -e "${BLUE}📁 Setting up installation directory...${NC}"
  52. mkdir -p $INSTALL_DIR
  53. mkdir -p $LOG_DIR
  54. chown $SERVICE_USER:$SERVICE_USER $INSTALL_DIR
  55. chown $SERVICE_USER:$SERVICE_USER $LOG_DIR
  56. # Copy files to installation directory
  57. echo -e "${BLUE}📋 Copying application files...${NC}"
  58. if [ -f "main.py" ]; then
  59. cp *.py $INSTALL_DIR/
  60. if [ -f "client_secret.json" ]; then
  61. cp client_secret.json $INSTALL_DIR/
  62. fi
  63. if [ -f "service_account.json" ]; then
  64. cp service_account.json $INSTALL_DIR/
  65. fi
  66. if [ -f "*.pickle" ]; then
  67. cp *.pickle $INSTALL_DIR/ 2>/dev/null || true
  68. fi
  69. chown -R $SERVICE_USER:$SERVICE_USER $INSTALL_DIR
  70. echo -e "${GREEN}✅ Application files copied${NC}"
  71. else
  72. echo -e "${RED}❌ main.py not found in current directory${NC}"
  73. exit 1
  74. fi
  75. # Create virtual environment
  76. echo -e "${BLUE}🐍 Creating Python virtual environment...${NC}"
  77. sudo -u $SERVICE_USER python3 -m venv $VENV_DIR
  78. echo -e "${GREEN}✅ Virtual environment created${NC}"
  79. # Install Python dependencies
  80. echo -e "${BLUE}📦 Installing Python dependencies...${NC}"
  81. cat > $INSTALL_DIR/requirements.txt << 'EOF'
  82. google-auth-oauthlib>=1.2.0
  83. google-api-python-client>=2.100.0
  84. google-auth>=2.15.0
  85. yt-dlp>=2023.7.6
  86. paho-mqtt>=2.0.0
  87. psutil>=5.9.0
  88. EOF
  89. sudo -u $SERVICE_USER $VENV_DIR/bin/pip install --upgrade pip
  90. sudo -u $SERVICE_USER $VENV_DIR/bin/pip install -r $INSTALL_DIR/requirements.txt
  91. echo -e "${GREEN}✅ Python dependencies installed${NC}"
  92. # Create configuration file
  93. echo -e "${BLUE}⚙️ Creating configuration file...${NC}"
  94. cat > $INSTALL_DIR/config.py << 'EOF'
  95. # YouTube Predownloader Configuration
  96. # Output directory for downloaded videos
  97. OUTPUT_DIR = "/media/youtube" # Change this to your desired output directory
  98. # Schedule settings
  99. SCHEDULE_START_TIME = "00:00" # Start time (24-hour format)
  100. SCHEDULE_END_TIME = "07:00" # End time (24-hour format)
  101. RUN_ONCE_PER_DAY = True # Only run once per day
  102. # YouTube API settings
  103. DAYS_BACK = 5 # Videos published in the last N days
  104. DAYS_TO_KEEP = 10 # Delete videos older than N days
  105. POPULAR_VIDEOS_MAX_RESULTS = 10
  106. REGION_CODES = ["US", "GB", "LU", "LK"]
  107. # Conversion settings
  108. MAX_CONCURRENT_CONVERSIONS = 2
  109. CONVERSION_QUEUE_SIZE = 50
  110. # Home Assistant MQTT settings (optional)
  111. HOMEASSISTANT_ENABLED = True
  112. MQTT_BROKER = "192.168.1.100" # Your Home Assistant IP
  113. MQTT_PORT = 1883
  114. MQTT_USERNAME = "" # Set if authentication required
  115. MQTT_PASSWORD = "" # Set if authentication required
  116. EOF
  117. chown $SERVICE_USER:$SERVICE_USER $INSTALL_DIR/config.py
  118. echo -e "${GREEN}✅ Configuration file created${NC}"
  119. # Create systemd service file
  120. echo -e "${BLUE}🔧 Creating systemd service...${NC}"
  121. cat > /etc/systemd/system/$SERVICE_NAME.service << EOF
  122. [Unit]
  123. Description=YouTube Predownloader Service
  124. After=network.target network-online.target
  125. Wants=network-online.target
  126. [Service]
  127. Type=simple
  128. User=$SERVICE_USER
  129. Group=$SERVICE_USER
  130. WorkingDirectory=$INSTALL_DIR
  131. Environment=PATH=$VENV_DIR/bin
  132. ExecStart=$VENV_DIR/bin/python main.py
  133. Restart=on-failure
  134. RestartSec=60
  135. StandardOutput=append:$LOG_DIR/youtube-predownloader.log
  136. StandardError=append:$LOG_DIR/youtube-predownloader.log
  137. # Security settings
  138. NoNewPrivileges=true
  139. PrivateTmp=true
  140. ProtectSystem=strict
  141. ProtectHome=true
  142. ReadWritePaths=$INSTALL_DIR $LOG_DIR /media
  143. ProtectKernelTunables=true
  144. ProtectKernelModules=true
  145. ProtectControlGroups=true
  146. [Install]
  147. WantedBy=multi-user.target
  148. EOF
  149. echo -e "${GREEN}✅ Systemd service created${NC}"
  150. # Create log rotation
  151. echo -e "${BLUE}📜 Setting up log rotation...${NC}"
  152. cat > /etc/logrotate.d/$SERVICE_NAME << EOF
  153. $LOG_DIR/*.log {
  154. daily
  155. missingok
  156. rotate 30
  157. compress
  158. delaycompress
  159. notifempty
  160. create 644 $SERVICE_USER $SERVICE_USER
  161. postrotate
  162. systemctl reload $SERVICE_NAME 2>/dev/null || true
  163. endscript
  164. }
  165. EOF
  166. echo -e "${GREEN}✅ Log rotation configured${NC}"
  167. # Create output directory
  168. echo -e "${BLUE}📁 Creating output directory...${NC}"
  169. OUTPUT_DIR_DEFAULT="/media/youtube"
  170. mkdir -p $OUTPUT_DIR_DEFAULT
  171. chown $SERVICE_USER:$SERVICE_USER $OUTPUT_DIR_DEFAULT
  172. echo -e "${GREEN}✅ Output directory created: $OUTPUT_DIR_DEFAULT${NC}"
  173. # Set up systemd service
  174. echo -e "${BLUE}🔧 Enabling systemd service...${NC}"
  175. systemctl daemon-reload
  176. systemctl enable $SERVICE_NAME
  177. echo -e "${GREEN}✅ Service enabled${NC}"
  178. # Create management script
  179. echo -e "${BLUE}🛠️ Creating management script...${NC}"
  180. cat > /usr/local/bin/youtube-predownloader << 'EOF'
  181. #!/bin/bash
  182. # YouTube Predownloader Management Script
  183. SERVICE_NAME="youtube-predownloader"
  184. INSTALL_DIR="/opt/youtube-predownloader"
  185. LOG_DIR="/var/log/youtube-predownloader"
  186. case "$1" in
  187. start)
  188. echo "Starting YouTube Predownloader..."
  189. systemctl start $SERVICE_NAME
  190. ;;
  191. stop)
  192. echo "Stopping YouTube Predownloader..."
  193. systemctl stop $SERVICE_NAME
  194. ;;
  195. restart)
  196. echo "Restarting YouTube Predownloader..."
  197. systemctl restart $SERVICE_NAME
  198. ;;
  199. status)
  200. systemctl status $SERVICE_NAME
  201. ;;
  202. logs)
  203. journalctl -u $SERVICE_NAME -f
  204. ;;
  205. logs-file)
  206. tail -f $LOG_DIR/youtube-predownloader.log
  207. ;;
  208. config)
  209. nano $INSTALL_DIR/config.py
  210. echo "Configuration updated. Restart service to apply changes."
  211. ;;
  212. update)
  213. echo "Updating yt-dlp..."
  214. sudo -u youtube-dl $INSTALL_DIR/venv/bin/pip install --upgrade yt-dlp
  215. echo "Restarting service..."
  216. systemctl restart $SERVICE_NAME
  217. ;;
  218. *)
  219. echo "Usage: $0 {start|stop|restart|status|logs|logs-file|config|update}"
  220. echo ""
  221. echo "Commands:"
  222. echo " start - Start the service"
  223. echo " stop - Stop the service"
  224. echo " restart - Restart the service"
  225. echo " status - Show service status"
  226. echo " logs - Show live system logs"
  227. echo " logs-file - Show live application logs"
  228. echo " config - Edit configuration"
  229. echo " update - Update yt-dlp"
  230. exit 1
  231. ;;
  232. esac
  233. EOF
  234. chmod +x /usr/local/bin/youtube-predownloader
  235. echo -e "${GREEN}✅ Management script created: youtube-predownloader${NC}"
  236. echo
  237. echo -e "${GREEN}🎉 Installation completed successfully!${NC}"
  238. echo
  239. echo -e "${BLUE}📋 Next Steps:${NC}"
  240. echo "1. Edit configuration: youtube-predownloader config"
  241. echo "2. Add your YouTube credentials (client_secret.json or service_account.json)"
  242. echo "3. Start the service: youtube-predownloader start"
  243. echo "4. Check status: youtube-predownloader status"
  244. echo "5. View logs: youtube-predownloader logs"
  245. echo
  246. echo -e "${BLUE}🛠️ Management Commands:${NC}"
  247. echo " youtube-predownloader start - Start service"
  248. echo " youtube-predownloader stop - Stop service"
  249. echo " youtube-predownloader restart - Restart service"
  250. echo " youtube-predownloader status - Check status"
  251. echo " youtube-predownloader logs - View logs"
  252. echo " youtube-predownloader config - Edit settings"
  253. echo " youtube-predownloader update - Update yt-dlp"
  254. echo
  255. echo -e "${YELLOW}⚠️ Important:${NC}"
  256. echo "- Edit $INSTALL_DIR/config.py to customize settings"
  257. echo "- Place your YouTube API credentials in $INSTALL_DIR/"
  258. echo "- Service will run continuously and handle scheduling internally"
  259. echo "- Logs are available at $LOG_DIR/youtube-predownloader.log"
  260. echo