Última atividade 1 week ago

Revisão 6cb2a03c22a828f580b10ac00b63c382397c51eb

metube3.sh Bruto
1#!/usr/bin/env bash
2
3# MeTube installation script with offline Deno installation
4# Place this script and deno-x86_64-unknown-linux-gnu.zip in the same directory
5
6set -e
7
8APP="MeTube"
9APP_DIR="/opt/metube"
10DENO_INSTALL_DIR="/usr/local"
11DENO_ZIP="deno-x86_64-unknown-linux-gnu.zip"
12GITHUB_REPO="alexta69/metube"
13SERVICE_USER="root"
14
15# Colors for output
16RED='\033[0;31m'
17GREEN='\033[0;32m'
18YELLOW='\033[1;33m'
19BLUE='\033[0;34m'
20NC='\033[0m'
21
22info() { echo -e "${GREEN}[INFO]${NC} $1"; }
23warn() { echo -e "${YELLOW}[WARN]${NC} $1"; }
24error() { echo -e "${RED}[ERROR]${NC} $1"; exit 1; }
25step() { echo -e "${BLUE}[STEP]${NC} $1"; }
26
27# Check if running as root
28if [[ $EUID -ne 0 ]]; then
29 error "This script must be run as root"
30fi
31
32# Check for local Deno archive
33SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
34DENO_ARCHIVE_PATH="${SCRIPT_DIR}/${DENO_ZIP}"
35
36if [[ ! -f "$DENO_ARCHIVE_PATH" ]]; then
37 error "Deno archive not found at $DENO_ARCHIVE_PATH"
38fi
39
40info "Found Deno archive: $DENO_ARCHIVE_PATH"
41
42# Install dependencies
43step "Installing system dependencies"
44apt-get update
45apt-get install -y curl wget unzip git build-essential python3 python3-pip python3-venv
46
47# Install FFmpeg
48step "Installing FFmpeg"
49apt-get install -y ffmpeg
50if command -v ffmpeg >/dev/null 2>&1; then
51 info "FFmpeg installed successfully: $(ffmpeg -version | head -n1)"
52else
53 error "FFmpeg installation failed"
54fi
55
56step "Installing NODEJS"
57curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
58apt install -y nodejs
59npm install -g pm2
60
61# Install Deno from local archive
62step "Installing Deno from local archive"
63if [[ -d "${DENO_INSTALL_DIR}/deno" ]]; then
64 rm -rf "${DENO_INSTALL_DIR}/deno"
65fi
66
67unzip -o "$DENO_ARCHIVE_PATH" -d /tmp/deno_extract
68chmod +x /tmp/deno_extract/deno
69mv /tmp/deno_extract/deno "${DENO_INSTALL_DIR}/bin/deno"
70rm -rf /tmp/deno_extract
71
72if command -v deno >/dev/null 2>&1; then
73 info "Deno installed successfully: $(deno --version)"
74else
75 error "Deno installation failed"
76fi
77
78# Install pnpm
79step "Installing pnpm"
80curl -fsSL https://get.pnpm.io/install.sh | sh -
81
82# Download and setup MeTube
83step "Downloading MeTube from GitHub"
84LATEST_URL=$(curl -s "https://api.github.com/repos/${GITHUB_REPO}/releases/latest" | grep "tarball_url" | cut -d '"' -f 4)
85
86if [[ -z "$LATEST_URL" ]]; then
87 error "Failed to get latest release URL"
88fi
89
90if [[ -d "$APP_DIR" ]]; then
91 warn "Existing installation found, backing up..."
92 mv "$APP_DIR" "${APP_DIR}_bak_$(date +%Y%m%d_%H%M%S)"
93fi
94
95mkdir -p "$APP_DIR"
96cd "$APP_DIR"
97
98curl -L "$LATEST_URL" -o metube.tar.gz
99tar -xzf metube.tar.gz --strip-components=1
100rm metube.tar.gz
101
102# Build frontend
103step "Building frontend"
104if [[ -d "$APP_DIR/ui" ]]; then
105 cd "$APP_DIR/ui"
106 /root/.local/share/pnpm/pnpm install --frozen-lockfile
107 /root/.local/share/pnpm/pnpm run build
108else
109 error "UI directory not found"
110fi
111
112# Setup Python backend
113step "Setting up Python backend"
114cd "$APP_DIR"
115python3 -m venv .venv
116source .venv/bin/activate
117pip install --upgrade pip
118
119# Install required Python packages
120step "Installing Python packages"
121pip install yt-dlp aiohttp python-dotenv python-socketio watchfiles
122
123# Check for requirements.txt and install if exists
124if [[ -f "$APP_DIR/requirements.txt" ]]; then
125 pip install -r requirements.txt
126fi
127
128# Configure yt-dlp to use Deno for JS challenges
129step "Configuring yt-dlp for JavaScript challenges"
130mkdir -p /root/.config/yt-dlp
131
132# Create yt-dlp config to enable remote components
133cat > /root/.config/yt-dlp/config <<EOF
134# Enable Deno for JS challenge solving
135--remote-components ejs:github
136EOF
137
138info "yt-dlp configured to use Deno for JavaScript challenges"
139
140# Create .env file with yt-dlp options in JSON format
141step "Creating configuration"
142if [[ ! -f "$APP_DIR/.env" ]]; then
143 cat > "$APP_DIR/.env" <<'EOF'
144# MeTube Configuration
145DOWNLOAD_DIR=/downloads
146STATE_DIR=/opt/metube/state
147URL_PREFIX=
148HTTP_USERNAME=
149HTTP_PASSWORD=
150YTDL_OPTIONS={"extractor_args":{"youtube":{"skip":[["hls","dash","live"]],"player_client":["android","web"]}},"remote_components":"ejs:github"}
151EOF
152else
153 # Check if YTDL_OPTIONS already exists in .env
154 if ! grep -q "YTDL_OPTIONS" "$APP_DIR/.env"; then
155 echo "" >> "$APP_DIR/.env"
156 echo "# yt-dlp options for JS challenge solving" >> "$APP_DIR/.env"
157 echo 'YTDL_OPTIONS={"extractor_args":{"youtube":{"skip":[["hls","dash","live"]],"player_client":["android","web"]}},"remote_components":"ejs:github"}' >> "$APP_DIR/.env"
158 fi
159fi
160
161# Create download and state directories
162mkdir -p /downloads
163mkdir -p "$APP_DIR/state"
164chown -R root:root /downloads
165chown -R root:root "$APP_DIR"
166
167# Fix potential missing app/main.py structure
168step "Checking MeTube application structure"
169if [[ ! -f "$APP_DIR/app/main.py" ]]; then
170 warn "app/main.py not found, checking alternative locations..."
171 if [[ -f "$APP_DIR/main.py" ]]; then
172 warn "Found main.py in root, creating app directory structure"
173 mkdir -p "$APP_DIR/app"
174 mv "$APP_DIR/main.py" "$APP_DIR/app/"
175 else
176 error "Cannot find main.py. MeTube structure may be different than expected"
177 fi
178fi
179
180# Create systemd service
181step "Creating systemd service"
182cat > /etc/systemd/system/metube.service <<EOF
183[Unit]
184Description=MeTube - YouTube Downloader
185After=network.target
186
187[Service]
188Type=simple
189WorkingDirectory=${APP_DIR}
190EnvironmentFile=${APP_DIR}/.env
191Environment="PATH=/usr/local/bin:/usr/bin:/bin:${APP_DIR}/.venv/bin"
192ExecStartPre=/bin/bash -c 'echo "Starting MeTube with YTDL_OPTIONS=\${YTDL_OPTIONS}"'
193ExecStart=${APP_DIR}/.venv/bin/python3 ${APP_DIR}/app/main.py
194Restart=always
195User=root
196StandardOutput=journal
197StandardError=journal
198
199[Install]
200WantedBy=multi-user.target
201EOF
202
203# Test the Python application manually before starting service
204step "Testing Python application manually"
205cd "$APP_DIR"
206source .venv/bin/activate
207timeout 5 python3 -c "
208import os
209import json
210os.environ['DOWNLOAD_DIR'] = '/downloads'
211os.environ['STATE_DIR'] = '/opt/metube/state'
212ytdl_options_str = '{\"extractor_args\":{\"youtube\":{\"skip\":[[\"hls\",\"dash\",\"live\"]],\"player_client\":[\"android\",\"web\"]}},\"remote_components\":\"ejs:github\"}'
213try:
214 ytdl_options = json.loads(ytdl_options_str)
215 print(f'YTDL_OPTIONS parsed successfully: {ytdl_options}')
216except Exception as e:
217 print(f'Error parsing YTDL_OPTIONS: {e}')
218 exit(1)
219" || warn "Manual test failed, but continuing..."
220
221# Reload systemd and start service
222step "Starting MeTube service"
223systemctl daemon-reload
224systemctl enable metube
225systemctl start metube
226
227# Check if service is running
228sleep 5
229if systemctl is-active --quiet metube; then
230 info "MeTube service started successfully"
231else
232 warn "MeTube service failed to start. Checking logs..."
233 journalctl -u metube --no-pager -n 30
234 echo ""
235 echo "Trying to run MeTube directly to see error:"
236 cd "$APP_DIR"
237 source .venv/bin/activate
238 timeout 10 python3 app/main.py || true
239 error "MeTube service failed to start. Check logs with: journalctl -u metube"
240fi
241
242# Get IP address
243IP_ADDRESS=$(hostname -I | awk '{print $1}')
244
245echo ""
246echo -e "${GREEN}========================================${NC}"
247echo -e "${GREEN}MeTube installation completed!${NC}"
248echo -e "${GREEN}========================================${NC}"
249echo -e "Access MeTube at: ${YELLOW}http://${IP_ADDRESS}:8081${NC}"
250echo ""
251echo -e "Commands:"
252echo -e " Start: ${BLUE}systemctl start metube${NC}"
253echo -e " Stop: ${BLUE}systemctl stop metube${NC}"
254echo -e " Status: ${BLUE}systemctl status metube${NC}"
255echo -e " Logs: ${BLUE}journalctl -u metube -f${NC}"
256echo ""
257echo -e "Download directory: ${YELLOW}/downloads${NC}"
258echo -e "Config file: ${YELLOW}${APP_DIR}/.env${NC}"
259echo -e "yt-dlp config: ${YELLOW}/root/.config/yt-dlp/config${NC}"
260echo ""
261echo -e "${GREEN}Note:${NC} yt-dlp has been configured to use Deno for JavaScript challenge solving"
262echo -e "This helps bypass YouTube's anti-bot protection for downloading videos"