Skip to content

Commit

Permalink
Start to move code into functions
Browse files Browse the repository at this point in the history
This is the first step toward adding tests (#8).
  • Loading branch information
waldoj committed Mar 22, 2023
1 parent 686d06a commit a35d977
Showing 1 changed file with 67 additions and 32 deletions.
99 changes: 67 additions & 32 deletions mastobot.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit a35d977

Please sign in to comment.