From a35d9773cc15519d4855cf2d5ade2c654a06faf0 Mon Sep 17 00:00:00 2001 From: Waldo Jaquith Date: Tue, 21 Mar 2023 21:26:36 -0400 Subject: [PATCH] Start to move code into functions This is the first step toward adding tests (#8). --- mastobot.sh | 99 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 67 insertions(+), 32 deletions(-) mode change 100644 => 100755 mastobot.sh diff --git a/mastobot.sh b/mastobot.sh old mode 100644 new mode 100755 index d8baebb..d5051a6 --- a/mastobot.sh +++ b/mastobot.sh @@ -11,9 +11,6 @@ MASTODON_TOKEN="ABCDefgh123456789x0x0x0x0x0x0x0x0x0x0x0" # The S3 bucket where your video clips are stored, including a trailing slash S3_BUCKET="s3://videobucket.amazonaws.com/directory/" -# Get the name of the working directory -cd "$(dirname "$0")" || exit - # Define a failure function function exit_error { printf '%s\n' "$1" >&2 @@ -22,45 +19,83 @@ function exit_error { exit "${2-1}" } -# Update the file listing 5% of the time -if [ $(( $RANDOM % 20 + 1 )) -eq 1 ]; then - aws s3 ls ${S3_BUCKET} |grep -E -o "([0-9]+).m4v" > files.txt -fi +# Copy the video over from S3 +function get_video { + aws s3 cp "${S3_BUCKET}${ENTRY}" "$ENTRY" || return 1 + return 0 +} + +# Add the clip to the history of filenames +function add_to_history { + echo "$ENTRY" >> history.txt + return 0 +} # Store the total number of clips -CLIP_COUNT=$(wc -l files.txt |cut -d " " -f 1) +function get_clip_count { + wc -l files.txt |cut -d " " -f 1 + return 0 +} -# Select a clip, making sure that it hasn't been used recently -while : -do +# Update the file listing 5% of the time, or generate it if it doesn't exist +function update_file_list { + if [ ! -f ./files.txt ] || [ $(( RANDOM % 20 + 1 )) -eq 1 ]; then + aws s3 ls ${S3_BUCKET} |grep -E -o "([0-9]+).m4v" > files.txt + if [ $? -gt 0 ]; then + exit_error "Could not update file listing" + fi + fi + return 0 +} - # Select a random filename from the list - ENTRY=$(sort -R files.txt |head -1) +# Select a clip, making sure that it hasn't been used recently +function select_clip { + while : + do + + # Select a random filename from the list + ENTRY=$(sort -R files.txt |head -1) + + # Remove any trailing carriage return from the filename + ENTRY=$(echo "$ENTRY" | tr -d '\r') + + # Ensure that the filename is a plausible length + if [ ${#ENTRY} -lt 5 ]; then + exit_error "Filename is too short" + fi + + # Divide the total number of clips in half (n clips), and if this proposed clip hasn't been + # posted in the last n times, then proceed (otherwise, loop around again) + CLIP_HISTORY=$((CLIP_COUNT/2)) + CLIP_HISTORY=$(printf "%.0f" $CLIP_HISTORY) + HISTORY=$(tail -"$CLIP_HISTORY" history.txt) + if [[ ! " ${HISTORY[*]} " =~ " ${ENTRY} " ]]; then + return 0 + fi + + done +} - # Remove any trailing carriage return from the filename - ENTRY=$(echo "$ENTRY" | tr -d '\r') +# Get the name of the working directory +cd "$(dirname "$0")" || exit - # Ensure that the filename is a plausible length - if [ ${#ENTRY} -lt 5 ]; then - exit 1 - fi +# Update the file listing +update_file_list - # Divide the total number of clips in half (n clips), and if this proposed clip hasn't been - # posted in the last n times, then proceed (otherwise, loop around again) - CLIP_HISTORY=$(($CLIP_COUNT/2)) - CLIP_HISTORY=$(printf "%.0f" $CLIP_HISTORY) - HISTORY=$(tail -"$CLIP_HISTORY" history.txt) - if [[ ! " ${HISTORY[*]} " =~ " ${ENTRY} " ]]; then - break - fi - -done +# Store the total number of clips +CLIP_COUNT=get_clip_count -# Add this to the history of filenames -echo "$ENTRY" >> history.txt +# Select a clip +select_clip # Copy the video over from S3 -aws s3 cp "${S3_BUCKET}${ENTRY}" "$ENTRY" || exit_error "Could not get video" +get_video +if [[ $? == 1 ]]; then + exit_error "Could not get video" +fi + +# Add this clip's filename to the history +add_to_history # Get the caption text ffmpeg -i "$ENTRY" -map 0:s:0 caption.srt