Skip to content

Instantly share code, notes, and snippets.

@vttc08
Created October 23, 2024 22:17
Show Gist options
  • Save vttc08/c179e959255381a30f9ccd7b924a64dc to your computer and use it in GitHub Desktop.
Save vttc08/c179e959255381a30f9ccd7b924a64dc to your computer and use it in GitHub Desktop.
Update sponsorblock from a faster mirror manually.
#!/bin/bash
# Manually update the sponsorTimes database via sb.minibomba.pro which is a much faster and up to date server. The official SponsorBlock rsync is slow and also incremental updates do not work.
# The full database import that is the most accurate takes 500-800s (excluding the download), this method takes less than 15s and it only compromises the accuracy of segments that has been updated prior to the last full import.
DOCKER_CT="postgres-sb-mirror"
THEPATH=~/docker/sponsorblock
URL="https://sb.minibomba.pro/mirror/sponsorTimes.csv.zst"
echo $(date)
cd $THEPATH
. .env
#POSTGRES_DB=
#POSTGRES_PASSWORD=
#POSTGRES_USER=
cd mirror
download() { # download and extract the sponsorTimes
if [ ! -f sponsorTimes.csv.zst ]; then
wget $URL
fi
zstd -d sponsorTimes.csv.zst -o sponsorTimes.new.csv
rm sponsorTimes.csv.zst
}
findtime() { # find the latest time in the database
number=$(docker exec $DOCKER_CT psql -U $POSTGRES_USER -d $POSTGRES_DB -t -c 'SELECT "timeSubmitted" FROM "sponsorTimes" ORDER BY "timeSubmitted" DESC LIMIT 1;' | tr -d '[:space:]')
echo $(( $number+1 ))
}
creatediff() { # create a diff file between the old and the new csv
val=$(findtime)
awk -F, -v val="$val" '$9 > val' sponsorTimes.new.csv | grep -v "hashedIP" > diff.csv
}
importdb() { # import the diff file into the database
docker exec $DOCKER_CT psql -U $POSTGRES_USER -d $POSTGRES_DB -c 'COPY "sponsorTimes" FROM '\''/mirror/diff.csv'\'' WITH (FORMAT csv, HEADER true);'
}
cleanup() { # update master csv and remove diff file
mv sponsorTimes.new.csv sponsorTimes.csv
rm diff.csv
}
full_db_reload() { # force full import of the sponsorTimes to cover
docker restart $DOCKER_CT
}
if [ -z $1 ]; then
download; findtime; creatediff; importdb; cleanup
else
for i in $*; do $i; done
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment