]> Wikimedia Canada | Git repositories - eccc_to_commons.git/commitdiff
Less naive rate limiter
authorPierre Choffet <peuc@wanadoo.fr>
Wed, 27 May 2020 17:46:50 +0000 (13:46 -0400)
committerPierre Choffet <peuc@wanadoo.fr>
Wed, 27 May 2020 17:46:50 +0000 (13:46 -0400)
mediawiki_post.sh

index 7c82687e07487a2c85b30456b6fcc1d802e5b687..bcddb358c9fa99d2f79b62f1864da693765e148a 100755 (executable)
@@ -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."