Files
projects/Dockers/iptv-epg/entrypoint.sh
T
Bram 1b3676371c
Build and Push Docker Images / build-and-push (push) Successful in 25s
timeout
2025-04-18 22:48:12 +02:00

93 lines
2.3 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
grab_all_sites() {
# Split the SITES environment variable by comma - POSIX sh compatible
echo "$SITES" | tr ',' '\n' >/tmp/sites.txt
# Process each site
while read site; do
grab_epg "$site"
done </tmp/sites.txt
# Clean up
rm /tmp/sites.txt
}
# Initial grab
if [ -n "$SITES" ]; then
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
cat >/app/grab-sites.js <<EOL
#!/usr/bin/env node
const { execSync } = require('child_process');
const sites = process.env.SITES.split(',');
sites.forEach(site => {
try {
console.log(\`Grabbing EPG data for site: \${site}\`);
let cmd = \`npm run grab -- --site=\${site} --output=output/\${site}.xml --maxConnections=\${process.env.MAX_CONNECTIONS || 1}\`;
// Add optional parameters
if (process.env.DAYS) cmd += \` --days=\${process.env.DAYS}\`;
if (process.env.DELAY) cmd += \` --delay=\${process.env.DELAY}\`;
if (process.env.LANG) cmd += \` --lang=\${process.env.LANG}\`;
if (process.env.GZIP === 'true') cmd += \` --gzip\`;
execSync(cmd, { stdio: 'inherit' });
} catch (error) {
console.error(\`Error grabbing EPG data for site: \${site}\`, error);
}
});
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"
fi
# Start the server to serve the guide files
cd /app
echo "Starting server on port 3000"
npx serve -p 3000
# Keep the container running
tail -f /dev/null