User:Asj/Bulk uploads: Difference between revisions
< User:Asj
Created page with "Uploading images is quite annoying on mediawiki. I use the following shell script to upload a directory of images. Make sure the filenames are unique, or set a prefix using the variable towards the top of the script. You need to create a bot account, see Special:BotPasswords - Not sure if special permissions are needed for this (they probably should be). It'll run as long as you have bash, sed, curl and jq. The sed expression is simple but I've not tested it on Ma..." |
(No difference)
|
Latest revision as of 20:55, 30 March 2025
Uploading images is quite annoying on mediawiki. I use the following shell script to upload a directory of images.
Make sure the filenames are unique, or set a prefix using the variable towards the top of the script.
You need to create a bot account, see Special:BotPasswords - Not sure if special permissions are needed for this (they probably should be).
It'll run as long as you have bash, sed, curl and jq. The sed expression is simple but I've not tested it on MacOS (which provides BSD sed instead of GNU sed).
#!/bin/bash
API="https://wiki.nottinghack.org.uk/api.php"
USERNAME=""
PASSWORD=""
COOKIES=$(mktemp)
DESCR="This text goes in the Image comment"
PREFIX="BulkUpload_"
######################################################################
# Get a login token
RESP=$(curl -s -X GET -G "$API" -c "$COOKIES" -b "$COOKIES" \
-d action=query \
-d meta=tokens \
-d type=login \
-d format=json)
LOGIN_TOKEN=$(echo "$RESP" | jq -r '.query.tokens.logintoken')
######################################################################
# Login
RESP=$(curl -s -X POST "$API" -c "$COOKIES" -b "$COOKIES" \
-F action=login \
-F "lgname=${USERNAME}" \
-F "lgpassword=${PASSWORD}" \
-F "lgtoken=${LOGIN_TOKEN}" \
-F format=json)
######################################################################
# Get a CSRF token
RESP=$(curl -s -X GET -G "$API" -c "$COOKIES" -b "$COOKIES" \
-d action=query \
-d meta=tokens \
-d format=json)
CSRF=$(echo "$RESP" | jq -r '.query.tokens.csrftoken')
######################################################################
# Perform upload
for img in img/* ; do
filename=$(basename "$img")
RESP=$(curl -s -X POST "$API" -c "$COOKIES" -b "$COOKIES" \
-H "Content-Type: multipart/form-data" \
-F "action=upload" \
-F "filename=$PREFIX$filename" \
-F "file=@$img" \
-F "comment=$DESCR" \
-F "ignorewarnings=1" \
-F "token=${CSRF}" \
-F format=json)
echo "$RESP" | jq -r '.upload.filename' | sed 's/^/File:/'
done
######################################################################
# Cleanup
rm "$COOKIES"