Skip to content

Commit

Permalink
add POST_DOWNLOAD_CMD
Browse files Browse the repository at this point in the history
  • Loading branch information
shizunge committed Jan 22, 2024
1 parent a44eaaf commit 7c13e8f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/dns-list-downloader.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fix_list() {
download_from_single_source_file() {
local SOURCE_FILE="${1}"
local DESTINATION_FOLDER="${2}"
local POST_DOWNLOAD_CMD="${3}"
[ -z "${SOURCE_FILE}" ] && log ERROR "SOURCE_FILE is empty." && return 1
[ -z "${DESTINATION_FOLDER}" ] && log ERROR "DESTINATION_FOLDER is empty." && return 1
local DESTINATION_FILE=
Expand Down Expand Up @@ -100,6 +101,10 @@ download_from_single_source_file() {
continue
fi
fix_list "${CURRENT_FILE}"
if [ -n "${POST_DOWNLOAD_CMD}" ]; then
log INFO "Run POST_DOWNLOAD_CMD: ${POST_DOWNLOAD_CMD} ${CURRENT_FILE}"
eval "${POST_DOWNLOAD_CMD} ${CURRENT_FILE}"
fi
log DEBUG "Merging ${CURRENT_FILE} to ${ACCUMULATOR_FILE}"
# SC2129: Consider using { cmd1; cmd2; } >> file instead of individual redirects.
# shellcheck disable=SC2129
Expand All @@ -122,6 +127,7 @@ download_from_single_source_file() {
download_lists() {
local SOURCES_FOLDER="${1}"
local DESTINATION_FOLDER="${2}"
local POST_DOWNLOAD_CMD="${3}"
if [ -z "${SOURCES_FOLDER}" ]; then
log ERROR "SOURCES_FOLDER is empty."
return 1
Expand All @@ -146,7 +152,7 @@ download_lists() {
local ACCUMULATED_ERRORS=0
SOURCE_FILE_LIST=$(find "${SOURCES_FOLDER}" -type f | sort)
for SOURCE_FILE in ${SOURCE_FILE_LIST}; do
download_from_single_source_file "${SOURCE_FILE}" "${DESTINATION_FOLDER}"
download_from_single_source_file "${SOURCE_FILE}" "${DESTINATION_FOLDER}" "${POST_DOWNLOAD_CMD}"
ACCUMULATED_ERRORS=$((ACCUMULATED_ERRORS + $?))
done
local DIR_SIZE=
Expand Down
9 changes: 6 additions & 3 deletions src/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ notify_via_apprise() {
local TITLE="${2}"
local BODY="${3}"
[ -z "${APPRISE_URL}" ] && log INFO "Skip notifying via apprise. APPRISE_URL is empty." && return 1
curl -X POST -H "Content-Type: application/json" --data "{\"title\": \"${TITLE}\", \"body\": \"${BODY}\"}" "${APPRISE_URL}"
curl -X POST -H "Content-Type: application/json" --data "{\"title\": \"${TITLE}\", \"body\": \"${BODY}\"}" "${APPRISE_URL}/notify"
}

post_blocky_lists_refresh() {
Expand Down Expand Up @@ -118,14 +118,15 @@ start_download_service() {
export LOG_SCOPE="download_service"
local SOURCES_FOLDER="${1}"
local DESTINATION_FOLDER="${2}"
local POST_DOWNLOAD_CMD="${3}"
[ -z "${STATIC_VAR_REQUEST_DOWNLOAD_FILE}" ] && log ERROR "STATIC_VAR_REQUEST_DOWNLOAD_FILE is empty" && return 1
local LAST_FILE_TIME=
local CURRENT_FILE_TIME=
while true; do
log DEBUG "Waiting for the next download request."
inotifywait -e modify -e move -e create -e delete "${STATIC_VAR_REQUEST_DOWNLOAD_FILE}" 2>&1 | log_lines DEBUG
LAST_FILE_TIME=$(head -1 "${STATIC_VAR_REQUEST_DOWNLOAD_FILE}")
download_lists "${SOURCES_FOLDER}" "${DESTINATION_FOLDER}"
download_lists "${SOURCES_FOLDER}" "${DESTINATION_FOLDER}" "${POST_DOWNLOAD_CMD}"
log INFO "Downloading done. Requesting lists refreshing."
request_refresh
CURRENT_FILE_TIME=$(head -1 "${STATIC_VAR_REQUEST_DOWNLOAD_FILE}")
Expand Down Expand Up @@ -206,6 +207,7 @@ main() {
local INTERVAL_SECONDS="${BLD_INTERVAL_SECONDS:-86400}"
local APPRISE_URL="${BLD_NOTIFICATION_APPRISE_URL:-""}"
local SOURCES_FOLDER="${BLD_SOURCES_FOLDER:-"/sources"}"
local POST_DOWNLOAD_CMD="${BLD_POST_DOWNLOAD_CMD:-""}"
local WATCH_FOLDER="${BLD_WATCH_FOLDER:-"/web/watch"}"
local WEB_FOLDER="${BLD_WEB_FOLDER:-"/web"}"
local WEB_PORT="${BLD_WEB_PORT:-8080}"
Expand All @@ -223,6 +225,7 @@ main() {
log DEBUG "INTERVAL_SECONDS=${INTERVAL_SECONDS}"
log DEBUG "APPRISE_URL=${APPRISE_URL}"
log DEBUG "SOURCES_FOLDER=${SOURCES_FOLDER}"
log DEBUG "POST_DOWNLOAD_CMD=${POST_DOWNLOAD_CMD}"
log DEBUG "WATCH_FOLDER=${WATCH_FOLDER}"
log DEBUG "WEB_FOLDER=${WEB_FOLDER}"
log DEBUG "WEB_PORT=${WEB_PORT}"
Expand All @@ -232,7 +235,7 @@ main() {
sleep 1
start_refresh_service "${BLOCKY_URL}" "${APPRISE_URL}" &
sleep 1
start_download_service "${SOURCES_FOLDER}" "${DESTINATION_FOLDER}" &
start_download_service "${SOURCES_FOLDER}" "${DESTINATION_FOLDER}" "${POST_DOWNLOAD_CMD}" &
sleep 1
start_watching_files "${WATCH_FOLDER}" &
sleep 1
Expand Down

0 comments on commit 7c13e8f

Please sign in to comment.