#!/bin/sh set -e # Function to grab EPG data for a site grab_epg() { site=$1 echo "Grabbing EPG data for site: $site" # Build command with optional parameters cmd="npm run grab -- --site=$site --output=output/${site}.xml --maxConnections=${MAX_CONNECTIONS}" # Add optional parameters if they are set if [ -n "$DAYS" ]; then cmd="$cmd --days=${DAYS}" fi if [ -n "$DELAY" ]; then cmd="$cmd --delay=${DELAY}" fi if [ -n "$LANG" ]; then cmd="$cmd --lang=${LANG}" fi if [ "$GZIP" = "true" ]; then cmd="$cmd --gzip" fi # Execute the command eval $cmd } # Function to grab EPG data for all sites concurrently grab_all_sites() { # Split the SITES environment variable by comma - POSIX sh compatible echo "$SITES" | tr ',' '\n' >/tmp/sites.txt # Create a temporary script to run all grab commands cat >/tmp/grab_all.sh </app/grab-sites.js < site.trim() !== ''); console.log(\`Starting scheduled EPG grab at \${new Date().toISOString()}\`); // Track running processes let completedCount = 0; sites.forEach(site => { try { console.log(\`Starting grab for site: \${site}\`); let cmd = 'npm'; let args = ['run', 'grab', '--', \`--site=\${site}\`, \`--output=output/\${site}.xml\`, \`--maxConnections=\${maxConnections}\`]; // Add optional parameters if (days) args.push(\`--days=\${days}\`); if (delay) args.push(\`--delay=\${delay}\`); if (lang) args.push(\`--lang=\${lang}\`); if (gzip) args.push('--gzip'); const process = spawn(cmd, args, { stdio: 'inherit' }); process.on('close', (code) => { completedCount++; if (code === 0) { console.log(\`Successfully grabbed EPG data for site: \${site}\`); } else { console.error(\`Error grabbing EPG data for site: \${site}, exit code: \${code}\`); } if (completedCount === sites.length) { console.log(\`Completed scheduled EPG grab at \${new Date().toISOString()}\`); } }); } catch (error) { console.error(\`Error starting EPG grab for site: \${site}\`, error); completedCount++; } }); EOL chmod +x /app/grab-sites.js # Start the cron job pm2 start --name epg-grabber /app/grab-sites.js --no-autorestart --cron-restart="$CRON_SCHEDULE" pm2 logs epg-grabber & fi # Start the server to serve the guide files echo "Starting server on port 3000 to serve EPG files from /app/output" npx serve -p 3000 /app/output & # Keep the container running tail -f /dev/null