2wiki.sh
Idee
Upload ins Mediawiki via möglichst einfach gehaltenem Bash-Skript. Konstruiert, um via N900 direkt aus der Kamera-App Bilder hochzuladen
Anforderungen
Bash, Wget Sed, Grep mktemp
Skript
#!/bin/bash # whacky hacky mediawiki uploader. # (c) 2011 Hans-Werner Hilse <hilse@web.de> # this software is published under the know-what-you're-doing licence: # # 1. you may use this software for whatever you want, given that you know what you're doing. # 2. author of this software isn't responsible for anything since you knew what you're doing. # 3. if you have still questions, chances are you do not fullfill requirements #---------------------------------------------------------------------------------------------- # hier konfigurieren: APIURL="http://cccgoe.de/w/api.php" LGNAME="" LGPASS="" # hier debuggen: :-) # wenn dies in der datei vorkommt, haut das MIME/multipart/form-data nicht hin. BOUNDARY="32ruofdihgo83hkj" # naja, das waer auch ueberfluessig: FILENAME="$1" # temp file fuer die cookies SAVECOOKIES=$(mktemp /tmp/2wiki.XXXXXXX) # einloggen RESULT=$(wget --quiet -O - \ --save-cookies "$SAVECOOKIES" \ --keep-session-cookies \ --post-data="action=login&format=xml&lgname=$LGNAME&lgpassword=$LGPASS" \ "$APIURL") # neuere mediawiki brauchen dann dies zur token-bestaetigung: if echo "$RESULT"|grep -q '<login result="NeedToken"'; then LGTOKEN=$(echo $RESULT|sed 's/^.*<login result="NeedToken" token="\([^"]\+\)".*$/\1/') RESULT=$(wget --quiet -O - \ --load-cookies "$SAVECOOKIES" \ --save-cookies "$SAVECOOKIES" \ --keep-session-cookies \ --post-data="action=login&format=xml&lgname=$LGNAME&lgpassword=$LGPASS&lgtoken=$LGTOKEN" \ "$APIURL") fi # gucken, ob wir drin sind: if ! echo "$RESULT" | grep -q '<login result="Success"'; then echo "Login failure" echo $RESULT rm "$SAVECOOKIES" fi echo "Logged in." # edit-token holen (irgendwie brauchts einen Seitentitel - wir nehmen "FooBar" RESULT=$(wget --quiet -O - \ --load-cookies "$SAVECOOKIES" \ --save-cookies "$SAVECOOKIES" \ --keep-session-cookies \ --post-data="action=query&format=xml&prop=info&intoken=edit&titles=FooBar" \ "$APIURL") EDITTOKEN=$(echo $RESULT|sed 's/^.* edittoken="\([^"]\+\)".*$/\1/') # hacky MIME multipart/form-data construction: # wir brauchen das in einer Datei, da wget nicht gepuffert aus stdin liest: TEMPPOSTDATA=$(mktemp /tmp/2wiki.XXXXXX) echo -e "--$BOUNDARY\r Content-Disposition: form-data; name=\"action\"\r \r upload\r --$BOUNDARY\r Content-Disposition: form-data; name=\"filename\"\r \r $FILENAME\r --$BOUNDARY\r Content-Disposition: form-data; name=\"format\"\r \r xml\r --$BOUNDARY\r Content-Disposition: form-data; name=\"token\"\r \r $EDITTOKEN --$BOUNDARY\r Content-Disposition: form-data; name=\"file\"; filename=\"$FILENAME\"\r Content-Type: application/octet-stream\r Content-Transfer-Encoding: binary\r \r" > "$TEMPPOSTDATA" #Content-Type: image/jpeg\r cat "$FILENAME" >> "$TEMPPOSTDATA" echo -e "\r\n--$BOUNDARY--\r" >> "$TEMPPOSTDATA" # und raus mit dem Kram! RESULT=$(wget --quiet -O - \ --load-cookies "$SAVECOOKIES" \ --save-cookies "$SAVECOOKIES" \ --keep-session-cookies \ --header="Content-Type: multipart/form-data; boundary=$BOUNDARY" \ --post-file="$TEMPPOSTDATA" \ "$APIURL") if ! echo "$RESULT" | grep -q '<upload result="Success"'; then echo "Upload failure" > /dev/stderr echo $RESULT > /dev/stderr else echo "Upload successful" > /dev/stderr echo $RESULT | sed 's/^.* url="\([^"]\+\)".*$/\1/' fi rm "$TEMPPOSTDATA" rm "$SAVECOOKIES"