Files
projects/Dockers/iptv-epg/entrypoint.sh
T
Bram cdc3bbd713
Build and Push Docker Images / build-and-push (push) Successful in 24s
fix errors
2025-04-26 22:05:05 +02:00

163 lines
4.5 KiB
Bash

#!/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 <<EOL
#!/bin/sh
# Process each site concurrently
while read site; do
if [ -n "\$site" ]; then
(
echo "Starting grab 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
echo "Completed grab for site: \$site"
) &
fi
done </tmp/sites.txt
# Wait for all background processes to complete
wait
echo "All EPG data grabs completed"
EOL
chmod +x /tmp/grab_all.sh
/tmp/grab_all.sh
# Clean up
rm /tmp/sites.txt /tmp/grab_all.sh
}
# Create output directory if it doesn't exist
mkdir -p /app/output
# Initial grab
if [ -n "$SITES" ]; then
echo "Starting initial EPG data grab for all sites concurrently"
grab_all_sites
fi
# Set up cron job using PM2
if [ -n "$CRON_SCHEDULE" ] && [ -n "$SITES" ]; then
echo "Setting up cron job with schedule: $CRON_SCHEDULE"
# Create a script to grab all sites concurrently
cat >/app/grab-sites.js <<EOL
#!/usr/bin/env node
const { spawn } = require('child_process');
// Store environment variables at the beginning
const envSites = process.env.SITES || '';
const maxConnections = process.env.MAX_CONNECTIONS || 1;
const days = process.env.DAYS;
const delay = process.env.DELAY;
const lang = process.env.LANG;
const gzip = process.env.GZIP === 'true';
const sites = envSites.split(',').filter(site => 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