From 77f4ccd72870f62719062e47eb83fa182489758b Mon Sep 17 00:00:00 2001 From: Pierre Choffet Date: Wed, 27 May 2020 13:46:50 -0400 Subject: [PATCH] Less naive rate limiter --- mediawiki_post.sh | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/mediawiki_post.sh b/mediawiki_post.sh index 7c82687..bcddb35 100755 --- a/mediawiki_post.sh +++ b/mediawiki_post.sh @@ -21,6 +21,7 @@ set -o pipefail ENDPOINT='https://commons.wikimedia.org/w/api.php' NAMESPACE='Data' +UPLOAD_MAX_RATE=15 # Per minute USERNAME_PATH='login_username' PASSWORD_PATH='login_password' @@ -109,6 +110,8 @@ login requestCSRFToken > "${CSRF_TOKEN_PATH}" readCSRFToken +MINUTE=$(date +%M) +MINUTE_UPLOADS=0 while IFS= read -r -d '' -u 9 do URI_PATH=${NAMESPACE}:$(realpath --relative-to="${SOURCE}" "${REPLY}") @@ -117,8 +120,22 @@ do case "${RESULT}" in Success) + SECOND_NOW=$(date +%S) + MINUTE_NOW=$(date +%M) + MINUTE_UPLOADS=$((MINUTE_UPLOADS+1)) + + if [ ${MINUTE_NOW} -ne ${MINUTE} ] + then + MINUTE=$(date +%M) + MINUTE_UPLOADS=0 + elif [ ${MINUTE_NOW} -eq ${MINUTE} ]&&[ ${MINUTE_UPLOADS} -eq ${UPLOAD_MAX_RATE} ] + then + sleep $((60-SECOND_NOW)) + MINUTE=$(date +%M) + MINUTE_UPLOADS=0 + fi + echo "Everything went right. Continue…" - sleep 4 ;; *) echo "Unknown code: ${RESULT}. Exiting." -- 2.42.0