mad/plugins/AutoResetAndRetryDownloads.sh
kittykat d62376f7a8
# 2025.02.18 - [uploadhive] Add handling of the new /cgi-bin/dl.cgi/ url tickets (WIP)
#               (unfortunately, this is tied to the requesting ip, so downloads get "Wrong IP")
# 2025.02.18 - [up_oshi] Add Manage url as comment on uploads
# 2025.02.18 - [up_oshi / oshi] use /nossl/ url and http
# 2025.02.17 - [gofile] Add a random sleep if 429 response detected (too many requests)
# 2025.02.17 - [*ALL] Audit and update all single bracket operations
# 2025.02.17 - [filehaus] Fix downloading from fh
# 2025.02.15 - [uploadbay] Update urls regex for acceptable alternate
# 2025.02.15 - [up_sendnow] Add send.now as upload host
# 2025.02.15 - [sendnow] Fix handling of filenames with special characters in url
2025-02-19 13:41:07 +00:00

93 lines
5.5 KiB
Bash

#! Name: AutoResetAndRetryDownloads.sh
#! Author: kittykat
#! Version: 2025.02.18
#! Desc: Runs mad.sh reset after processing all urls and then relaunches MAD
#! Usage: Edit LoadPlugin="" line in mad.sh or mad.config
#! LoadPlugin="AutoResetAndRetryDownloads.sh"
#!
#! Plugin System: Additional Hook Functions added for easy of overriding:
#!
#! To Hook a Function, your function must begin with the hookable function name, and be unique
#! Best practice: HooKFuncName_<yourfilename>
#! ie.
#! PostSuccessfulDownload_myfilename() {
#! }
#!
#! Available Hook Functions:
#! -------------------------
#! * OnLoad(): Occurs after load mad.config / load plugins (prior to processing).
#! * BeginProcessing(): Occurs immediately after beginning processin of urls.txt (loops with Catnaps).
#! * PreProcessUrl(): occurs immediately after reading in an unprocessed url (^http) line to process.
#! * PostSuccessfulDownload(): occurs after a download success (is marked #OK# in the urls.txt).
#! * PostFailedDownload(): occurs after a download fails (is marked #FAIL# in the urls.txt).
#! * PostFailRetryDownload(): occurs after a download fails with a retry (is marked #RETRY# in the urls.txt).
#! * DoneProcessingAllUrls(): occurs after all the urls have finished processing (no flocks or other terms downloading).
#! * PostSuccessfulUpload(): occurs after an upload success (after upload completed ticket is created in ./downloads/).
#! * PostFailedUpload(): occurs after an upload fails definitively -- #FAIL# in the temp_upload_handler.txt
#! * PostFailRetryUpload(): occurs after an upload fails with a retry (network drop, unexpected result)
#! * DoneProcessingAllUploads: occurs after alll the files have finished processing
#!
DoneProcessingAllUrls_AutoResetAndRetryDownloads() {
local plugName='AutoResetAndRetryDownloads'
local plugFunc='DoneProcessingAllUrls_AutoResetAndRetryDownloads'
if [[ "${DebugPluginsEnabled}" == "true" ]]; then
echo -e "[${PINK}DEBUG${NC}]: Running ${PINK}$plugFunc${NC} in ${BLUE}$plugName${NC} ...${NC}"
fi
local pInputFile="$1"
arard_ticketactivetimeout=60
arard_ticketexpirytimeout=540
if [[ "$LoopThroughFileUntilComplete" == "true" ]] && [[ ! -z $CatnapDuration ]] && ((CatnapDuration > 0)); then
arard_ticketactivetimeout=$(( arard_ticketactivetimeout + (CatnapDuration * 60) ))
arard_ticketexpirytimeout=$(( arard_ticketexpirytimeout + (CatnapDuration * 60) ))
fi
if grep -Eqi '^#(http.*#RETRY#|direct=http.*#RETRY#).*$' "$pInputFile" ; then
if [[ ! -f "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt" ]]; then
echo $(date +%s) > "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt"
else
arard_startTime=$(cat "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt")
arard_currTime=$(date +%s)
arard_durationseconds=$((arard_currTime - arard_startTime))
if ((arard_durationseconds > arard_ticketexpirytimeout)); then
if [[ "${DebugPluginsEnabled}" == "true" ]]; then
echo -e "[${PINK}DEBUG${NC}]: Ticket expiry occurred ($arard_durationseconds > $arard_ticketexpirytimeout)..${NC}"
fi
echo $(date +%s) > "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt"
arard_startTime=$(cat "${WorkDir}/.temp/AutoResetAndRetryDownloadsarard_startTime.txt")
arard_currTime=$(date +%s)
arard_durationseconds=$((arard_currTime - arard_startTime))
elif ((arard_durationseconds > arard_ticketactivetimeout)); then
if [[ "${DebugPluginsEnabled}" == "true" ]]; then
echo -e "[${PINK}DEBUG${NC}]: Ticket inactive ($arard_durationseconds > $arard_ticketactivetimeout)..${NC}"
fi
for ((a=1; a<=2; a++)); do
printf "\033[1A\r"
printf "\33[2K\r"
done
echo -e "${NC}"
echo -e "${YELLOW}Reset&Retry${NC}: Done processing... ${BLUE}$((arard_durationseconds/60))${NC} min(s). Expiry ${PINK}$(( (arard_ticketexpirytimeout - arard_durationseconds)/60 ))${NC} min(s).${NC}"
return 0
fi
if [[ "${DebugPluginsEnabled}" == "true" ]]; then
echo -e "[${PINK}DEBUG${NC}]: Ticket active ($arard_durationseconds)..${NC}"
fi
for ((a=1; a<=2; a++)); do
printf "\033[1A\r"
printf "\33[2K\r"
done
echo -e "${NC}"
echo -e "${YELLOW}Reset&Retry${NC}: Dozing ${BLUE}1${NC} min. Processing for ${BLUE}$((arard_durationseconds/60))${NC} of ${BLUE}$((arard_ticketactivetimeout/60))${NC} min(s)${NC}. Expiry ${PINK}$(( (arard_ticketexpirytimeout - arard_durationseconds)/60 ))${NC} min(s).${NC}"
sleep 1m
fi
if [[ ! -f "${WorkDir}/.temp/AutoResetAndRetryDownloadsCnt.lk" ]]; then
touch "${WorkDir}/.temp/AutoResetAndRetryDownloadsCnt.lk"
trap "rm -f ${WorkDir}/.temp/AutoResetAndRetryDownloadsCnt.lk; echo ""; tput cnorm; exit" 0 1 2 3 6 15
madReset "$pInputFile" "true" "false" "false" "true"
rm -f "${WorkDir}/.temp/AutoResetAndRetryDownloadsCnt.lk"
fi
qChkLineCount=$(grep -Ei '^(http|direct=http)' "${pInputFile}" | wc -l | awk '{ print $1 }')
if ((qChkLineCount > 0)) ; then
ReloadScript "$@"
fi
fi
return 0
}