Skip to content

Instantly share code, notes, and snippets.

@tyrm
Created June 27, 2024 19:52
Show Gist options
  • Save tyrm/6265e095e71dd6eb9802dca144e66d40 to your computer and use it in GitHub Desktop.
Save tyrm/6265e095e71dd6eb9802dca144e66d40 to your computer and use it in GitHub Desktop.
bark
#!/bin/bash
. readdit.env
USER_LIST="users.list"
FOLDER_SCHEME="{REDDITOR}"
FILE_SCHEME="{DATE}+{SUBREDDIT}+{POSTID}"
NORD_SLEEP=90
# Skip YAMLS?
SKIP_TO=""
if [ "$#" -eq 1 ]; then
# Set the argument as an environment variable
export SKIP_TO="$1"
echo "Skipping to $SKIP_TO"
elif [ "$#" -gt 1 ]; then
# Error message and exit if more than one argument
echo "Error: This script accepts zero or one argument only." >&2
exit 1
fi
# The log file where the output will be stored
LOG_DIR=./logs/redditor_$(date +%s)
mkdir -p ${LOG_DIR}
touch "${LOG_DIR}/BDFR.log"
log() {
timestamp="[$(date "+%Y-%m-%d %H:%M:%S")]"
logline="$@"
echo "${timestamp}${logline}" | tee -a "${LOG_DIR}/BDFR.log"
}
# Connect to Nord
reconnect
#everyother=1
while IFS= read -r user
do
if [ -n "$SKIP_TO" ]; then # Check if MY_VAR is set and not empty
if [ "${SKIP_TO}" == "$user" ]; then
echo "Resuming at ${user}"
unset SKIP_TO
else
echo "Skipping ${user}"
continue
fi
fi
command_to_run="bdfr download ${OUTPUT_FOLDER} --user ${user} --folder-scheme ${FOLDER_SCHEME} --file-scheme ${FILE_SCHEME} --no-dupes --no-dupes-cache --no-dupes-cache-path=${HASHCACHE_FILE} --search-existing --submitted --time-format=%s --limit=100"
# Maximum number of attempts
max_attempts=3
# Current attempt number
attempt=1
while [ $attempt -le $max_attempts ]; do
log "(${user})[${attempt}] Running BDFR."
LOG_FILE="${LOG_DIR}/${user}+${attempt}.log"
touch "${LOG_FILE}"
# do scrape
$command_to_run 2>&1 | tee -a "${LOG_FILE}"
# Check if the command succeeded
if [ ${PIPESTATUS[0]} -eq 0 ]; then
if grep -q "Could not find user" ${LOG_FILE}; then
#echo "The text 'Could not find user' was found in the output."
log "(${user}) User was not Found."
break
fi
if grep -q "PRAW exception" ${LOG_FILE}; then
#echo "The text 'PRAW exception' was found in the output."
log "(${user})[${attempt}] PRAW exception."
else
log "(${user})[${attempt}] BDFR succeeded."
break
fi
fi
# Disconnect and sleep
log "(${user})[${attempt}] BDFR filed."
log "(${user})[${attempt}] Sleeping ${NORD_SLEEP} seconds between attempts to be nice to nord."
sleep $NORD_SLEEP
# Reconnect VPN
log "(${user})[${attempt}] Restarting vpn."
((attempt++))
everyother=1
reconnect
done
# be nice
log "Sleeping ${NORD_SLEEP} seconds between users to be nice to nord."
sleep $NORD_SLEEP
done < "$USER_LIST"
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment