Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggested improvements for scripts/lb-wrapper [refactor logging & subprocess substitution, etc] #87

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 47 additions & 18 deletions scripts/lb-wrapper
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,76 @@

LOG_FILE="/var/log/xklb.log"
XKLB_EXECUTABLE="${XKLB_EXECUTABLE:-lb}"
VERBOSITY="-vv"
TMP_DOWNLOADS_DIR="/library/downloads/calibre-web"
SURVEY_DB_FILE="${TMP_DOWNLOADS_DIR}/survey.db"
URL="$1"

# Or download largest possible HD-style / UltraHD videos, to try to force
# out-of-memory "502 Bad Gateway" for testing of issues like #37 and #79
# FORMAT_OPTIONS="--format-sort size"

FORMAT_OPTIONS="--format best --format-sort 'tbr~1000'"
XKLB_FULL_CMD="${XKLB_EXECUTABLE} tubeadd ${SURVEY_DB_FILE} ${URL} --verbose && ${XKLB_EXECUTABLE} dl ${SURVEY_DB_FILE} --prefix ${TMP_DOWNLOADS_DIR} --write-thumbnail ${FORMAT_OPTIONS} --video ${URL} --verbose"
XKLB_FULL_CMD="${XKLB_EXECUTABLE} tubeadd ${SURVEY_DB_FILE} ${URL} ${VERBOSITY} \
&& ${XKLB_EXECUTABLE} dl ${SURVEY_DB_FILE} --prefix ${TMP_DOWNLOADS_DIR} \
--video ${URL} ${FORMAT_OPTIONS} --write-thumbnail ${VERBOSITY}"


mkdir -p ${TMP_DOWNLOADS_DIR}

# Function to log messages (and errors especially!)
# Function to log messages e.g. with level "Info" or "Error"
holta marked this conversation as resolved.
Show resolved Hide resolved
log() {
echo "$(date +'%Y-%m-%d %H:%M:%S') - $1" | tee -a ${LOG_FILE}
local level=$1
local message=$2
if [[ $message == downloading* ]]; then
echo "$message"
else
echo "$(date +'%Y-%m-%d %H:%M:%S') - [$level] $message" | tee -a ${LOG_FILE}
fi
}

if ! command -v "${XKLB_EXECUTABLE}"; then
log "xklb could not be found. Please install xklb and try again."
exit 1
fi

if [ $# -eq 0 ]; then
log "No arguments provided. Please provide a URL to download."
log "Error" "No arguments provided. Please provide a URL to download."
exit 1
fi

# URL validation already taken care of by cps/static/js/main.js Lines 194-195
# URL validation already taken care of by cps/static/js/main.js Lines 167-170
# which (1) trims outer whitespace, and (2) prepends https:// if URL doesn't
# already begin with http:// or https://
# (Test below meant well, enforcing much the same things with logging, but
# let's avoid duplicate code insofar as possible!)
# already begin with http:// or https:// (Test below means well,
# enforcing much the same and with logging, but let's avoid duplicate code!)
# if [[ ! ${URL} =~ ^http[s]?:// ]]; then
# log 'Invalid URL: xklb commands require URLs begin with "http://" or "https://"'
# exit 1
# fi

if ! command -v "${XKLB_EXECUTABLE}"; then
log "Error" "xklb could not be found. Please install xklb and try again."
exit 1
fi

log "Info" "xklb version: $(${XKLB_EXECUTABLE} --version)"

if mv ${SURVEY_DB_FILE} ${SURVEY_DB_FILE}.$(date +%F_%T_%Z) 2> /dev/null; then
log "Old ${SURVEY_DB_FILE} moved aside."
log "Info" "Old ${SURVEY_DB_FILE} moved aside."
fi

log "Running command: ${XKLB_FULL_CMD}"
if OUTPUT=$(eval "${XKLB_FULL_CMD}"); then
log "Download completed successfully."
log "Info" "Running xklb commands: ${XKLB_FULL_CMD}"

# >(...) "process substitution" explained at https://unix.stackexchange.com/a/324170
# 1>&2 redirect back-to-STDERR explained at https://stackoverflow.com/a/15936384
eval "${XKLB_FULL_CMD}" \
> >(while read -r line; do log "Info" "$line"; done) \
2> >(while read -r line; do log "Error" "$line" 1>&2; done) &
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
2> >(while read -r line; do log "Error" "$line" 1>&2; done) &
2> >(while read -r line; do log "Warning" "$line" 1>&2; done) &

pid=$!

# Wait for background process to complete
wait $pid
rc=$?

# Check return code (exit status)
if [ $rc -eq 0 ]; then
log "Info" "lb-wrapper's xklb commands (download) completed successfully."
else
log "An error occurred while running the command. Output: ${OUTPUT}"
log "Error" "Error $rc occurred while running lb-wrapper's xklb commands."
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good you log the return code here.

exit 1
fi
52 changes: 0 additions & 52 deletions scripts/lb-wrapper.greedy

This file was deleted.