Files
xteve_lazystream/root/cronjob.sh

233 lines
6.9 KiB
Bash
Executable File

#!/usr/bin/with-contenv bash
function prepend() {
time=`date +"%Y/%m/%d %H:%M:%S"`
while read line;
do echo "${time} ${1}${line}";
done;
}
exec 1> >(prepend "[cronjob.sh] ")
LOCK_DIR=/tmp/xteve/.cronjob.lock
mkdir -p /tmp/xteve
if ! mkdir "$LOCK_DIR" 2>/dev/null; then
echo "Another cronjob instance is already running, exiting."
exit 0
fi
function cleanup() {
rmdir "$LOCK_DIR" >/dev/null 2>&1 || true
}
trap cleanup EXIT
function post_xteve_cmd() {
local cmd="$1"
local retries="${2:-5}"
local retry_sleep="${3:-2}"
local response=""
for ((attempt=1; attempt<=retries; attempt++)); do
response=$(curl -sS --max-time 120 -X POST -d "{\"cmd\":\"${cmd}\"}" "http://127.0.0.1:${XTEVE_PORT}/api/" 2>&1)
if echo "$response" | grep -q '"status"[[:space:]]*:[[:space:]]*true'; then
echo "xTeVe API ${cmd}: accepted"
return 0
fi
echo "xTeVe API ${cmd}: attempt ${attempt}/${retries} failed (${response})"
sleep "$retry_sleep"
done
echo "xTeVe API ${cmd}: failed after ${retries} attempts"
return 1
}
function xteve_internal_update_due_now() {
if [ "${skip_xteve_api_when_internal_update:-yes}" != "yes" ]; then
return 1
fi
if [ ! -f /xteve/settings.json ]; then
return 1
fi
local now_hhmm
now_hhmm=$(date +"%H%M")
local update_values
update_values=$(tr -d '\n' < /xteve/settings.json | sed -n 's/.*"update"[[:space:]]*:[[:space:]]*\[\([^]]*\)\].*/\1/p' | tr -d ' "')
if [ -z "$update_values" ]; then
return 1
fi
IFS=',' read -r -a updates <<< "$update_values"
for update_time in "${updates[@]}"; do
if [ "$update_time" = "$now_hhmm" ]; then
echo "xTeVe internal update is scheduled for ${now_hhmm}; skipping wrapper API update to avoid overlap."
return 0
fi
done
return 1
}
echo "Running scripts..."
### Generate playlist and XML data from Lazystream
if [ "$use_lazystream" = "yes" ]; then
args=()
if [ "$trim_xmltv" = "yes" ]; then args+=("--trim"); fi
if [ ! -z ${quality} ]; then args+=("--quality" "$quality"); fi
if [ "$cdn" = "l3c" ]; then args+=("--cdn" "l3c"); fi
if [ ! -z "$hostOverride" ]; then args+=("--host" "$hostOverride"); fi
if [ "$include_nhl" = "yes" ]; then
echo "Running Lazystream (NHL $quality via $cdn)..."
mkdir -p /playlists/lazystream
nhl_args=()
nhl_args+=("--channel-prefix")
nhl_args+=("Lazystream: NHL")
nhl_args+=("--start-channel")
if [ -z "$nhl_start_channel" ]; then
nhl_args+=("1000")
else
nhl_args+=("$nhl_start_channel")
fi
nhl_args+=("/playlists/lazystream/lazystream-nhl")
if [ "$nhl_exclude_home" = "yes" ]; then nhl_args+=("--exclude-feeds" "HOME"); fi
if [ "$nhl_exclude_away" = "yes" ]; then nhl_args+=("--exclude-feeds" "AWAY"); fi
if [ "$nhl_exclude_national" = "yes" ]; then nhl_args+=("--exclude-feeds" "NATIONAL"); fi
if [ "$nhl_exclude_french" = "yes" ]; then nhl_args+=("--exclude-feeds" "FRENCH"); fi
if [ "$nhl_exclude_composite" = "yes" ]; then nhl_args+=("--exclude-feeds" "COMPOSITE"); fi
set -x
lazystream generate xmltv "${args[@]}" "${nhl_args[@]}"
set +x
fi
if [ "$include_mlb" = "yes" ]; then
echo "Running Lazystream (MLB $quality via $cdn)..."
mkdir -p /playlists/lazystream
mlb_args=()
mlb_args+=("--sport")
mlb_args+=("mlb")
mlb_args+=("--channel-prefix")
mlb_args+=("Lazystream: MLB")
mlb_args+=("--start-channel")
if [ -z "$mlb_start_channel" ]; then
mlb_args+=("2000")
else
mlb_args+=("$mlb_start_channel")
fi
mlb_args+=("/playlists/lazystream/lazystream-mlb")
if [ "$mlb_exclude_home" = "yes" ]; then mlb_args+=("--exclude-feeds" "HOME"); fi
if [ "$mlb_exclude_away" = "yes" ]; then mlb_args+=("--exclude-feeds" "AWAY"); fi
if [ "$mlb_exclude_national" = "yes" ]; then mlb_args+=("--exclude-feeds" "NATIONAL"); fi
if [ "$mlb_exclude_composite" = "yes" ]; then mlb_args+=("--exclude-feeds" "COMPOSITE"); fi
set -x
lazystream generate xmltv "${args[@]}" "${mlb_args[@]}"
set +x
fi
fi
# run guide2go in a loop
if [ "$use_guide2go" = "yes" ]; then
echo "Running guide2Go..."
for yamls in $YamlList
do
yamlfile="${yamls%.*}"
filecache=' "file.cache": "/guide2go/cache_'$yamls'",'
fileoutput=' "file.output": "/guide2go/'$yamlfile'.xml",'
sed -i "/file.cache/c $filecache" /guide2go/$yamls
sed -i "/file.output/c $fileoutput" /guide2go/$yamls
guide2go -config /guide2go/$yamls
done
fi
sleep 1
if [ "${bootstrap_only:-no}" = "yes" ]; then
echo "Bootstrap mode enabled, skipping xTeVe/Emby/Plex/Channels API updates."
exit 0
fi
# update xteve via API
if [ "$use_xTeveAPI" = "yes" ]; then
if xteve_internal_update_due_now; then
echo "Skipping xTeVe API update in cronjob."
else
echo "Updating xTeVe..."
if post_xteve_cmd "update.m3u"; then
sleep "${xteve_m3u_settle_seconds:-2}"
if post_xteve_cmd "update.xmltv"; then
sleep "${xteve_xmltv_settle_seconds:-20}"
post_xteve_cmd "update.xepg"
sleep "${xteve_xepg_settle_seconds:-2}"
else
echo "Skipping update.xepg because update.xmltv failed."
fi
else
echo "Skipping remaining xTeVe API updates because update.m3u failed."
fi
fi
fi
# update Emby via API
if [ "$use_embyAPI" = "yes" ]; then
echo "Updating Emby..."
if [ -z "$embyIP" ]; then
echo "no Emby credentials provided"
else
curl -s -X POST "http://$embyIP:$embyPORT/emby/ScheduledTasks/Running/$embyID?api_key=$embyApiKey" -H "accept: */*" -d ""
sleep 1
fi
fi
# update Plex via API
if [ "$use_plexAPI" = "yes" ]; then
echo "Updating Plex..."
if [ -z "${plexUpdateURL//[[:space:]]/}" ]; then
echo "no Plex credentials provided"
elif ! echo "$plexUpdateURL" | grep -Eq '^https?://[^[:space:]]+$'; then
echo "invalid plexUpdateURL, expected a plain http(s) URL. Skipping Plex update."
else
# get protocol
proto="$(echo $plexUpdateURL | grep :// | sed -e's,^\(.*://\).*,\1,g')"
# remove the protocol
url="$(echo ${plexUpdateURL/$proto/})"
# extract the host
plexHostPort="$(echo ${url/} | cut -d/ -f1)"
curl --location --request POST "$plexUpdateURL" -H "authority: $plexHostPort" -H "content-length: 0" -H "pragma: no-cache" -H "cache-control: no-cache" -H "sec-ch-ua: 'Google Chrome';v='95', 'Chromium';v='95', ';Not A Brand';v='99'" -H "accept: text/plain, */*; q=0.01" -H "x-requested-with: XMLHttpRequest" -H "accept-language: en" -H "sec-ch-ua-mobile: ?0" -H "user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36" -H "sec-ch-ua-platform: 'macOS'" -H "origin: http://$plexHostPort" -H "sec-fetch-site: same-origin" -H "sec-fetch-mode: cors" -H "sec-fetch-dest: empty" -H "referer: http://$plexHostPort/web/index.html"
sleep 1
fi
fi
# update Channels via API
if [ "$use_channelsAPI" = "yes" ]; then
echo "Updating Channels..."
if [ -z "$channelsUpdateM3uURL" ]; then
echo "no Channels M3U URL provided"
else
curl -s -X POST "$channelsUpdateM3uURL"
sleep 1
fi
if [ -z "$channelsUpdateXmltvURL" ]; then
echo "no Channels XMLTV URL provided"
else
curl -s -X PUT "$channelsUpdateXmltvURL"
sleep 1
fi
fi
exit